Click here to Skip to main content
15,912,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
please help me find node in XML.
my XML file is:

XML
<?xml version="1.0" encoding="UTF-8"?>
<DistanceMatrixResponse>
 <status>OK</status>
 <origin_address>California, USA</origin_address>
 <destination_address>Louisiana, USA</destination_address>
 <row>
  <element>
   <status>OK</status>
   <duration>
    <value>110693</value>
    <text>1 day 7 hours</text>
   </duration>
   <distance>
    <value>3037646</value>
    <text>3,038 km</text>
   </distance>
  </element>
 </row>
</DistanceMatrixResponse>


I want distance from this XML file. I'm using this code but it gives error
C#
XmlDocument xml = new XmlDocument();
  xml.LoadXml(tempString);  // suppose that str string contains "<Names>...</Names>"

//  XmlNodeList xnList = xml.SelectNodes("/DistanceMatrixResponse/text/[@type='distance']");

  XmlNode node = xml.SelectSingleNode("DistanceMatrixResponse/row/element/distance/text()");
  foreach (XmlNode xn in node)
  {
      Console.WriteLine(xn.InnerText);
  }
Posted
Updated 31-Jan-12 0:02am
v4
Comments
walterhevedeich 31-Jan-12 5:42am    
Whats the error?

Do you mean:
C#
XmlDocument xml = new XmlDocument();
xml.Load("myfile.xml");

XmlNode node;
node = xml.SelectSingleNode("DistanceMatrixResponse/row/element/distance/text");

Console.WriteLine(node.InnerText);

?
 
Share this answer
 
Use following code to read Xml file-
C#
Now add namespace - using System.Xml;
on button click write code as-
XmlTextReader reader = new XmlTextReader("C:\\Users\\Dell\\Desktop\\New folder\\XMLFile1.xml");   //this will location and name of xml 
               while (reader.Read())
                {
                  switch (reader.NodeType)
                    {
                  case XmlNodeType.Element: // The node is an element.
                        MessageBox.Show("<" + reader.Name);
                       //MessageBox.Show(">");
                       //MessageBox.Show(reader.Value);
                        break;
                  case XmlNodeType.Text: //Display the text in each element.
                        MessageBox.Show(reader.Value);        
                        break;
                  case XmlNodeType.EndElement: //Display the end of the element.
                        //Console.Write("</" + reader.Name);
                        //MessageBox.Show(">");
                         break;
                     }
                  }
 
Share this answer
 
v2
The .NET Framework comes with different library classes used to parse XML; please see my overview of them below:


  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
 
The .NET Framework comes with different library classes used to parse XML; please see my overview of them below:


  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
 

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