XPathSelectElements 返回 null

发布于 2024-12-22 02:03:58 字数 1208 浏览 1 评论 0原文

Load 函数已在 xmlData 类中定义,

public class XmlData
{
    public void Load(XElement xDoc)
    {
        var id = xDoc.XPathSelectElements("//ID");
        var listIds = xDoc.XPathSelectElements("/Lists//List/ListIDS/ListIDS");
    }
}

我只是从我的一端调用 Load 函数。

            XmlData aXmlData = new XmlData();

            string input, stringXML = "";
            TextReader aTextReader = new StreamReader("D:\\test.xml");
            while ((input = aTextReader.ReadLine()) != null)
            {
                stringXML += input;
            }
            XElement Content = XElement.Parse(stringXML);
            aXmlData.Load(Content);

在加载函数中,我将 id 和 listIds 都设置为 null。

我的 test.xml 包含

<SEARCH>
  <ID>11242</ID>
  <Lists>
    <List CURRENT="true" AGGREGATEDCHANGED="false">
      <ListIDS>
        <ListID>100567</ListID>
        <ListID>100564</ListID>
        <ListID>100025</ListID>
        <ListID>2</ListID>
        <ListID>1</ListID>
      </ListIDS>
    </List>
  </Lists>
</SEARCH>

Load function is already defined in xmlData class

public class XmlData
{
    public void Load(XElement xDoc)
    {
        var id = xDoc.XPathSelectElements("//ID");
        var listIds = xDoc.XPathSelectElements("/Lists//List/ListIDS/ListIDS");
    }
}

I'm just calling the Load function from my end.

            XmlData aXmlData = new XmlData();

            string input, stringXML = "";
            TextReader aTextReader = new StreamReader("D:\\test.xml");
            while ((input = aTextReader.ReadLine()) != null)
            {
                stringXML += input;
            }
            XElement Content = XElement.Parse(stringXML);
            aXmlData.Load(Content);

in load function,im getting both id and and listIds as null.

My test.xml contains

<SEARCH>
  <ID>11242</ID>
  <Lists>
    <List CURRENT="true" AGGREGATEDCHANGED="false">
      <ListIDS>
        <ListID>100567</ListID>
        <ListID>100564</ListID>
        <ListID>100025</ListID>
        <ListID>2</ListID>
        <ListID>1</ListID>
      </ListIDS>
    </List>
  </Lists>
</SEARCH>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

一腔孤↑勇 2024-12-29 02:03:58

编辑:您的示例 XML 没有在名称空间中具有带有 nss 别名的 id 元素。在这种情况下,它将是 ,或者会设置默认命名空间。对于这个答案,我假设实际上您要查找的元素位于名称空间中。

您的查询正在尝试在根级别查找名为 id 的元素。要查找所有 id 元素,您需要:

var tempId = xDoc.XPathSelectElements("//nss:id", ns);

...尽管我个人会使用:(

XDocument doc = XDocument.Parse(...);
XNamespace nss = "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner";
// Or use FirstOrDefault(), or whatever...
XElement idElement = doc.Descendants(nss + "id").Single();

我更喜欢使用 LINQ to XML 类型上的查询方法而不是 XPath.. 。我发现更容易避免愚蠢的语法错误等。)

您的示例代码也不清楚,因为您使用的是尚未声明的 xDoc...它有助于编写完整< /em> 示例,理想情况下包括作为控制台应用程序编译和运行所需的所有内容。

EDIT: Your sample XML doesn't have an id element in the namespace with the nss alias. It would be <nss:id> in that case, or there'd be a default namespace set up. I've assumed for this answer that in reality the element you're looking for is in the namespace.

Your query is trying to find an element called id at the root level. To find all id elements, you need:

var tempId = xDoc.XPathSelectElements("//nss:id", ns);

... although personally I'd use:

XDocument doc = XDocument.Parse(...);
XNamespace nss = "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner";
// Or use FirstOrDefault(), or whatever...
XElement idElement = doc.Descendants(nss + "id").Single();

(I prefer using the query methods on LINQ to XML types instead of XPath... I find it easier to avoid silly syntax errors etc.)

Your sample code is also unclear as you're using xDoc which hasn't been declared... it helps to write complete examples, ideally including everything required to compile and run as a console app.

浅忆流年 2024-12-29 02:03:58

我在问题提交 3 小时后、(最后)编辑后 41 分钟后查看该问题。

提供的 XML 文档中没有定义命名空间。

 var listIds = xDoc.XPathSelectElements("/Lists//List/ListIDS/ListIDS");

这个 XPath 表达式显然不会从提供的 XML 文档中选择任何节点,因为 XML 文档没有名为 Lists 的顶部元素(实际顶部元素的名称是 SEARCH< /代码>)

var id = xDoc.XPathSelectElements("//ID");

在加载函数中,我将 id 和 listIds 都设为 null。

该语句是错误的,因为//ID选择了所提供的XML文档中唯一名为ID的元素,因此C#变量id的值是非空的。可能您在编辑 XML 文档后没有进行彻底的测试。

最有可能的是原始 ID 元素属于某个命名空间。但现在它处于“无命名空间”状态,并且上面的 XPath 表达式确实选择了它。

I am looking at the question 3 hours after it was submitted and 41 minutes after it was (last) edited.

There are no namespaces defined in the provided XML document.

    var listIds = xDoc.XPathSelectElements("/Lists//List/ListIDS/ListIDS");

This XPath expression obviously doesn't select any node from the provided XML document, because the XML document doesn't have a top element named Lists (the name of the actual top element is SEARCH)

var id = xDoc.XPathSelectElements("//ID");

in load function,im getting both id and and listIds as null.

This statement is false, because //ID selects the only element named ID in the provided XML document, thus the value of the C# variable id is non-null. Probably you didn't test thoroughly after editing the XML document.

Most probably the original ID element belonged to some namespace. But now it is in "no namespace" and the XPath expression above does select it.

错爱 2024-12-29 02:03:58
        string xmldocument = "<response xmlns:nss=\"http://schemas.microsoft.com/SQLServer/reporting/reportdesigner\"><action>test</action><id>1</id></response>";

        XElement Content = XElement.Parse(xmldocument);
        XPathNavigator navigator = Content.CreateNavigator();
        XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable);
        ns.AddNamespace("nss", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
        var tempId = navigator.SelectSingleNode("/id");
        string xmldocument = "<response xmlns:nss=\"http://schemas.microsoft.com/SQLServer/reporting/reportdesigner\"><action>test</action><id>1</id></response>";

        XElement Content = XElement.Parse(xmldocument);
        XPathNavigator navigator = Content.CreateNavigator();
        XmlNamespaceManager ns = new XmlNamespaceManager(navigator.NameTable);
        ns.AddNamespace("nss", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
        var tempId = navigator.SelectSingleNode("/id");
梨涡少年 2024-12-29 02:03:58

出现空值或系统返回值的原因是由于以下

 var id = xDoc.XPathSelectElements("//ID");

XpathSElectElements是System.xml.linq.XElment,它是linq查询的日期。它不能直接输出。
获取单个第一个匹配元素
使用 XPathSelectElement("//ID");
您可以使用 XPathSelectElements 检查出现次数,因为

var count=xDoc.XPathSelectElements("//ID").count();

您还可以使用特定条件按顺序查询 linq 语句

为了从列表中获取节点值,您可以使用此

foreach (XmlNode xNode in xDoc.SelectNodes("//ListIDS/ListID"))
{
Console.WriteLine(xNode.InnerText);
}

对于第二个列表,您还没有获得值,因为列表项的 XPath不正确

The reason for the null value or system returned value is due to the following

 var id = xDoc.XPathSelectElements("//ID");

XpathSElectElements is System.xml.linq.XElment which is linq queried date. It cannot be directly outputed as such.
To Get individual first match element
use XPathSelectElement("//ID");
You can check the number of occurrences using XPathSelectElements as

var count=xDoc.XPathSelectElements("//ID").count();

you can also query the linq statement as order by using specific conditions

Inorder to get node value from a list u can use this

foreach (XmlNode xNode in xDoc.SelectNodes("//ListIDS/ListID"))
{
Console.WriteLine(xNode.InnerText);
}

For Second list you havnt got the value since, the XPath for list items is not correct

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文