Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to read the values of HeadId and Amount from the following XML:
XML
<PARAMS>
<BasicSetup HeadId="10" Amount="122" />
<BasicSetup HeadId="11" Amount="222" />
<BasicSetup HeadId="12" Amount="333" />
<BasicSetup HeadId="13" Amount="444" />
</PARAMS>
Posted

Different approached to XML parsing are offered by .NET FCL. This is my short overview of them:


  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


—SA
 
Share this answer
 
here is simple solution create class BasicSetup

C#
class BasicSetup
       {
           public string HeadId { get; set; }
           public string Amount { get; set; }
       }



and here is code to read xml object

C#
string path = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\XMLData.xml";
            XDocument objXML = XDocument.Load(path);
            List<BasicSetup> objPARAMS = (from b in objXML.Descendants("BasicSetup")
                            select new BasicSetup()
                            {
                                HeadId =  b.Attribute("HeadId").Value,
                                Amount = b.Attribute("Amount").Value
                            }).ToList<BasicSetup>();


            foreach(BasicSetup obj in objPARAMS )
            {
                Console.WriteLine(String.Format("HeadId : {0} and Amount : {1}.", obj.HeadId,obj.Amount));
            }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900