Jump to content
  • Advertisement
Sign in to follow this  
GameMasterXL

Reading XML using C#

This topic is 4598 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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]

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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);
}

Share this post


Link to post
Share on other sites
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;
}


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!