Using XML
The best way to understand XML is to look at two different XML files. The first one is a simple one
<?xml version="1.0"?> <configure> <ljmu> <Introduction>20</Introduction> <Method>10</Method> <Results>20</Results>> <Discussion>40</Discussion> <Additional>10</Additional> <date>28-11-2007</date> </ljmu> <access> <Introduction>20</Introduction> <Method>10</Method> <Results>20</Results> <Discussion>40</Discussion> <Additional>10</Additional> <date>28-11-2007</date> </access> </configure>
It is simple enough to parse through a structure like this. ident is the node to be searched for and attr is the node. XmlReader.Create(conpath, settings, null) creates a reader stream.
public int dotheread(string ident, string attr) { XmlReaderSettings settings = new XmlReaderSettings(); XmlReader reader = XmlReader.Create(conpath, settings, null); int retval = -1; reader.MoveToAttribute(attr); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == ident) { reader.Read(); retval = Convert.ToInt32(reader.Value); break; } } } reader.Close(); if (retval == -1) { DialogResult result; result = MessageBox.Show(this, "Unable to find the element", "D'oh!", MessageBoxButtons.OK); } return retval; }
Writing XML like this is also very simple
XmlTextWriter xmlWriter = new XmlTextWriter(conpath,null); xmlWriter.Formatting = Formatting.Indented; xmlWriter.WriteStartDocument(); xmlWriter.WriteStartElement("configure"); xmlWriter.WriteStartElement("ljmu"); xmlWriter.WriteElementString("Introduction","20"); xmlWriter.WriteElementString("Method","10"); xmlWriter.WriteElementString("Results","20"); xmlWriter.WriteElementString("Discussion","40"); xmlWriter.WriteElementString("Additional","10"); xmlWriter.WriteElementString("Date", dateToday); xmlWriter.WriteEndElement(); xmlWriter.WriteStartElement("access"); xmlWriter.WriteElementString("Introduction","20"); xmlWriter.WriteElementString("Method","10"); xmlWriter.WriteElementString("Results","20"); xmlWriter.WriteElementString("Discussion","40"); xmlWriter.WriteElementString("Additional","10"); xmlWriter.WriteElementString("Date", dateToday); xmlWriter.WriteEndElement(); xmlWriter.WriteEndElement(); xmlWriter.WriteEndDocument(); xmlWriter.Close();
This is a very simple example, but it can be much more entertaining...