Search code examples
c#xmldita

How to find a XML tag with a specific attribute (in C#)


I need to get a list of tags that contain a specific attribute. I am using DITA xml and I need to find out all tags that has a href attribute.

The problem here is that the attribute may be inside any tag so XPath will not work in this case. For example, an image tag may contain a href, a topicref tag may contain a href, and so on.

So I need to get a XmlNodeList (as returned by the getElementByTagName method). Ideally I need a method getElementByAttributeName that should return XmlNodeList.


Solution

  • I might have misunderstood your problem here, but I think you could possibly use an XPath expression.

    var nodes = doc.SelectNodes("//*[@href='pic1.jpg']");
    

    The above should return all elements with href='pic1.jpg', where doc is the XmlDocument