1
如果我有以下xml;Linq2XML创建对象模型
<productList>
<product>
<id>1</id>
<name>prod 1</name>
</product>
<product>
<id>2</id>
<name>prod 2</name>
</product>
<product>
<id>3</id>
<name>prod 3</name>
</product>
</productList>
如何使用Linq2XML创建对象heiarchy?
我试过这个;
var products = from xProducts in xDocument.Descendants("root").Elements("productList")
select new
{
product = from xProduct in xProducts.Elements("product")
select new
{
id = xProduct.Element("id").Value,
name = xProduct.Element("name").Value
}
}
但是这会产生一个错误,因为我认为product
正在多次声明。
我想结束一个这样的对象;
ProductList
List<product>
id
name
我不能有一个模型,这些将进入,所以我需要使用var。
编辑
如果我只得到说这个名字还是那么的ID代码工作。它只会失败,如果我试图获得这两个领域。
什么是错误? –
类型'<> f__AnonymousType0'同时存在于'MyApplication.dll'和'System.Web.dll' –
griegs