问题
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?
回答1:
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);
回答2:
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");
回答3:
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.
来源:https://stackoverflow.com/questions/4308118/get-attribute-values-from-matching-xml-nodes-using-xpath-query