It is currently January 21st, 2018, 10:10 am


All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: XML Loading/Saving
PostPosted: May 16th, 2012, 7:19 am 

Joined: October 13th, 2011, 3:52 pm
Posts: 8
Location: The Netherlands
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:
<?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:
<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:
Show
Code:
<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):

Show
Ranks.xml
Code:
<?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
Show
Code:
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
Show
Code:
            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.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: XML Loading/Saving
PostPosted: June 8th, 2012, 8:36 am 

Joined: August 21st, 2011, 6:31 am
Posts: 280
Location: United Kingdom
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].


Top
Offline Profile  
Reply with quote  
 Post subject: Re: XML Loading/Saving
PostPosted: August 28th, 2012, 4:00 pm 

Joined: October 13th, 2011, 3:52 pm
Posts: 8
Location: The Netherlands
It shouldn't, just don't forget to close and clean everything up.


Top
Offline Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net