gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:37:38 25 4
gpt4 key购买 nike

我有一个我正在查询的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com