XPathSelectElements 返回 null
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:您的示例 XML 没有在名称空间中具有带有
nss
别名的id
元素。在这种情况下,它将是
,或者会设置默认命名空间。对于这个答案,我假设实际上您要查找的元素位于名称空间中。您的查询正在尝试在根级别查找名为
id
的元素。要查找所有id
元素,您需要:...尽管我个人会使用:(
我更喜欢使用 LINQ to XML 类型上的查询方法而不是 XPath.. 。我发现更容易避免愚蠢的语法错误等。)
您的示例代码也不清楚,因为您使用的是尚未声明的
xDoc
...它有助于编写完整< /em> 示例,理想情况下包括作为控制台应用程序编译和运行所需的所有内容。EDIT: Your sample XML doesn't have an
id
element in the namespace with thenss
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 allid
elements, you need:... although personally I'd use:
(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.我在问题提交 3 小时后、(最后)编辑后 41 分钟后查看该问题。
提供的 XML 文档中没有定义命名空间。
这个 XPath 表达式显然不会从提供的 XML 文档中选择任何节点,因为 XML 文档没有名为
Lists
的顶部元素(实际顶部元素的名称是SEARCH< /代码>)
该语句是错误的,因为
//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.
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 isSEARCH
)This statement is false, because
//ID
selects the only element named ID in the provided XML document, thus the value of the C# variableid
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.出现空值或系统返回值的原因是由于以下
XpathSElectElements是System.xml.linq.XElment,它是linq查询的日期。它不能直接输出。
获取单个第一个匹配元素
使用 XPathSelectElement("//ID");
您可以使用 XPathSelectElements 检查出现次数,因为
您还可以使用特定条件按顺序查询 linq 语句
为了从列表中获取节点值,您可以使用此
对于第二个列表,您还没有获得值,因为列表项的 XPath不正确
The reason for the null value or system returned value is due to the following
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
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
For Second list you havnt got the value since, the XPath for list items is not correct