LINQ to XML Query无法按预期工作

时间:2017-04-22 12:46:41

标签: c# .net xml linq

我在Visual Studio 2015 C#.net中遇到LINQ to XML查询问题。我正在编写一个控制台应用程序。本着透明的精神,我是上述所有人的新手,所以我可能会有一个需要指出的简单错误。我花了几个小时阅读例子来实现这一目标。

XML数据集:

<inv-balance>
  <item ccard-charge-percent="3.8" color-code="51" description="g200 BLACK XL" gtin="00821780001021" price="$2.83" size-code="6" special-expiry="04/23/17" special-price="$1.97" style-code="g200">
    <whse code="CC">69666</whse>
    <whse code="KC">30613</whse>
    <whse code="GD">63661</whse>
    <whse code="PH">98920</whse>
    <whse code="TD">54505</whse>
    <whse code="FO">36127</whse>
    <whse code="MA">88852</whse>
    <whse code="CD">19212</whse>
    <whse code="CN">62769</whse>
    <whse code="WA">15232</whse>
    <whse code="D4">0</whse>
    <whse code="DB">0</whse>
    <whse code="DD">0</whse>
    <whse code="DL">0</whse>
    <whse code="DR">0</whse>
    <whse code="DU">0</whse>
    <whse code="DP">0</whse>
    <whse code="D2">0</whse>
    <whse code="DT">0</whse>
    <whse code="DK">0</whse>
    <whse code="DO">0</whse>
    <whse code="NC">0</whse>
    <whse code="DZ">0</whse>
    <whse code="DS">0</whse>
    <whse code="DI">0</whse>
    <whse code="DQ">0</whse>
    <whse code="D1">0</whse>
    <whse code="DE">0</whse>
    <whse code="DV">0</whse>
    <whse code="DN">0</whse>
    <whse code="D8">0</whse>
  </item>
</inv-balance>

我的代码:

XDocument rawxml = XDocument.Load(url);
var things = from r in rawxml.Descendants("inv-balance")
             select new
             {
                 price = r.Element("item").Attribute("price"),
                 saleprice = (string)r.Element("item").Attribute("special-price") ?? "none",
                 expdate = (string)r.Element("item").Attribute("special-expiry") ?? "none"
             };

var inv = (from whinv in rawxml.Descendants("item")
           select new
           {
               whse = whinv.Element("whse").Attribute("code").Value,
               qty = whinv.Element("whse").Value
           }).ToList();

foreach (var r in things)
{
    Console.WriteLine("Regular Price: " + r.price);
    if (r.saleprice != "none")
        Console.WriteLine("Sale Price: " + r.saleprice + " On sale through " + r.expdate);
    Console.WriteLine("Inventory Available:");
    foreach (var whinv in inv)
    {
        Console.WriteLine("Location: " + whinv.whse + " | " + whinv.qty);
    }
}

此图像是结果

enter image description here

我想要做的是从所有仓库中检索库存....最好是CC,KC,GD,PH,TD,FO,MA,CD,CN,WA,其他人永远不会有库存。

现在它只拉动第一个仓库代码。我尝试过没有骰子的attribue过滤。 XML没有格式错误,我在这里的粘贴由于某种原因很奇怪......它应该是whse code =&#34; xx&#34;。

我很欣赏有关我做错的任何建议。我最终的目标是将其写入数据库,但现在我可以逐步显示数据。

1 个答案:

答案 0 :(得分:0)

var codes = new List<string> { "CC", "KC", "GD", "PH", "TD", "FO", "MA", "CD", "CN", "WA" };

var inv = (from whinv in rawxml.Descendants("item").Elements("whse")
           where codes.Contains(whinv.Attribute("code").Value)
           select new
           {
               whse = whinv.Attribute("code").Value,
               qty = whinv.Value
           }).ToList();
相关问题