获取 XML 个节点属性并设置为 List<myType> 的属性?
Get XML nodes attributes and set as properties of List<myType>?
示例XML:
<Root>
<Product value="Candy">
<Item value="Gum" price="1.00"/>
<Item value="Mints" price="0.50"/>
</Product>
</Root>
假设我有一个 class 属性:
public class CandyItems
{
public string Value{get; set;}
public string Price{get; set;}
}
在我的主程序中 class,我有一个列表:
var Candies = new List<CandyItems>;
我正在努力寻找一种使用 LINQ 来填充 Candies
列表的简洁方法。
我可以像这样分步进行:
//Get list of Items within <Product value="Candy">
XElement tempCandies = XDocument.Load("file.xml").Root.Elements("Product").Single(c => c.Attributes("value") == "Candy").Descendants("Item");
//Loop through the elements
foreach(var item in tempCandies){
Candies.Add(new CandyItems{Value = item.Attributes("value"), Price = item.Attributes("price")});
}
但似乎我可以以某种方式使用纯 LINQ 更简洁地完成此操作。或者有其他推荐的方法吗?
这样的事情怎么样(加载文档后):
var candies =
xdoc.Root.Elements("Product")
.Where(p => p.Attribute("value").Value == "Candy")
.SelectMany(p => p.Descendants("Item").Select(i => new CandyItems {
Value = i.Attribute("value").Value,
Price = i.Attribute("price").Value }));
注意:省略所有错误处理。
试试这个:-
XDocument xdoc = XDocument.Load(@"Path\Candies.xml");
List<CandyItems> Candies = xdoc.Descendants("Item")
.Select(x => new CandyItems
{
Value = (string)x.Attribute("value"),
Price = (string)x.Attribute("price")
}).ToList();
尽管您没有提到,但如果您只想获取糖果,那么您的 XML 可能还包含其他产品,例如:-
<Root>
<Product value="Candy">
<Item value="Gum" price="1.00"/>
<Item value="Mints" price="0.50"/>
</Product>
<Product value="Chocolate">
<Item value="MilkChocolate" price="7.00"/>
<Item value="DarkChocolate" price="10.50"/>
</Product>
</Root>
然后您可以应用过滤器以仅获取 Candy
产品,如下所示:-
List<CandyItems> Candies = xdoc.Descendants("Item")
.Where(x => (string)x.Parent.Attribute("value") == "Candy")
.Select(x => new CandyItems
{
Value = (string)x.Attribute("value"),
Price = (string)x.Attribute("price")
}).ToList();
示例XML:
<Root>
<Product value="Candy">
<Item value="Gum" price="1.00"/>
<Item value="Mints" price="0.50"/>
</Product>
</Root>
假设我有一个 class 属性:
public class CandyItems
{
public string Value{get; set;}
public string Price{get; set;}
}
在我的主程序中 class,我有一个列表:
var Candies = new List<CandyItems>;
我正在努力寻找一种使用 LINQ 来填充 Candies
列表的简洁方法。
我可以像这样分步进行:
//Get list of Items within <Product value="Candy">
XElement tempCandies = XDocument.Load("file.xml").Root.Elements("Product").Single(c => c.Attributes("value") == "Candy").Descendants("Item");
//Loop through the elements
foreach(var item in tempCandies){
Candies.Add(new CandyItems{Value = item.Attributes("value"), Price = item.Attributes("price")});
}
但似乎我可以以某种方式使用纯 LINQ 更简洁地完成此操作。或者有其他推荐的方法吗?
这样的事情怎么样(加载文档后):
var candies =
xdoc.Root.Elements("Product")
.Where(p => p.Attribute("value").Value == "Candy")
.SelectMany(p => p.Descendants("Item").Select(i => new CandyItems {
Value = i.Attribute("value").Value,
Price = i.Attribute("price").Value }));
注意:省略所有错误处理。
试试这个:-
XDocument xdoc = XDocument.Load(@"Path\Candies.xml");
List<CandyItems> Candies = xdoc.Descendants("Item")
.Select(x => new CandyItems
{
Value = (string)x.Attribute("value"),
Price = (string)x.Attribute("price")
}).ToList();
尽管您没有提到,但如果您只想获取糖果,那么您的 XML 可能还包含其他产品,例如:-
<Root>
<Product value="Candy">
<Item value="Gum" price="1.00"/>
<Item value="Mints" price="0.50"/>
</Product>
<Product value="Chocolate">
<Item value="MilkChocolate" price="7.00"/>
<Item value="DarkChocolate" price="10.50"/>
</Product>
</Root>
然后您可以应用过滤器以仅获取 Candy
产品,如下所示:-
List<CandyItems> Candies = xdoc.Descendants("Item")
.Where(x => (string)x.Parent.Attribute("value") == "Candy")
.Select(x => new CandyItems
{
Value = (string)x.Attribute("value"),
Price = (string)x.Attribute("price")
}).ToList();