【问题标题】:Replace string.empty and Show "Null" using linq to xml?使用linq to xml替换string.empty并显示“Null”?
【发布时间】:2012-09-14 09:55:41
【问题描述】:

我需要显示 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>

这是我的代码。,

var Price1 = xd.Descendants(ns + "ListPrice").Select(c => new
{
    PPrice = (c.Element(ns + "FormattedPrice") != null) ? 
             c.Element(ns + "FormattedPrice").Value : **string.Empty**
}).ToList();

如何将 String.Empty 替换为“Null”或 0 等值。提前致谢。 如果“FormattedPrice”不适用于项目二,则从 Xml 响应中,它应该 在列表中显示或为空。

【问题讨论】:

  • 哪种情况为空,哪种情况为0?
  • @CuongLe 如果条件失败,这里显示 string.empty。而不是我必须显示任何文本,它可能是“Null”或数字“0”。
  • 但显示错误“没有隐式转换 b/w 字符串和 int。
  • 奇怪了,再试一下"0"

标签: linq linq-to-xml


【解决方案1】:

不清楚你为什么在这里使用匿名类型,如果你使用显式的string 转换,你也可以避免条件运算符。当然,您可以轻松地将 string.Empty 更改为 "null" 并且 真的 应该可以正常工作 - 如果不是,那么还有其他问题。

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

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

您可能不需要 (string) c.Element(ns + "FormattedPrice") 周围的括号 - 我不记得在强制转换或空合并运算符中具有更高优先级的内容。

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

var price = (string) item.Element(ns + "ListPrice").Element(ns + "Price") ?? "0";

获取价格列表并在ListPrice 不存在时以某种方式推断在何处插入“0”将需要您找到Items,例如

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

只是是价格,而不是其他商品数据,这很奇怪。

【讨论】:

  • @Thirisangu:除了将FormattedPrice 更改为Price 并将"Null" 更改为"0" 之外,目前尚不清楚您认为我的回答是否行不通。
  • @Thirisangu:您的 XML 不包含 FormattedPrice anywhere,并且您写道:“如果“价格”不适用于项目二,则来自 Xml 响应”。如果你一直前后矛盾,就不可能给你一个好的答案。如果ListPrice 根本不可用,您从未提及您想要发生的事情。请注意,我的代码不会返回 null - 它会返回一个空列表,这不是一回事。此外,如果ListPrice 不可用,这与大多数问题所讨论的情况完全不同。
  • 那个错误是我的。我没有给你足够的信息。但是你的代码是正确的。现在我变了。谢谢
  • @Thirisangu:我在编辑中添加了更多选项,但请注意在未来提出正确且一致的信息的问题。在tinyurl.com/so-hints 阅读我的提问指南
  • .,它在第二行中显示为,“不包含'select'的定义,并且找不到接受第一个参数类型linq.xelement的扩展方法'select'。
【解决方案2】:

这是上述问题的解决方案。

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();

【讨论】:

    猜你喜欢
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多