locked
how to read xml file using asp.net C# RRS feed

  • Question

  • User-2101450227 posted

    hi,

    i want to read xml file data from asp.net c#

    and select a node in to label that what i want

    thanks for the help.

    Tuesday, December 23, 2014 8:24 AM

Answers

  • User1577371250 posted
    XmlDocument document = new XmlDocument();
    document.Load(@"C:....\test.xml"); // Load XML File
    
    XmlNode node = document.SelectSingleNode("/root/Name");
    string name = node.InnerText;
    
    node = document.SelectSingleNode("/root/City");
    string city = node.InnerText;
    
    <?xml version="1.0" encoding="utf-8" ?> 
    <root>
    
       <Name>John</Name>
    
      <City>KA</City>
    
    </root>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 23, 2014 8:42 AM

All replies

  • User1577371250 posted
    XmlDocument document = new XmlDocument();

    // string xmlString = ""; // If you are passing XML As String // document.LoadXml(xmlString); // Load XML string
    document.Load(@"C:....\test.xml"); // Load XML File XmlNodeList nodes = document.SelectNodes("/root/string"); foreach (XmlNode item in nodes) { string res = item.InnerText; }

    <?xml version="1.0" encoding="utf-8" ?> 
    <root>
    
       <string> ABC  </string>
    
      <string> CYZ </string>
    
    </root>



    Tuesday, December 23, 2014 8:29 AM
  • User-2101450227 posted

    if i load want a abc data in label only abc data what should i do.

    Tuesday, December 23, 2014 8:32 AM
  • User1577371250 posted

    This code is for the above example  and  the XPATH depends on your XML

    Label1.Text = nodes[0].InnerText;

    Tuesday, December 23, 2014 8:34 AM
  • User-2101450227 posted

    can any one provide me full example :)

    thanks for the help

    Tuesday, December 23, 2014 8:36 AM
  • User1577371250 posted

    can you Post your sample XML? or try the below samples

    http://msdn.microsoft.com/en-us/library/cc189056(v=vs.95).aspx

    http://www.dotnetperls.com/xmlreader

    Tuesday, December 23, 2014 8:39 AM
  • User1577371250 posted
    XmlDocument document = new XmlDocument();
    document.Load(@"C:....\test.xml"); // Load XML File
    
    XmlNode node = document.SelectSingleNode("/root/Name");
    string name = node.InnerText;
    
    node = document.SelectSingleNode("/root/City");
    string city = node.InnerText;
    
    <?xml version="1.0" encoding="utf-8" ?> 
    <root>
    
       <Name>John</Name>
    
      <City>KA</City>
    
    </root>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, December 23, 2014 8:42 AM
  • User-484054684 posted

    can any one provide me full example :)

    Hi Immad,

    You can also use LINQ to XML. The complete sample for the similar requirement is found on this article:

    http://www.codeproject.com/Articles/38534/LinQ-To-Xml-retrieve-element-value-by-its-attribut

    Tuesday, December 23, 2014 8:51 AM