Search code examples
c#xmlforeachgetelementsbytagnamexmlnodelist

Reading an XML in C#


I'm using System.Xml to read a xml file in C#. First I open the file (locally)... and use foreach to get the values, like this:

XmlNodeList titles = xmlDoc.GetElementsByTagName("title");
foreach (XmlNode title in titles)
{
rowNews = new ListViewItem();
rowNews.Text = (title.ChildNodes[0].Value);
listView1.Items.Add(rowNews);
}

The problem is, I have many rss tags called title in my file, I'd like to read only those what are inside <entry></entry>?


Solution

  • See ParentNode and LocalName properties:

    if (title.ParentNode.LocalName == "entry") { ... }