Re: Reading an XMLFile

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi Jon,

I prefer XmlDocument and selecting nodes using XPath queries when reading xml, so below is an alternative solution.

Claimkey is not in your sample xml so I don't know where it would be.

XmlDocument doc = new XmlDocument();
doc.LoadXml(@"c:\xml\isn_prod.XML-0139.xml");

XmlNodeList list = doc.SelectNodes("/comment_list/comment");

foreach (XmlNode node in list)
{
sb.AppendLine("Date: " + node["date"].InnerText);
sb.AppendLine("Author: " + node["author"].InnerText);
sb.AppendLine("Visible to: " + node["visible_to"].InnerText);
sb.AppendLine("Comment: " + node["comment"].InnerText);
}

If you just want a single comment node based on its item_no attribute, use

XmlNode node = doc.SelectSingleNode("/comment_list/comment[@item_no='2']");


--
Happy Coding!
Morten Wennevik [C# MVP]
.



Relevant Pages

  • Re: Update a specific XML node
    ... XmlNode node = ... XPathNavigator navigator = document.CreateNavigator; ... SelectSingleNode or SelectNodes on XmlDocument. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Xml performance comparison
    ... for reading xml, ... you can use XMLTextReader for better performance. ... > to archive this operation which are XMLDocument, XMLElement and ReadXML, ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: string to XmlNode
    ... to get a XmlNode from it? ... XmlTextReader xmlReader = new XmlTextReader(new ... XmlDocument xmlDocument = new XmlDocument; ... XmlNode node = xmlDocument.ReadNode; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Read XML element value
    ... Load the XML file in a XMLDocument object and get the desired not using ... XPath query. ... XmlDocument doc = new XmlDocument; ... XmlNode node = doc.DocumentElement; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Read XML element value
    ... Load the XML file in a XMLDocument object and get the desired not using ... XPath query. ... XmlDocument doc = new XmlDocument; ... XmlNode node = doc.DocumentElement; ...
    (microsoft.public.dotnet.languages.csharp)