| CODE |
//Add reference to XML using System.Xml;
... XmlDocument doc = new XmlDocument(); //XmlString must contain a valid xml string doc.LoadXml(XmlString); |
That's all there is to loading an xml.
To search for certain tags:
| CODE |
XmlNodeList xmlNList = doc.GetElementsByTagName("Image"); XmlElement listElement; for (int i = 0; i< xmlNList.Count; i++) { listElement = xmlNList.Item(i) as XmlElement; if (listElement != null) { //...do your thing //you can use the GetAttribute method to query attributes. i.e.: listElement.GetAttribute("Source")); } } |
I'm also using a generic function which you may find usefull
| CODE |
private XmlElement FindChildElement(XmlElement pElement, string ElementName, string AttributeValue, bool Detach) { if (pElement == null || (ElementName == null || ElementName.Length == 0)) return null;
XmlNodeList xmlNList = pElement.SelectNodes(ElementName); for (int i = 0; i { if (AttributeValue.Length >0) { //modify this code and loop thru available attributes if (!xmlNList.Item(i).Attributes["Text"].Value.Equals(AttributeValue)) continue; }
if (Detach) { XmlElement pReturn = xmlNList.Item(i).Clone() as XmlElement; pElement.RemoveChild(xmlNList.Item(i)); return pReturn; } else return (xmlNList.Item(i) as XmlElement); } return null; } |
An example showing how to use the above code:
| CODE |
XmlElement listElement = null; //let's find the Menu element listElement = FindChildElement((doc.ChildNodes[0] as XmlElement), "Menu", "", false); //ChildNodes[0] is the root of the document //let's find the Submenu called 'Games" listElement = FindChildElement(listElement, "List", "Games", false); |
Note: For brevity's sake, all error handlers have been removed.