複数のアイテムを持つXMLファイルがあります。すべてをリストするのではなく、特定のリストを1つずつデシリアライズしたい。 this siteから例を使用してXMLから特定のアイテムのみをデシリアライズしてリストに追加
は、どのように私はId=2
のみProduct
をデシリアライズし、productList
に追加しますか?
クラス:
public class Product
{
public int Id { get; set; }
public string Name { get; set; }
}
コード:
void foo()
{
string xmlString = "<Products><Product><Id>1</Id><Name>My XML product</Name></Product><Product><Id>2</Id><Name>My second product</Name></Product></Products>";
XmlSerializer serializer = new XmlSerializer(typeof(List<Product>), new XmlRootAttribute("Products"));
StringReader stringReader = new StringReader(xmlString);
List<Product> productList = (List<Product>)serializer.Deserialize(stringReader);
}
LINQ to XMLなどのXMLパーサーを使用しないのはなぜですか? – Tim