Jon Lambell

Jon Lambell

  • NA
  • 1
  • 0

Reading and writing XML file Attributes

Jul 15 2008 9:45 AM
Hey all,

I'm new to C#, frech from Learning C at Uni this year.
I'm writing a Chat Bot which will resond to commands, but I'm having some problems with one of the features.

Here's What's I've wrote so far:

        public static void OnAlt2(Chat chat, CommandArgs e)
        {
            XmlTextReader altxml = new XmlTextReader("alts.xml");
            altxml.Read();
            while (altxml.Read())
            {
                if (altxml.NodeType == XmlNodeType.Element) // The node is an element.
                {
                        altxml.MoveToNextAttribute(); // Read the attributes.
                        if(altxml.Value == e.Character)
                        {
                            altxml.MoveToNextAttribute();
                            string altlist = altxml.Value;
                            string[] arInfo = new string[7];

                            // define which character is seperating fields
                            char[] splitter = { ',' };
                            arInfo = altlist.Split(splitter);
                            for (int x = 0; x < arInfo.Length; x++)
                            {
                                if (arInfo[x] == e.Args[0])
                                {
                                    e.Reply("True");
                                    return;
                                }
                            }
                            e.Reply("Can't find that character from list: " + altlist);
                            e.Reply("Write to XML Attributes is incomplete");
                        }
                }
            }
        }

Here is the XML:
<?xml version="1.0"?>
<Alts>
    <Alt main="Bareshaman" alts="Largoh,Largo"></Alt>
</Alts>

I'm having trouble writing to the alts Attribute, everything I try either crashes or doesn't compile.
If Someone called Bareshaman types !alt Largoh, it will reply with "True", but if he types !alt Rago, it should add to the alt Attribute making the XML file read: <Alt main="Bareshaman" alts="Largoh,Largo.Rago"></Alt>

I'd also like it to add a new element when someone new types the command.
So if Bob comes along and types !alt Fred, the XML file should read:
<?xml version="1.0"?>
<Alts>
    <Alt main="Bareshaman" alts="Largoh,Largo"></Alt>
    <Alt main="Bob" alts="Fred"></Alt>
</Alts>

I hope that's understandable.

Thanks very much

Answers (1)