C# XmlElement:SelectSingleNode 为空字符串返回 null?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1173177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# XmlElement: SelectSingleNode returns null for empty string?
提问by Roee Adler
I'm new to C#, and just started using XmlElement and its SelectSingleNode method. In my XML file there's a tag that may have a value (i.e. <tag>value</tag>
) or be empty (i.e. <tag></tag>
). If it's empty, SelectSingleNode returns null.
我是 C# 新手,刚开始使用 XmlElement 及其 SelectSingleNode 方法。在我的 XML 文件中,有一个标签可能有一个值(即<tag>value</tag>
)或为空(即<tag></tag>
)。如果为空,则 SelectSingleNode 返回 null。
I'm currently using the following code to catch the value of the tag:
我目前正在使用以下代码来捕获标签的值:
XmlElement elem = ....
string s = elem.SelectSingleNode("somepath").Value;
This code obviously raises an exception for empty tags. However, for me an empty tag is a valid value, where I expect the value of my string to be "".
这段代码显然引发了空标签的异常。但是,对我来说,空标签是一个有效值,我希望字符串的值为“”。
Wrapping each call to SelectSingleNode with try...catch seems a huge waste of code (I have many fields that may be empty), and I'm sure there's a better way to achieve this.
用 try...catch 包装对 SelectSingleNode 的每次调用似乎是对代码的巨大浪费(我有许多可能为空的字段),我相信有更好的方法来实现这一点。
What is the recommended approach?
推荐的方法是什么?
EDIT:
编辑:
Following requests, a sample XML code will be:
根据请求,示例 XML 代码将是:
<Elements>
<Element>
<Name>Value</Name>
<Type>Value</Type> <-- may be empty
<Color>Value</Color>
</Element>
<Element>
<Name>Value</Name>
<Type>Value</Type>
<Color>Value</Color>
</Element>
</Elements>
The CS code:
CS代码:
XmlDocument doc = new XmlDocument();
doc.Load("name.xml");
foreach (XmlElement elem in doc.SelectNodes("Elements/Element"))
{
myvalue = elem.SelectSingleNode("Type/text()").Value;
}
采纳答案by Pavel Minaev
Your sample code:
您的示例代码:
myvalue = elem.SelectSingleNode("Type/text()").Value;
is where the problem is. The XPath expression you've used there doesn't mean "give me text of element Type
". It means "give me all child text nodes of element Type". And an empty element doesn't have any child text nodes (a text node cannot be empty in XPath document model). If you want to get text value of the node, you should use:
问题出在哪里。您在那里使用的 XPath 表达式并不意味着“给我元素的文本Type
”。这意味着“给我元素类型的所有子文本节点”。并且空元素没有任何子文本节点(XPath 文档模型中的文本节点不能为空)。如果要获取节点的文本值,应使用:
myvalue = elem.SelectSingleNode("Type").InnerText;
回答by Andrew Hare
The recommended approach would be to use .NET's new XML API (namely LINQ to XML).
推荐的方法是使用 .NET 的新 XML API(即 LINQ to XML)。
Here is an example:
下面是一个例子:
using System;
using System.Linq;
using System.Xml.Linq;
class Program
{
static void Main()
{
String xml = @"<Root><Value></Value></Root>";
var elements = XDocument.Parse(xml)
.Descendants("Value")
.Select(e => e.Value);
}
}
回答by tessa
Maybe this will work for you:
也许这对你有用:
string s = elem.SelectSingleNode("somepath") != null ? elem.SelectSingleNode("somepath").value : ""
回答by Paige Watson
http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.value(VS.71).aspx
http://msdn.microsoft.com/en-us/library/system.xml.xmlnode.value(VS.71).aspx
Because the "value" returned depends on the NodeType, there is a chance that the node will be interpreted as a type that can return NULL.
因为返回的“值”取决于 NodeType,所以节点可能会被解释为可以返回 NULL 的类型。
You might be better off using:
您最好使用:
XmlElement elem = ....
string s = elem.SelectSingleNode("somepath").InnerText;
as XMLNode.InnerText(or XmlNode.InnerXML) will return a string, including an empty string.
因为XMLNode.InnerText(或XmlNode.InnerXML)将返回一个字符串,包括一个空字符串。
回答by Thorarin
When I'm actually bothering with XML DOM, you could write a helper method along the lines of:
当我真正为 XML DOM 烦恼时,您可以按照以下方式编写辅助方法:
static string NodeValue(XmlNode node, string defaultValue)
{
if (node != null)
return node.Value ?? defaultValue;
return defaultValue;
}
Then you can do the following if you're not sure your node will exist:
如果您不确定您的节点是否存在,那么您可以执行以下操作:
string s = NodeValue(elem.SelectSingleNode("Type"), String.Empty);
If keeps your code readable, especially if you're doing this for multiple elements.
如果保持您的代码可读,尤其是当您为多个元素执行此操作时。
All that being said, SelectSingleNode(..) does notreturn a null value if the tag is empty. The Valueattribute will be null however. If you're just trying to work around that, this should do:
所有这一切是说,的SelectSingleNode(..)不不,如果标签为空,返回空值。但是,Value属性将为 null。如果你只是想解决这个问题,应该这样做:
string s = elem.SelectSingleNode("Type").Value ?? String.Empty;
Edit:ah, you're using /text() to select the actual text node. You could just get rid of that part of the XPath, but the NodeValue method I supplied should still work (the "?? defaultValue" part is not needed in that case though).
编辑:啊,您正在使用 /text() 来选择实际的文本节点。您可以去掉 XPath 的那部分,但是我提供的 NodeValue 方法应该仍然有效(尽管在这种情况下不需要“?? defaultValue”部分)。