Slinidy
Slinidy

Reputation: 387

How to get "elements" in node

I trying to get "elements" from node and show in MessageBox

My XML: <Item Name="Test" Count="5"/>

Elements:

Name
Count

My Code:

XmlNodeList xmlNodes = xmlDocument.SelectNodes("Item");
                            foreach (XmlNode xmlNode in xmlNodes)
                            {
                                MessageBox.Show(xmlNode.InnerText);
                            }

But I do not know how to do this

Upvotes: 0

Views: 70

Answers (1)

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30625

You need to use Attributes. Check my solution

XmlNodeList xmlNodes = xmlDocument.SelectNodes("Item");
foreach (XmlNode xmlNode in xmlNodes)
{
    foreach (XmlAttribute attr in xmlNodes.Attributes)
    {
         MessageBox.Show($"Attribute Name is {attr.Name} and Value is {attr.Value}");
    }
}

Upvotes: 3

Related Questions