问题描述
我想使用LINQ查询一些xml文件。有一些要求和一些可选元素。唯一需要的是名字 - 一切是可选的。
。如果有一些空例如cageCode = NULL - 它不选择任何东西 - 我需要添加到字符串列表 - - 我试了一下象下面,但它不工作。当我把一切都充满正常工作的时候,有一些空它不保存到列表什么。
你能帮助我如何设置,列出哪里是空的元素?
谢谢!
I would like to query with linq some xml file. There are some required and some optional elements. Only required is name - everything else is optional.If there is some NULL for example cageCode = NULL - it doesnt select anything - I need to add to List of strings - "" - I tried it like below, but it doesnt work. When I have everything filled it works fine, when there is something NULL it doesnt save to list anything.Could you help me how to set "" to list where is null element?Thanks!
var queryManufacturer = from dataManufaturer in input.Identification.Manufacturers.Manufacturer
select
new
{
dataManufaturer.name,
dataManufaturer.cageCode,
dataManufaturer.FaxNumber,
dataManufaturer.URL.OriginalString
};
foreach (var a in queryManufacturer)
{
data.Add(a.name);
if (a.cageCode == null) data.Add("");
else data.Add(a.cageCode);
if (a.FaxNumber == null) data.Add("");
else data.Add(a.FaxNumber);
if (a.OriginalString == null) data.Add("");
else data.Add(a.OriginalString);
}
这引发了我一个空的异常,如果是一些在XML文件中的元素缺失 - 我不想得到这个例外 - 我想只是添加缺少的元素旁边空字符串
It throws me a null exception if is some of elements in xml file missing - I dont wanna get this exception - I would like just add empty string beside missing element
推荐答案
在您的LINQ试试这个以XML查询
try this in your Linq to XML query:
select new
{
name = dataManufaturer.name ?? "",
cageCode = dataManufaturer.cageCode ?? "",
FaxNumber = dataManufaturer.FaxNumber ?? "",
OriginalString = dataManufaturer.URL!=null ? dataManufaturer.URL.OriginalString : ""
};
这篇关于LINQ查询和空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!