Wednesday 15 April 2015

c# - Extract value from xml file, simple example -



c# - Extract value from xml file, simple example -

structure of xml file

<?xml version="1.0" encoding="utf-8"?> <itemstoprocess xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <item>somevalue1</item> <item>somevalue2</item> </itemstoprocess>

i tried extract values this

xelement elem = xelement.load(filename); var items = c in elem.descendants("itemstoprocess") select new itemtoprocess { itemvalue = c.element("item").value; }; homecoming items;

but i'm missing something. i'm doing wrong?

you have xml namespace need specify.and if need item elements can utilize :

xnamespace ns = "http://www.w3.org/2001/xmlschema-instance"; var items = elem.descendants(ns + "item") .select(x => new itemtoproces { itemvalue = x.value });

see this find more info xml namespaces.

c# .net xml

No comments:

Post a Comment