tags:

views:

49

answers:

3

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.

+3  A: 

If you're on C#, then the following approach might work for you:

XDocument document = XDocument.Load(xmlReader);
XAttribute xa = new XAttribute("href", "pic1.jpg");
var attrList = document.Descendants().Where (d => d.Attributes().Contains(xa));
Developer Art
+5  A: 

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

Chris Taylor
Not possibly, definitely.
annakata
The more generic XPATH to return all elements that have an href attribute, regardless of what it's value is: `//*[@href]`
Mads Hansen
A: 

i made it by writing this code:

        XmlTextReader reader = new XmlTextReader(FILE_NAME);
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(reader);`enter code here`
        reader.Close();
        XmlElement root = xmlDoc.DocumentElement;
        XmlNodeList newXMLNodes = xmlDoc.SelectNodes("/catalog/item[<yourAtributeKey>=<yourAtributeValue>]");
        foreach (XmlNode newXMLNode in newXMLNodes)
        {
            // here you cand do anythink with newXMLNode variable
        }
        xmlDoc.Save(FILE_NAME);
AEMLoviji