もし私が次の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>
私はどのようにオブジェクトheiarchyを作成するのにLinq2XMLを使用しますか?
私はこれを試しました。
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