Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当两个子节点都必须存在时,如何使用XPath从XML文件中提取子节点?_C#_Xpath_Xpathnavigator - Fatal编程技术网

C# 当两个子节点都必须存在时,如何使用XPath从XML文件中提取子节点?

C# 当两个子节点都必须存在时,如何使用XPath从XML文件中提取子节点?,c#,xpath,xpathnavigator,C#,Xpath,Xpathnavigator,我试图从xml文档中提取某些值。在下面的示例中,我希望将存储在“c”和“d”节点中的值存储在列表中,但仅当“b”节点同时包含“c”和“d”时。到目前为止,我的代码循环了所有的“b”节点,但我不确定在while循环中放什么,或者这是否是最好的方法 XmlDocument attrsXML = new XmlDocument(); attrsXML.LoadXml(dbReader["SampleXml"].ToString()); XPathNavigator nav = attrsXML.Cr

我试图从xml文档中提取某些值。在下面的示例中,我希望将存储在“c”和“d”节点中的值存储在列表中,但仅当“b”节点同时包含“c”和“d”时。到目前为止,我的代码循环了所有的“b”节点,但我不确定在while循环中放什么,或者这是否是最好的方法

XmlDocument attrsXML = new XmlDocument();
attrsXML.LoadXml(dbReader["SampleXml"].ToString());

XPathNavigator nav = attrsXML.CreateNavigator();

XPathNodeIterator attribNodes = nav.Select("/a/b");

while (attribNodes.MoveNext())
{
    // What do I need to put here in order to extract the 'c' and 'd' nodes?
    // Any other nodes can be ignored (such as 'e' above). I am only interested
    // when 'b' contains both 'c' AND 'd'.
}
其中,从数据库加载的“SampleXml”是:

<a>
    <b>
        <c>Extract this</c>
        <d>And this</d>
        <e>not this</e>
    </b>
    <b>
        <c>not this</c>
        <e>not this</e>
    </b>
    <b>
        <c>Extract this</c>
        <d>And this</d>
    </b>
</a>

提取这个
还有这个
不是这个
不是这个
不是这个
提取这个
还有这个
感谢您的帮助。

我是这样解决的:

while (attribNodes.MoveNext())
{
    string cText = String.Empty;
    string dText = String.Empty;

    XPathNavigator nav2 = attribNodes.Current;

    var cNode = nav2.SelectSingleNode("c");

    if (cNode != null)
    {
        cText = nameNode.ToString();

        var dNode = nav2.SelectSingleNode("d");
        if (dNode != null)
        {
            dText = dNode.ToString();
        }
    }

    if (dText != String.Empty && cText != String.Empty)
    {
        // Problem solved
    }
}
欢迎任何更好的解决方案,因为它看起来并不优雅。

我是这样解决的:

while (attribNodes.MoveNext())
{
    string cText = String.Empty;
    string dText = String.Empty;

    XPathNavigator nav2 = attribNodes.Current;

    var cNode = nav2.SelectSingleNode("c");

    if (cNode != null)
    {
        cText = nameNode.ToString();

        var dNode = nav2.SelectSingleNode("d");
        if (dNode != null)
        {
            dText = dNode.ToString();
        }
    }

    if (dText != String.Empty && cText != String.Empty)
    {
        // Problem solved
    }
}

欢迎使用任何更好的解决方案,因为它看起来并不优雅。

您可以使用以下代码:

XmlDocument attrsXML = new XmlDocument();
attrsXML.LoadXml(dbReader["SampleXml"].ToString());


XmlNodeList nodeList = attrsXML.SelectNodes("/a/b[c and d]");

foreach (XmlNode xmlNode in nodeList)
{
  string cText = xmlNode.SelectSingleNode("c").InnerText;
  string dText = xmlNode.SelectSingleNode("d").InnerText;
}

XPath“/a/b[c和d]”将包含c和d元素的所有b元素作为子元素返回,这意味着您不需要在循环中手动检查它。

您可以使用以下代码:

XmlDocument attrsXML = new XmlDocument();
attrsXML.LoadXml(dbReader["SampleXml"].ToString());


XmlNodeList nodeList = attrsXML.SelectNodes("/a/b[c and d]");

foreach (XmlNode xmlNode in nodeList)
{
  string cText = xmlNode.SelectSingleNode("c").InnerText;
  string dText = xmlNode.SelectSingleNode("d").InnerText;
}

XPath“/a/b[c和d]”将包含c和d元素的所有b元素作为子元素返回,这意味着您不需要在循环中手动检查它。

谢谢eminsenay-我已改为使用您的解决方案。比我的好多了!谢谢你,eminsenay-我已经改用你的解决方案了。比我的好多了!