使用XPath查询从匹配的XML节点获取属性值

Sar*_*els 11 c# xml extension-methods xpath xpathnavigator

这似乎不应该是困难的,但我现在卡住了.我正在尝试从与给定XPath查询字符串匹配的节点获取特定属性的属性值.这是我到目前为止所拥有的:

    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;
                    }
                }
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

这目前抛出异常:

System.InvalidCastException:无法将类型为"MS.Internal.Xml.Cache.XPathDocumentNavigator"的对象强制转换为"System.Xml.IHasXmlNode".

我错了吗?是否有更简单的方法从匹配节点获取属性值?

Sim*_*ier 32

对于以下xml:

<root>
  <elem att='the value' />
</root>
Run Code Online (Sandbox Code Playgroud)

您可以使用此C#代码获取"值"文本

    XmlDocument xdoc = new XmlDocument();
    xdoc.LoadXml(text);
    Console.WriteLine(xdoc.SelectSingleNode("/root/elem/@att").Value);
Run Code Online (Sandbox Code Playgroud)


Shi*_*mar 5

如果您使用 .net 3.5 或更高版本,则可以使用 linq to Xml

对于给定的 xml 文档

<?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>
Run Code Online (Sandbox Code Playgroud)

以下 linq 表达式将为您提供所有存储过程节点的“名称”属性的值

XDocument xDcoument = XDocument.Load(xmlStoredProcSchemeFile);

  var storedProcedureNames = from doc in xDcoument.Descendants("storedProcedure")
                             select doc.Attribute("name").Value;
Run Code Online (Sandbox Code Playgroud)

您还可以使用常规 XPath 语法。在下面的代码中,变量节点保存由“usp_GET_HOME_PAGE_DATA”名称标识的节点,然后属性变量保存所选节点的所有子节点(属性)及其子节点。

  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");
Run Code Online (Sandbox Code Playgroud)

  • “你应该完全放弃它并使用 jQuery” (5认同)
  • 检查 http://meta.stackexchange.com/questions/19478/the-many-memes-of-meta/19492#19492 **;)** (2认同)