Reading XML using C#

Started by
20 comments, last by GameMasterXL 18 years, 5 months ago
I have searched everywere and havn't found much good info on this. Is what i am wanting to do is read in a xml file that will store how my program looks like the background colour, font colour, font size, font type, font style ect ect so then i could change the programs looks with xml. This is what a basic xml file for my program would look like:

<buttons>
 <bgcolor>red</bgcolor>
 <fontcolor>blue</fontcolor>
 <fontsize>12</fontsize>
 <fonttype>arial</fonttype>
 <fontstyle>bold</fontstyle>
</buttons>

How would i read this in so i can get each individual value to change my programs properties and be able to allways expand on the xml file aswell? thanks in advance. [/source]
Advertisement
There are a ton of XML-with-C# articles here.
In .NET 1.1 you want the XmlSerializer class. Things have apparently changed a bit for .NET 2.0 though, but I haven't had a chance to check out XML serialization in 2.0 yet.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
Quote:Original post by Dave Hunt
There are a ton of XML-with-C# articles here.


I did find one usefull there but it only seemed to read in all commands like this
<width> .. </width>
<height> .. </height>

and i don't think i can encapsulate the attributes into each object like in my xml setup file i want to be able to change buttons ui, textboxs, window, font ect and each one of those are in its own catogary with its own attributes like
<buttons>
<width>123</width>
</buttons>


I will also look into serialization to but i suck at xml lol.
You might try a format more like this:
<buttons> <property name="bgcolor" value="red" /> <property name="fontcolor" value="blue /> <property name="fontsize" value="12" /> <property name="fonttype" value="arial" /> <property name="fontstyle" value="bold" /></buttons>

I find that a bit easier to parse.
How do i parse that with C# xml classes? so i can read in the values of each attribute and assigne the value into my programs global variables.
You have a couple of options when it comes to XML in .NET:

  • The System.Xml namespace provides a XmlDocument which you can use to construct and "destruct" XML documents. You pretty much just write it node by node or read through it node by node.
  • The IXmlSerializable interface will let you serialize classes by reading and writing the members your own way.
  • With the XmlSerializer class, you can serialize classes to xml dynamically.

Play around with each method of creating and reading XML data and pick which method you like best. It really depends on what you're trying to accomplish with what you're doing.
Rob Loach [Website] [Projects] [Contact]
Is what i am creating is a setup file so i can read the xml data in and each element has attributes then i read each attribute in and assigne the value of each into the right variable. How can i do this can you by anychance show some code? thanks.
Here's some code from my game. Obviously, it has nothing to do with whatever you're doing, but you might be able to gleam something from it.

    public static void loadShipsXml() {        SHIPS = new Dictionary<string, ShipData>();        XmlDocument doc = new XmlDocument();        doc.Load(Settings.DATA_PATH + @"ships.xml");        XmlNode rootNode = doc.DocumentElement;        XmlNodeList allShips = rootNode.ChildNodes;        for(int i = 0; i < allShips.Count; i++) {            ShipData nextShip = new ShipData();            XmlElement current = (XmlElement) allShips;            XmlNodeList children = current.ChildNodes;            for(int j = 0; j < children.Count; j++) {                if(children[j].Name == "speed") {                    nextShip.speed = double.Parse(children[j].InnerText);                } else if(children[j].Name == "hp") {                    nextShip.hp = int.Parse(children[j].InnerText);                } else if(children[j].Name == "graphics") {                    nextShip.graphics = Settings.processXMLGraphics(children[j]);                } else if(children[j].Name == "component") {                    nextShip.components.AddLast(ComponentData.COMPONENTS[children[j].InnerText]);                }            }            SHIPS.Add(current.Attributes[0].Value, nextShip);        }
"For sweetest things turn sour'st by their deeds;Lilies that fester smell far worse than weeds."- William Shakespere, Sonnet 94
Side note:

C# allows switches on strings:

               if(children[j].Name == "speed") {                    nextShip.speed = double.Parse(children[j].InnerText);                } else if(children[j].Name == "hp") {                    nextShip.hp = int.Parse(children[j].InnerText);                } else if(children[j].Name == "graphics") {                    nextShip.graphics = Settings.processXMLGraphics(children[j]);                } else if(children[j].Name == "component") {                    nextShip.components.AddLast(ComponentData.COMPONENTS[children[j].InnerText]);                }


can become:

               switch (children[j].Name)             {               case "speed":                    nextShip.speed = double.Parse(children[j].InnerText);                    break;                case "hp":                    nextShip.hp = int.Parse(children[j].InnerText);                    break;                case "graphics"                    nextShip.graphics = Settings.processXMLGraphics(children[j]);                    break;                case "component":                    nextShip.components.AddLast(ComponentData.COMPONENTS[children[j].InnerText]);                    break;                }

This topic is closed to new replies.

Advertisement