xml serialization - XMl deserializer in C# -
i have following xml, , want deserialize streams of product1, syntax in c#? thanks. couldn't find documentations online.
<arrayofproductdata> - <productdata> <productname>product1</productname> <productid>1</productid> - <streams> <productid>1</productid> <name>current stream</name> <id>1</id> </streams> - <streams> <productid>1</productid> <name>stream 1.1</name> <id>2</id> </streams> </productdata> - <productdata> <productname>product2</productname> <productid>2</productid> - <streams> <productid>2</productid> <name>current stream</name> <id>1</id> </streams> - <streams> <productid>2</productid> <name>stream 1.2</name> <id>2</id> </streams> </productdata> </arrayofproductdata>
you use xdocument , xpath filter:
using system; using system.linq; using system.xml.linq; using system.xml.xpath; public class productstream { public int id { get; set; } public int productid { get; set; } public string name { get; set; } } class program { static void main() { var streams = xdocument .load("test.xml") .xpathselectelements("//productdata[productid='1']/streams") .select(s => new productstream { id = int.parse(s.element("id").value), productid = int.parse(s.element("productid").value), name = s.element("name").value }); foreach (var stream in streams) { console.writeline(stream.name); } } }
Comments
Post a Comment