How to parse (read) xml files in ASP.NET and C# sample code?

How to parse (read) xml files in ASP.NET and C# sample code?

How to read the attribute of xml node in ASP.NET and in C#?
How to read the value of xml node in ASP.NET and in C#?


<Library>
  <Books>
    <book check="yes">
      <name>ASP.NET</name>
    </book>
    <book check="no">
      <name>C#</name>
    </book>
    <book check="yes">
      <name>SQL</name>
    </book>
    <book check="yes">
      <name>Sharepoint</name>
    </book>
  </Books>
</Library>

Sample code to get all the book names where check is equals to “yes”

XmlDocument doc = new XmlDocument();
                doc.Load(“<xmlfile >”);
                XmlElement root = doc.DocumentElement;
                XmlNodeList nodes = root.SelectNodes("Books/book");
foreach (XmlNode node in nodes)
                        {
                            if (node.Attributes["check"].Value.ToLower().Equals("yes"))
                            {
                                strName = node["name"].InnerText;
                            }
                        }

No comments:

Post a Comment