Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
替换string.empty并显示";空";使用linq转换xml?_Linq_Linq To Xml - Fatal编程技术网

替换string.empty并显示";空";使用linq转换xml?

替换string.empty并显示";空";使用linq转换xml?,linq,linq-to-xml,Linq,Linq To Xml,我需要显示null或零,而不是显示空字符串 Xml响应: <Items> <Item> <ASIN>111</ASIN> <ItemAttributes> <Title>xxx</Title> <ListPrice> <Currency>USD</Currency> <Formatte

我需要显示null或零,而不是显示空字符串

Xml响应:

<Items>
   <Item>
     <ASIN>111</ASIN>
      <ItemAttributes>
       <Title>xxx</Title>
      <ListPrice>
        <Currency>USD</Currency>
         <FormattedPrice>45.25</FormattedPrice>
        </ListPrice>
        </ItemAttributes>
        <Variation>
        <Item>
         <ItemAttributes>
            <Title>yes</Title>
          </ItemAttributes>
        </Item>
        </Variation>
     </Item>
   <Item>
     <ASIN>222</ASIN>
      <ItemAttributes>
       <Title>yyy</Title>
       </ItemAttributes>
         <Variation>
        <Item>
         <ItemAttributes>
            <Title>No</Title>
          </ItemAttributes>
        </Item>
        </Variation>
    </Item>
   <Items>
如何用“Null”或0等值替换String.Empty。提前谢谢。 从Xml响应中,如果“FormattedPrice”不适用于第二项,则应
在列表中显示或为空

这里不清楚为什么要使用匿名类型,如果使用显式
字符串
转换,也可以避免使用条件运算符。当然,您可以很容易地将
string.Empty
更改为
的“null”
,这应该可以很好地工作-如果不是这样,那么还有其他问题

无论如何,下面是使用“Null”代替的简化代码(以及更传统的变量名):

有可能您不需要在
(string)c.Element(ns+“FormattedPrice”)
周围使用括号-我无法立即记住在强制转换或空合并运算符之外什么具有更高的优先级

编辑:要处理没有
ListPrice
元素的情况,如果您已经处于单个项目的级别,则可以使用:

var price = (string) item.Element(ns + "ListPrice").Element(ns + "Price") ?? "0";
当缺少
ListPrice
时,获取价格列表并以某种方式推断在何处插入“0”将要求您查找
项目,例如

var prices = xd.Descendants(ns + "Item")
               .Select(item => item.Elements("ListPrice")
                                   .Select(c => (string) c.Element(ns + "Price"))
                                   .FirstOrDefalut() ?? "0")
               .ToList();

不过,这只是价格,而不是其他项目数据,这将非常奇怪。

这是上述问题的解决方案

var Title = xd.Descendants(ns + "Items").Elements(ns + "Item").Select(DVDTitle =>DVDTitle.Elements(ns + "ItemAttributes").Select(DVDTitle1 => (string)DVDTitle1.Element(ns + "Title")).FirstOrDefault() ?? "Null").ToList();

哪种情况为空,哪种情况为0?@CuongLe如果条件失败,这里显示string.empty。相反,我必须显示任何文本,它可能是“Null”或数字“0”。但显示错误“无隐式转换b/w字符串和int。这很奇怪,请尝试再次将
“0”
@Thirisangu:除了更改
价格的
格式化价格
,以及
价格的
“Null”
“0”“
,不清楚您认为从我的回答中有什么不起作用。@Thirisangu:您的XML在任何地方都不包含FormattedPrice,您写道:“如果第二项的“价格”不可用,则从XML响应”。如果你总是前后矛盾,就不可能给你一个好的答案。您从未提到如果根本无法获得
ListPrice
您希望发生什么。请注意,我的代码不会返回null-它将返回一个空列表,这与此不同。另外,如果
ListPrice
不可用,这与大多数问题所讨论的情况完全不同。那是我的错误。我没有给你足够的信息。但是你的代码是正确的。现在我变了。Thanks@Thirisangu:我已在编辑中添加了更多选项,但请注意在将来提问时使用正确且一致的信息。请阅读我在的《提问指南》,它在第二行中显示为,“不包含'select'的定义,并且找不到接受第一个参数类型的扩展方法'select'。
var prices = xd.Descendants(ns + "Item")
               .Select(item => item.Elements("ListPrice")
                                   .Select(c => (string) c.Element(ns + "Price"))
                                   .FirstOrDefalut() ?? "0")
               .ToList();
var Title = xd.Descendants(ns + "Items").Elements(ns + "Item").Select(DVDTitle =>DVDTitle.Elements(ns + "ItemAttributes").Select(DVDTitle1 => (string)DVDTitle1.Element(ns + "Title")).FirstOrDefault() ?? "Null").ToList();