A xml file must consist of atleast one line (otherwise its deemed invalid):
Code: Select all
<?xml version="1.0" encoding="utf-8"?>
Code: Select all
<root-element>
<parent-element>
<parent attr1="" attr2="" />
<child-element>
<baby attr1="" attr2="" />
</child-element>
</parent-element>
</root-element>
Another example of xml is:
Code: Select all
<root-element>
<parent attr1="" attr2="" />
<child attr1="" attr2="" />
</root-element>
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>
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).
}
}
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");
}
Feel free to PM me with any questions or write directly in this topic.