[英]c# how to use xml.SelectSingleNode method
I'm using c# .NET 4.8 and I'm trying to extract a value from an XML document.我正在使用 c# .NET 4.8,我正在尝试从 XML 文档中提取一个值。 Here is a sample of the document I'm using:这是我正在使用的文档示例:
<SOME_GROUP SomeValue="2">
<SOME_PLACEHOLDER SOMEDateTime="2022-02-10T14:50:48-08">
<SOME_DATA>
<MAIN_SOME SomeValue="2" MAINSOMEID="MAINSOME1">
<TARGET_FIELD MAINFIELDID="MAIN111" SomeID="somedata" _Value="someValue">
<_SUB_FIELD _Code="038" _Text="some other data"></_SUB_FIELD>
</TARGET_FIELD>
</MAIN_SOME>
</SOME_DATA>
</SOME_PLACEHOLDER>
</SOME_GROUP>
I want to get the _Value
from TARGET_FIELD
and store it in a variable.我想从TARGET_FIELD
获取_Value
并将其存储在一个变量中。 Right now I can do this using the method SelectNodes()
like this:现在我可以使用SelectNodes()
方法来做到这一点,如下所示:
using System.Xml;
XmlDocument someXmlDocument = new XmlDocument();
someXmlDocument.LoadXml(someXmlFile);
var myValue = someXmlDocument.SelectNodes("/SOME_GROUP/SOME_PLACEHOLDER/SOME_DATA/MAIN_SOME/TARGET_FIELD")
.Cast<XmlNode>()
.Select(x => x.Attributes["_Value"].Value).ToList();
and I get my value someValue
in the first element of the list myValue
.我在列表myValue
的第一个元素中得到我的值someValue
。 It seems like because there is only one node I'm getting this value from I should use the SelectSingleNode()
method instead.似乎因为只有一个节点我从中获取此值我应该改用SelectSingleNode()
方法。
If I try this:如果我试试这个:
var myValue = someXmlDocument.SelectSingleNode("/SOME_GROUP/SOME_PLACEHOLDER/SOME_DATA/MAIN_SOME/TARGET_FIELD")
.Cast<XmlNode>()
.Select(x => x.Attributes["_Value"].Value);
the code runs but I don't get anything stored in myValue
as I expect.代码运行,但我没有像预期的那样在myValue
中存储任何内容。
If I leave off the ToList()
in an attempt to store the value in myValue
as just a string like this:如果我离开ToList()
试图将值存储在myValue
中,就像这样的字符串:
var myValue = someXmlDocument.SelectNodes("/SOME_GROUP/SOME_PLACEHOLDER/SOME_DATA/MAIN_SOME/TARGET_FIELD")
.Cast<XmlNode>()
.Select(x => x.Attributes["_Value"].Value);
then I get a null reference exception like this:然后我得到一个 null 引用异常,如下所示:
System.NullReferenceException: 'Object reference not set to an instance of an object.' System.NullReferenceException:“对象引用未设置为 object 的实例。”
Sorry if this is a stupid question, I'm just getting back into c# development.对不起,如果这是一个愚蠢的问题,我只是回到 c# 开发。 Can anyone tell me what I'm doing wrong when trying to use SelectSingleNode()
or if that is even something I should be trying to use in this case?谁能告诉我在尝试使用SelectSingleNode()
时我做错了什么,或者在这种情况下我是否应该尝试使用它?
As always, a correct, clearly explained answer will be marked as accepted and upvoted.与往常一样,正确、解释清楚的答案将被标记为已接受和已投票。
Thanks.谢谢。
Try this...尝试这个...
public static void DoSelectSingleNode()
{
var xml = "<SOME_GROUP SomeValue=\"2\"><SOME_PLACEHOLDER SOMEDateTime=\"2022-02-10T14:50:48-08\"><SOME_DATA><MAIN_SOME SomeValue=\"2\" MAINSOMEID=\"MAINSOME1\"><TARGET_FIELD MAINFIELDID=\"MAIN111\" SomeID=\"somedata\" _Value=\"someValue\"><_SUB_FIELD _Code=\"038\" _Text=\"some other data\"></_SUB_FIELD></TARGET_FIELD></MAIN_SOME></SOME_DATA></SOME_PLACEHOLDER></SOME_GROUP>";
var xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml);
var value = xmlDocument.DocumentElement.SelectSingleNode("//TARGET_FIELD").Attributes["_Value"].InnerText;
Console.WriteLine(value);
}
... naturally, if there are more than one TARGET_FIELD
then you'll only pull back one. ...自然地,如果有多个TARGET_FIELD
,那么您只会撤回一个。
I find this to be a good resource when it comes to using XPath for node selections... https://devhints.io/xpath在使用 XPath 进行节点选择时,我发现这是一个很好的资源... https://devhints.io/xpath
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.