11

This doesn't seem like it should be difficult, but I'm stuck currently. I'm trying to get the attribute values for a particular attribute from nodes that match a given XPath query string. Here's what I have so far:

    public static IEnumerable<string> GetAttributes(this XmlDocument xml,
        string xpathQuery, string attributeName)
    {
        var doc = new XPathDocument(new XmlNodeReader(xml));
        XPathNavigator nav = doc.CreateNavigator();
        XPathExpression expr = nav.Compile(xpathQuery);
        XPathNodeIterator iterator = nav.Select(expr);
        while (iterator.MoveNext())
        {
            XPathNavigator curNav = iterator.Current;
            if (curNav.HasAttributes)
            {
                XmlNode curNode = ((IHasXmlNode)curNav).GetNode();
                if (null != curNode)
                {
                    XmlAttribute attrib = curNode.Attributes[attributeName];
                    if (null != attrib)
                    {
                        yield return attrib.Value;
                    }
                }
            }
        }
    }

This currently throws an exception:

System.InvalidCastException: Unable to cast object of type 'MS.Internal.Xml.Cache.XPathDocumentNavigator' to type 'System.Xml.IHasXmlNode'.

Am I going about this wrong? Is there a simpler way to get attribute values from matching nodes?

Micha Wiedenmann
  • 19,979
  • 21
  • 92
  • 137
Sarah Vessels
  • 30,930
  • 33
  • 155
  • 222
  • 1
    Why not use LinqToXml? This would cut down on the noise considerably unless I am missing some reason why you couldn't do this with Linq? – Brad Cunningham Nov 29 '10 at 21:15

3 Answers3

33

For the following xml:

<root>
  <elem att='the value' />
</root>

You can get the "the value" text with this C# code

    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(text);
    Console.WriteLine(xdoc.SelectSingleNode("/root/elem/@att").Value);
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298
  • Derp. I had seen `SelectNodes` was a method on an `XmlElement`, like `xmlDoc.DocumentElement`, but didn't think to check `XmlDocument`. Using your example but with `SelectNodes` works for me. – Sarah Vessels Nov 29 '10 at 21:35
5

If you're using .net 3.5 or later you could use linq to Xml

For a given xml document

<?xml version="1.0" encoding="utf-8" ?>
<root>
  <storedProcedures>
    <storedProcedure name="usp_GET_HOME_PAGE_DATA">
      <resultSet name="Features"/>
      <resultSet name="Highlights"/>
    </storedProcedure>
    <storedProcedure name="usp_GET_FEATURES" />
    <storedProcedure name="usp_GET_FEATURE" />
    <storedProcedure name="usp_UPDATE_FEATURE" />
    <storedProcedure name="usp_GET_FEATURE_FOR_DISPLAY">
      <resultSet name="CurrentFeature"/>
      <resultSet name="OtherFeatures"/>
    </storedProcedure>
    <storedProcedure name="usp_GET_HIGHLIGHT_TITLES">
      <resultSet name="Highlights"/>
    </storedProcedure>
  </storedProcedures>
</root>

The following linq expression will get you the values of the "name" attributes of all storedProcedure node

XDocument xDcoument = XDocument.Load(xmlStoredProcSchemeFile);

  var storedProcedureNames = from doc in xDcoument.Descendants("storedProcedure")
                             select doc.Attribute("name").Value;

you could also use regular XPath syntax. In the code below the variable node holds the node identified by the "usp_GET_HOME_PAGE_DATA" name and then the attributes variable holds all child nodes (the attributes) of the selected node and it's children.

  XmlDocument xmlDocument = new XmlDocument();
  xmlDocument.Load(@"C:\inetpub\wwwroot\ASPNETBuilder\BusinessLayer\DataAccessCodeGenerationSchema.xml");
  var node = xmlDocument.DocumentElement.SelectSingleNode("./storedProcedures/storedProcedure[@name='usp_GET_HOME_PAGE_DATA']");
  var attributes = node.SelectNodes("./resultSet/@name");
Shiv Kumar
  • 9,599
  • 2
  • 36
  • 38
  • 5
    "You should totally drop that and use jQuery" –  Nov 29 '10 at 21:44
  • What are you talking about? This is on the server side or in a GUI client. Where are you getting Javascript/jQuery from? – Shiv Kumar Nov 29 '10 at 21:45
  • 2
    Check http://meta.stackexchange.com/questions/19478/the-many-memes-of-meta/19492#19492 **;)** –  Nov 29 '10 at 23:16
1

solution to the initial problem of the exception being thrown...

var doc = new XPathDocument(new XmlNodeReader(xml));

should be replaced by...

var doc = new XmlDocument();
doc.load(*you can either specify the path to the file, the string out of which the xml document is to be generated or specify an xmlreader, look for more overloads*);

This will not throw the exception and the code will work just fine.