I have a xml file I'm querying that looks like this:
<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>
Basically I have certain attributes I'm looking for in each product. I need attributes within TYPE_PRD_RANGE
, SUB_RANGE
and ITEM
and I would like to nest them all in a text file. Currently I'm querying all three elements separately and writing to different txt files and then manually figuring out which TYPE_PRD_RANGE
links to SUB_RANGE
links to list of ITEM
. But this is tedious, some values aren't entered to the system properly and prone to human error.
Current code:
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`
Ideally, I'd like the following output in text format:
"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"
Any help whatsoever is appreciated.
Use String Join
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();
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.