簡體   English   中英

使用LINQ在XML中查詢具有相同名稱的嵌套元素

[英]Querying nested elements with same name in XML with LINQ

我正在查詢的xml文件如下所示:

<Product ID="Sample A" UserTypeID="TYPE_PRD_RANGE">
  <MultiValues>
  <Value>
   <Value AttributeId = "Att_1">Value1</Value>
  </>Values>
  <MultiValues>
  <Values AttributeId = "Att_2">
    <Value AttributeId = "Att_3">Value1</Value>
    <Value AttributeId = "Att_4">Value2</Value>
    <Value AttributeId = "Att_5">Value3</Value>
    <Value AttributeId = "Att_6">Value4</Value>
  </Values>
  <Product ID="Sample A_1" UserTypeID="SUB_RANGE">
    <Values AttributeId = "Att_2_5">
      <Value AttributeId = "Att_2_4">Value1</Value>
      <Value AttributeId = "Att_2_3">Value2</Value>
      <Value AttributeId = "Att_2_1">Value3</Value>
      <Value AttributeId = "Att_2_2">Value4</Value>
    </Values>
  </Product>
  <Product ID="Sample A_1_1" UserTypeID="ITEM">
    <Values AttributeId = "12345">
      <Value AttributeId = "Att_2_1_1">Value1</Value>
      <Value AttributeId = "Att_2_2_1">Value2</Value>
      <Value AttributeId = "Att_2_3_1">Value3</Value>
      <Value AttributeId = "Att_2_4_1">Value4</Value>
    </Values>
  </Product>    
</Product>

基本上,我在每種產品中都有某些要尋找的屬性。 我需要TYPE_PRD_RANGESUB_RANGEITEM內的屬性,我想將它們全部嵌套在文本文件中。 目前,我正在分別查詢所有三個元素,並寫入不同的txt文件,然后手動找出哪些TYPE_PRD_RANGE鏈接到SUB_RANGE鏈接到ITEM列表。 但這很乏味,有些值未正確輸入到系統中,容易出現人為錯誤。

當前代碼:

   var rangedesiredIds = new[]  {//attributes needed from range here};

   var subRangeDesiredIds = new[]{//attributes needed from subrange here};

   var itemDesiredIds = new[]{//attributes needed from items here};         


    //Export all the main product ranges from here..........
   var rangeProducts = xml.Descendants("Product")
   .Where(x => (string)x.Attribute("UserTypeID") ==TYPE_PRD_RANGE);
    var rangeproductDetails = rangeProducts.Select(x => new
        {
            ID = (String)x.Attribute("ID"), 
            UserTypeID = (String)x.Attribute("UserTypeID"), 
            Values = x.Descendants("Value")
                      .Where(y=>rangedesiredIds.Contains(y.Attribute("AttributeID"))),
          MultiValue = x.Descendants("MultiValue")
                       .Where(y => rangedesiredIds.Contains(y.Attribute("AttributeID")))}

                );

//Rinse and repeat for `SUB_RANGE` and `ITEM`

理想情況下,我想要以下文本格式的輸出:

"range_Product ID" ,"range_Attribute 1.Value", 
    "subrange Product_ID", "subrange_Attribute2.Value" 
        Item Product_ID,  "item_Attribute_1.Value" ,"item_Attribute_2.Value"
        Item Product_ID,  "item_Attribute_1.Value" ,"item_Attribute_2.Value"

任何幫助表示贊賞。

使用字符串連接

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApplication34
{
    class Program
    {

        static void Main(string[] args)
        {
            string input =
                "<Root>" +
                "<Product ID=\"Sample A\" UserTypeID=\"TYPE_PRD_RANGE\">" +
                  "<MultiValues>" +
                  "<Values>" +
                   "<Value AttributeId = \"Att_1\">Value1</Value>" +
                  "</Values>" +
                  "</MultiValues>" +
                  "<Values AttributeId = \"Att_2\">" +
                    "<Value AttributeId = \"Att_3\">Value1</Value>" +
                    "<Value AttributeId = \"Att_4\">Value2</Value>" +
                    "<Value AttributeId = \"Att_5\">Value3</Value>" +
                    "<Value AttributeId = \"Att_6\">Value4</Value>" +
                  "</Values>" +
                  "<Product ID=\"Sample A_1\" UserTypeID=\"SUB_RANGE\">" +
                    "<Values AttributeId = \"Att_2_5\">" +
                      "<Value AttributeId = \"Att_2_4\">Value1</Value>" +
                      "<Value AttributeId = \"Att_2_3\">Value2</Value>" +
                      "<Value AttributeId = \"Att_2_1\">Value3</Value>" +
                      "<Value AttributeId = \"Att_2_2\">Value4</Value>" +
                    "</Values>" +
                  "</Product>" +
                  "<Product ID=\"Sample A_1_1\" UserTypeID=\"ITEM\">" +
                    "<Values AttributeId = \"12345\">" +
                      "<Value AttributeId = \"Att_2_1_1\">Value1</Value>" +
                      "<Value AttributeId = \"Att_2_2_1\">Value2</Value>" +
                      "<Value AttributeId = \"Att_2_3_1\">Value3</Value>" +
                      "<Value AttributeId = \"Att_2_4_1\">Value4</Value>" +
                    "</Values>" +
                  "</Product>" +
                "</Product>" +
                "</Root>";


            XElement products = XElement.Parse(input);

            var results = products.Elements("Product").Select(x => new
            {
                ID = x.Attribute("ID").Value,
                valueId = x.Descendants("Value").Attributes("AttributeId").FirstOrDefault().Value,
                products = x.Elements("Product").Select(y => new
                {
                    ID = y.Attribute("ID").Value,
                    Name = y.Attribute("UserTypeID").Value,
                    values = y.Element("Values").Elements("Value").Select(z => new
                    {
                        ID = z.Attribute("AttributeId").Value,
                        Value = (string)z
                    }).ToList()
                    .Select(aa =>  string.Join(",", new string[] { aa.ID, aa.Value })).ToList()
                }).ToList()
                .Select(ac => ac.values.Select(ad => string.Join(",", new string[] { ac.ID, ac.Name, ad }))).ToList()
            }).ToList()
            .Select(ae => ae.products.Select(af => af.Select(ag => string.Join(",", new string[] { ae.ID, ae.valueId, ag })).ToList()).ToList()).ToList();

        }

    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM