Home     ProductivityTools     Articles     Fun fatcs     How To     C Sharp     Science News     Contact Us     Submit your website     General Blog     Jobs      
c# Keywords
ASP .NET
C# Utopia
How do I add text items t
How to download a file fr
How to print all values i
How to copy text to buffe
How to bind dataset to da
How to parse xml using li
How to launch Bat file
How to launch message box
How to read text file
How to append to text fil
How to set focus on a con
How to generate new GUID
How to parse xml using linq to xml?
 
Include using System.Xml.Linq;
 

foreach (XElement fieldsElement in XElement.Load(@"C:\my.xml").Elements("fields"))

{

foreach (XElement fieldElement in fieldsElement.Elements("field"))

{

this.richTextBox1.AppendText(fieldElement.Attribute("number").Value + " :- " + fieldElement.Attribute("name").Value + "\n");

myField t = new myField(fieldElement.Attribute("number").Value, fieldElement.Attribute("name").Value, fieldElement.Attribute("type").Value);

foreach (XElement valueElement in fieldElement.Elements("value"))

{

this.richTextBox1.AppendText("\t" + valueElement.Attribute("enum").Value + " = " + valueElement.Attribute("description").Value + "\n");

t.addMapEntry(valueElement.Attribute("enum").Value, valueElement.Attribute("description").Value);

}

myFields.Add(t.tagNumber, t);

}

}

 

 

 

This is helpful in parsing following xml.

 

<fields>

  <field number='4' name='myenum' type='CHAR'>
   <value enum='1' description='val1' />
   <value enum='a' description='val2' />
   <value enum='c' description='val3' />
  </field>

</fields>

 

The parsing is done using the XElement class provided in linq to xml. This class is my favourite for parsing xmls. Very helful, useful and simple class.