.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Xml fle parsing various ways

Posted By:Maverick       Posted Date: May 17, 2011    Points: 200    Category: .Net Framework    URL: http://www.dotnetspark.com  

In this article we will see various approch for parsing xml file using C#
 

Hello,

If you want to parse the following xml file. I have mentioned lot of techniques for it.

 <Peak>
        <SignalDesc>DAD1 A, Sig=254,4 Ref=550,100SignalDesc>
        <PeakType>BB PeakType>
        <ExpRetTime Unit="min">0.747087ExpRetTime>
        <MeasRetTime Unit="min">0.74711MeasRetTime>
        <Area Unit="mAU*s">300.036407Area>
        <Height Unit="mAU">106.920616Height>
        <Width Unit="min">0.044739Width>
        <Symmetry>0.716419Symmetry>
        <Name>DimethylphthalateName>
        <Amount Unit="wt%">0.0905459542Amount>
        <kPrime>0.867774kPrime>
        <PlatesHalfWidth>1266.536758PlatesHalfWidth>
        <ResolutionHalfWidth/>
        <Selectivity/>
        <Skew>1.226811Skew>
        <Excess>3.487737Excess>
        <WidthHalfHeight>0.049412WidthHalfHeight>
        <Width5Sigma>0.10902Width5Sigma>
        <WidthTangent>0.075845WidthTangent>
        <WidthTailing>0.106667WidthTailing>
        <USPTailing>1.098393USPTailing>
        <TimeIncrement Unit="msec">800TimeIncrement>
        <DataPoints>22DataPoints>
        <StatisticalMoment0>296.804047StatisticalMoment0>
        <StatisticalMoment1>0.752654StatisticalMoment1>
        <StatisticalMoment2>0.000491StatisticalMoment2>
        <StatisticalMoment3>0.000013StatisticalMoment3>
        <StatisticalMoment4>1.566e-006StatisticalMoment4>
        <PlatesTangent>1552.502327PlatesTangent>
        <Plates5Sigma>1174.085003Plates5Sigma>
        <PlatesStatistical>1153.031523PlatesStatistical>
        <ResolutionTangent/>
        <Resolution5Sigma/>
        <ResolutionStatistical/>
      Peak>
1) Using XMLREADER

public void ParseFile()
    {
        //parse using reader.
        string path = Server.MapPath("myfile.xml");
 
        using (XmlReader reader = new XmlTextReader(path))
        {
            reader.MoveToContent(); 
            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element || reader.NodeType == XmlNodeType.Tet)
                {
//Read SignalDesc                      if (reader.Name == "SignalDesc")                     {                         string value = reader.ReadElementString();                                              }                 }             }         }     }
2) Using linq

 private void ReadXml()
    {
        string path = Server.MapPath("Chemstation.xml");
        XDocument xmlDoc = XDocument.Load(path);
 
        var players = from player in xmlDoc.Descendants("Peak")
                      select new
                      {
                          SignalDesc = player.Element("SignalDesc").Value,
                          PeakType = player.Element("PeakType").Value,
                          ExpRetTime = player.Element("ExpRetTime").Value,
                      };
        foreach (var player in players)
        {
            string SignalDesc = players.signaldesc
        }
    }
3) Using Xpath

 public static string GetNodeValue(string sFileName, string sSelectExpression)
    {
        //parser using xpath.
        string sRetVal = "";
        // Read the XML document
        XPathDocument myXPathDocument = new XPathDocument(sFileName);
        XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator();
        XPathNodeIterator myXPathNodeIterator = myXPathNavigator.Select(sSelectExpression);
        while (myXPathNodeIterator.MoveNext())
        {
            //Console.WriteLine("<" + myXPathNodeIterator.Current.Name + "> " + myXPathNodeIterator.Current.Value);
            sRetVal = myXPathNodeIterator.Current.Value;
        }
        return sRetVal;
    }
--Function Call
 GetNodeValue("c:/myfile.xml", "/Peak/SignalDesc");
Thanks,
Maverick

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend