XML Loading/Saving

For all mod-related questions and custom code.
Post Reply
BinaryX
Offline
Posts: 8
Joined: October 13th, 2011, 3:52 pm
Location: The Netherlands
Contact:

XML Loading/Saving

Post by BinaryX »

Basic demonstration of how-to load and save custom configuration by using XML.
A xml file must consist of atleast one line (otherwise its deemed invalid):

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
The layout of an xml file is build with elements and attributes, the most important one is the root element:

Code: Select all

<root-element>
   <parent-element>
        <parent attr1="" attr2="" />
       <child-element>
           <baby attr1="" attr2="" />
       </child-element>
   </parent-element>
</root-element>
An element is considered to be a section; in this section you should declare elements that fit in that particular section (like how i did it above).

Another example of xml is:

Code: Select all

<root-element>
   <parent attr1="" attr2="" />
   <child attr1="" attr2="" />
</root-element>
This would work the same but do it properly and make the necessary sections, keeps things organized right.
Now onto the more serious stuff: saving and loading of xml files, thats the fun part.
Your making custom ranking system for fCraft, it would be nice if the server loads the ranks at startup, lets use xml for this.
I will keep it compact, this topic is aimed at xml and not at the code for a ranking system.
The name of the ranking system will be MyRankingSystem, there will be a total of 16 ranks.
Every x amount of blocks built the player gets ranked up to a certain maximum.

XML Layout (setup from my server):
Ranks.xml

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<MyRankingSystem>
  <Ranks>
    <Rank blocks="100"  name="Rookie" />
    <Rank blocks="600" name="Worker" />
    <Rank blocks="1500" name="Builder" />
    <Rank blocks="3000" name="Constructer" />
    <Rank blocks="5500" name="Miner" />
    <Rank blocks="6500" name="Coal Miner" />
    <Rank blocks="80007" name="Steel Miner" />
    <Rank blocks="10000" name="Gold Miner" />
    <Rank blocks="15000" name="Creeper Hunter" />
    <Rank blocks="20000" name="Scavenger" />
    <Rank blocks="25000" name="Collector" />
    <Rank blocks="32000" name="Engineer" />
    <Rank blocks="45000" name="Epic Builder" />
    <Rank blocks="55000" name="Dragon Slayer" />
    <Rank blocks="100000" name="Creeper Lord" />
    <Rank blocks="200000" name="Like A Boss" />
  </Ranks>
</MyRankingSystem>
Loading

Code: Select all

XDocument doc = XDocument.Load( "Ranks.xml" );
XElement root = doc.Root; //obviously the first element in the xml file, MyRankingSystem.
if (root != null)
{
   XElement FirstEl = root.Element("Ranks"); //find the element Ranks in the MyRankingSystem element.
   foreach (XElement el in FirstEl.Elements("Rank")) //loop through every single element starting with Rank in the Ranks element.
   {
      Logger.LogToConsole("Found rank " + el.Attribute("name").Value); //display the Name attr of the el Rank in the console (16 ranks 16 times).
   }
}
Yep~! it is that easy, one of the reasons why i love XML.
Alright now lets do the saving which work almost the same way.

Saving

Code: Select all

            XDocument doc = XDocument.Load("Ranks.xml");
            XElement root = doc.Root;
            if (root != null)
            {
                XElement FirstEl = root.Element("MyRankingSystem");
                List<XElement> SaveEl = new List<XElement>();
                foreach (Rank rank in loaded)
                {
                    XElement el = new XElement("Rank");
                    el.Add(new XAttribute("blocks", Convert.ToInt32(rank.blocks)));
                    el.Add(new XAttribute("name", rank.Name));
                    SaveEl.Add(el);
                }
                if (SaveEl.Count != 0) FirstEl.RemoveAll();
                foreach (XElement el in SaveEl)
                {
                    FirstEl.Add(el);
                }
                doc.Save("Ranks.xml");
}
That's it, hope you learnt something from this.
Feel free to PM me with any questions or write directly in this topic.

Jonty800
Offline
Posts: 280
Joined: August 21st, 2011, 6:31 am
Location: United Kingdom
Contact:

Re: XML Loading/Saving

Post by Jonty800 »

Thanks for the tutorial.
I had a problem with "cannot access blah.xml, its already in use", in an app I made (non-fCraft) while saving the XML. Would this happen in fCraft? It seemed that the stream didn't close after loading.
You cannot use certain BBCodes: [img].

BinaryX
Offline
Posts: 8
Joined: October 13th, 2011, 3:52 pm
Location: The Netherlands
Contact:

Re: XML Loading/Saving

Post by BinaryX »

It shouldn't, just don't forget to close and clean everything up.

Post Reply