c# - 使用 LINQ 在 XML 中查询具有相同名称的嵌套元素

标签 c# xml 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();

        }

    }

}

关于c# - 使用 LINQ 在 XML 中查询具有相同名称的嵌套元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31310901/

相关文章:

javascript - asp.net按钮点击不会通过jquery在隐藏字段中触发大量数据

c# - 我应该如何从自定义对象的 List<T> 中提取不同值的集合?

c# - linq to dataset 相当于 SQL LIKE 子句

c# - 不能隐式转换类型 - 泛型

c# - 通用表达式树,每个提供的属性都有 'OR' 子句

c# - 如何使用反射根据xml文件中的文本实例化一个新类

java - if 或语句 xml 和 java 控制台输出

c# - 检查通用 IEnumerable 是否为空

c# - 组合自定义 AuthorizeAttribute 和 RequireHttpsAttribute

android - 错误 :(27) No resource identifier found for attribute 'srcCompat' in package 'com.example.jaisonjoseph.newsclient'