[英]Avoid object null reference exception when accessing sub-elements that may or may not exist (JSON)
[英]Avoid object null reference exception when accessing sub-elements that may or may not exist
我有:带有一些元素的XML。 可以在此XML中定义的子元素,也可以不在此XML中定义。 需要在子元素存在时提取子元素的值。
如何在不抛出对象引用错误的情况下获取值?
例如:
string sampleXML = "<Root><Tag1>tag1value</Tag1></Root>";
//Pass in <Tag2> and the code works:
//string sampleXML = "<Root><Tag1>tag1value</Tag1><Tag2>tag2Value</Tag2></Root>";
XDocument sampleDoc = XDocument.Parse(sampleXML);
//Below code is in another method, the 'sampleDoc' is passed in. I am hoping to change only this code
XElement sampleEl = sampleDoc.Root;
string tag1 = String.IsNullOrEmpty(sampleEl.Element("Tag1").Value) ? "" : sampleEl.Element("Tag1").Value;
//NullReferenceException:
//Object reference not set to an instance of an object.
string tag2 = String.IsNullOrEmpty(sampleEl.Element("Tag2").Value) ? "" : sampleEl.Element("Tag2").Value;
您可以使用null-coalescing-operator作为快捷方式:
string tag1= (string)sampleEl.Element("Tag1") ?? string.Empty;
这也使用LINQ to XML允许转换操作获取元素的值(在这种情况下转换为字符串),但如果元素不存在则返回null
。
您需要检查null
以防止它们。 鉴于您正在使用的重复模式,我只会将其分解为扩展方法。
public static string GetElementValue(this XElement parent, string elementName) {
if (parent == null) {
return string.Empty;
}
var element = parent.Element(elementName);
if (element == null || element.Value == null) {
return string.Empty;
}
return element.Value;
}
现在您的上述代码可以替换为以下代码
string tag1 = sampleEl.GetElementValue("Tag1");
string tag2 = sampleEl.GetElementValue("Tag2");
C#三元运算符非常适合这个:
string tag2 = sampleEl.Element("Tag2") == null ? "" : sampleEl.Element("Tag2").Value;
首先,你应该检查文档是否为null,记住你正在访问.Value,这将抛出一个空引用异常,所以在你申请之前.value做一个测试:
if (sampleEl != null)
//now apply .value
或者三元:
string tag2 = sampleEl.Element("Tag2") != null ? sampleEL.Element("Tag2").Value : String.Empty
然后你的代码变成:
string sampleXML = "<Root><Tag1>tag1value</Tag1></Root>";
//Pass in <Tag2> and the code works:
//string sampleXML = "<Root><Tag1>tag1value</Tag1><Tag2>tag2Value</Tag2></Root>";
XDocument sampleDoc = XDocument.Parse(sampleXML);
//Below code is in another method, the 'sampleDoc' is passed in. I am hoping to change only this code
XElement sampleEl = sampleDoc.Root;
string tag1 = sampleEl.Element("Tag1") != null ? sampleEl.Element("Tag1").Value : String.Empty;
//NullReferenceException:
//Object reference not set to an instance of an object.
string tag2 = sampleEl.Element("Tag2") != null ? sampleEL.Element("Tag2").Value : String.Empty
C#6.0允许我们使表达式更短更简单:
string uniqueIdentifier = myNode.Document?.Element("elem1")?.Element("elem2")?.Attribute("attribute1")?.Value;
string tag1 = sampleEl.Element("Tag1") != null ? sampleEl.Element("Tag1").Value : string.Empty;
我提出了这种扩展方法。 它要求您指定要作为lambda访问的属性,以及当链中的实际值或任何内容为null时使用的默认值:
public static TOut ValueOrDefault<TIn, TOut>(this TIn input, Func<TIn, TOut> projection, TOut defaultValue)
where TOut : class
{
try
{
return projection(input) ?? defaultValue;
}
catch (NullReferenceException)
{
return defaultValue;
}
catch (InvalidOperationException)
{
return defaultValue;
}
}
用法:
var value = topObject.ValueOrDefault(x=>x.ChildObject.AnotherChild.ChildProperty, String.Empty);
如果topObject,ChildObject,AnotherChild或ChildProperty为null,则value
将为空字符串。 如果所有这些都是有效引用,则返回将是ChildProperty实际上的任何内容(它仍然可以是空字符串)。 NullReferenceException的catch处理空引用的子成员的引用。 对于可空类型,在访问null可空类型的Value属性时抛出InvalidOperationException。
只是为了它的乐趣,这是一个使用LINQ to XML的解决方案
不需要三元运算符,因此您不需要两次指定标记的名称:
string tag1 = sampleEl.Elements("Tag1").Select(x => x.Value).FirstOrDefault(); string tag2 = sampleEl.Elements("Tag2").Select(x => x.Value).FirstOrDefault();
添加?? ""
?? ""
如果你想要空字符串而不是null
如果标签不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.