我尝试使用XML Reader,Xpath ......并且知道linq 但是找不到解决这些问题的方法。
我必须提取信息,将每个订单分成一行,在这一行中应该是第一个元素的信息,以及物品和订单以及订单的状态......
有没有办法在一个linq查询中将所有这些信息提取到一行?或者我是否必须为此构建步骤?
(Visualstudio 2010/2013 C#.Net 4)
<Account>
<Name>Name1</Name>
<InId>100</InId>
<CustomID>100000087</CustomID>
<ZipCode>zipcode</ZipCode>
<Items>
<Item>
<ItemID>700</ItemID>
<ItemName>Itemname1</ItemName>
<Orders>
<Order>
<IDIndex>1000</IDIndex>
<IDParam>T1</IDParam>
<Themes>
<Theme>
<Status>Alert</Status>
<Lastget>01.01.2015</Lastget>
</Theme>
</Themes>
</Order>
</Orders>
<Item>
<ItemID>800</ItemID>
<ItemName>Itemname2</ItemName>
<Orders>
<Order>
<IDIndex>5001</IDIndex>
<IDParam>T1</IDParam>
<Themes>
<Theme>
<Status>Alert1</Status>
<Lastget>01.01.2015</Lastget>
</Theme>
</Themes>
</Order>
<Order>
<IDIndex>5002</IDIndex>
<IDParam>T1</IDParam>
<Themes>
<Theme>
<Status>Alert1</Status>
<Lastget>01.01.2015</Lastget>
</Theme>
</Themes>
</Order>
<Order>
<IDIndex>5003</IDIndex>
<IDParam>T1</IDParam>
<Themes>
<Theme>
<Status>Alert2</Status>
<Lastget>01.01.2015</Lastget>
</Theme>
</Themes>
</Order>
</Orders>
</Item>
</Items>
</Account>
答案 0 :(得分:1)
以下查询将为您提供所需的数据: -
var result = xdoc.Root.Descendants("Item")
.Select(x => new
{
Name = (string)x.Document.Root.Element("Name"),
InId = (string)x.Document.Root.Element("InId"),
CustomID = (string)x.Document.Root.Element("CustomID"),
ItemID = (string)x.Element("ItemID"),
ItemName = (string)x.Element("ItemName"),
OrdersList = x.Descendants("Order")
.Select(y => new
{
IDIndex = (string)y.Element("IDIndex"),
IDParam = (string)y.Element("IDParam"),
ThemesList = y.Descendants("Theme")
.Select(z => new
{
Status = (string)z.Element("Status"),
Lastget = (string)z.Element("Lastget")
}).ToList()
}).ToList()
});
请注意,我们会为2 items
创建两个列表,对于每个项目,我都会在orders
的每个订单列表中创建themes
列表。