How do I parse text from complex type xml in c# using XDocument? -
<?xml version="1.0" encoding="utf-8"?> <meta> <field type="xs-string" name="assetid">tf00000002</field> <field type="xs-string" name="title">titleofasset</field> </meta>
i have xml loaded in xdocument using function
xdocument doc = xdocument.parse(xmldata)
however, want able retrieve text fields "tf00000002" , "titleofasset" ... how go doing this?
templatemetadata.assetid = doc .descendants() .where(p => p.name.localname == "assetid") .tostring();
returns:
system.linq.enumerable+whereenumerableiterator`1[system.xml.linq.xelement]
can shine light on this?
in query, calling tostring
on ienumerable<xelement>
never give expected result, instead field
elements under root
, value:
var values = doc.root .elements("field") .select(element => (string)element);
if want access values using name
attribute can use dictionary
:
var values = doc.root .elements("field") .todictionary(x => (string)x.attribute("name"), x => (string)x);
then can access value of assetid
:
var id = values["assetid"];
Comments
Post a Comment