【问题标题】:XDocument C# parse node value inside nodeXDocument C#解析节点内的节点值
【发布时间】:2019-03-22 15:04:02
【问题描述】:

我有一个基本上如下所示的 XML 文档:

<Item>
 <Seller>
   <UserID>SomeSeller</UserID>
   <FeedbackScore>2535</FeedbackScore>
 </Seller>
</Item>

现在我正在尝试解析文档,如下所示:

 var document = XDocument.Parse(upcList);
                    XNamespace ns = "urn:ebay:apis:eBLBaseComponents";
                    var result= document
                        .Descendants(ns + "Item")
                        .Select(item => new CustomClass
                        {
                            FeedBackScore = Convert.ToInt32(item.Descendants(ns+ "Seller")
                                            .Where(p=>p.Name.LocalName=="FeedbackScore").FirstOrDefault()),             

                            Sales = (int) item.Element(ns+"QuantitySold"),
                            Location = (string)item.Element(ns+"Location"),
                            CurrentPrice = (double)item.Element(ns + "CurrentPrice"),
                            Title = (string)item.Element(ns + "Title"),
                            ItemID = (string)item.Element(ns + "ItemID")

                        }).ToList();

请注意这部分我如何尝试解析 FeedbackScore 节点值:

   FeedBackScore = Convert.ToInt32(item.Descendants(ns+ "Seller")
                                            .Where(p=>p.Name.LocalName=="FeedbackScore").FirstOrDefault()),   

但是当我尝试解析它时,我将所有“FeedbackScore”节点值都设为“0”:(

谁能告诉我我做错了什么以及如何在这个节点“2535”中获取这个值?

【问题讨论】:

  • 你试过XNamespace ns = ""吗?
  • 所以,document.Descendants() 返回条目,对吗?只是那个会返回“0”吗?我说的对吗?
  • FirstOrDefault 返回什么类型?那不是 XElement 吗?你应该在哪个上面使用Value 属性?
  • 是的,那是因为转换。不确定“null”是否会转换为 0,但这是我的怀疑。但如果是这样,那就意味着找不到您的元素。
  • 我猜通过调用Descendants(ns+ "Seller") 你会得到Seller 节点,它们的名字永远不会匹配"FeedbackScore" 并产生null。然后Convert.ToIn32(null)yields 0。您应该直接使用 Descendants() 来查看“FeedBackScore”(类似 item.Descendants("FeedBackValue").FirstOrDefault() 的东西应该可以解决问题)。

标签: c# asp.net xml asp.net-mvc xml-parsing


【解决方案1】:
FeedBackScore = Convert.ToInt32(item.Descendants(ns + "FeedbackScore").Value)

【讨论】:

    【解决方案2】:

    您错误地检查了Seller 节点的名称而不是其子节点。通过这样做,FirstOrDefault() 将产生nullWhere() 的条件由于错误的节点而永远不会满足)并且Convert.ToIn32(null) 将产生0

    要解决此问题,您可以直接使用“FeedbackScore”节点并像这样转换其值

    FeedBackScore = Convert.ToInt32(item.Descendants("FeedBackValue").FirstOrDefault()?.Value),
    

    【讨论】:

      【解决方案3】:

      Descendants 这里将返回Seller 元素,然后您检查其中是否有任何名称为FeedbackScore。这是不可能的——他们不能同时拥有两个名字。

      假设您想要FeedbackScore 如果父级是Seller,您需要阅读ElementsSeller 元素。

      我还要注意,您可以像处理其他属性一样使用显式转换。

      把它们放在一起,这会起作用:

      FeedBackScore = (int) item.Elements(ns + "Seller")
          .Elements(ns + "FeedbackScore")
          .Single()
      

      如果此元素不总是存在,您可以默认为0

      FeedBackScore = (int?) item.Elements(ns + "Seller")
          .Elements(ns + "FeedbackScore")
          .SingleOrDefault() ?? 0;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多