I am working on XmlElement in c#. I have an XmlElement. The source of the XmlElement will look like the sample below.
Sample:
<data>
<p>hello all
<strong>
<a id="ID1" href="#" name="ZZZ">Name</a>
</strong>
</p>
<a id="ID2" href="#" name="ABC">Address</a>
</data>
I have to loop through the above XML to Search for element name a. I also want to extract the ID of that element into a variable.
Basically I want to get the ID attribute of the element <a>. It may occur as a child element or as a separate parent.
Can any one help how it can be done.
Since you’re using C# 4.0 you could use linq-to-xml like this:
Should give you the element
aregardless of where it is in the hierarchy.From your comment, for code that only uses the XmlDocument and XmlElement classes the equivalent code would be: