c# - how can i filter the Linq results to only return selected XML column -
just want ask on how can filter linq results return selected xml column, per xml below want return value of entityid
, entityname
, ya
, reasontext
.
<tasubmittedfiledt> <submissiontype>ecitesvw</submissiontype> <entityid>201413671m</entityid> <entitytype>6</entitytype> <entityname>epic</entityname> <ya>2018</ya> <filename>2018.xml</filename> <reason>0</reason> <reasontext>successful</reasontext> <totaleci>132961837365</totaleci> <revenue>3559940928276</revenue> </tasubmittedfiledt>
below code:
xdocument doc = xdocument.load(xmlfilefullname); //code added reuel //var tasubmittedfiledt = doc.root.elements("{http://tempuri.org/batchds.xsd}tasubmittedfiledt").tolist(); //xelement root = xelement.load(xmlfilefullname); ienumerable<xelement> tasubmittedfiledt = p in doc.root.elements() select p; foreach (xelement e in tasubmittedfiledt) console.writeline(e); console.writeline("got here!!!"); console.readline();
add where
clause check element's name:
hashset<string> names = new hashset<string>(new string[] { "entityid", "entityname", "ya", "reasontext" }); var result = xdocument.load("data.xml").root.descendants() .where(element => names.contains(element.name.localname)).tolist();
or in query syntax:
var result = element in xdocument.load("data.xml").root.descendants() names.contains(element.name.localname) select element;
node if xml contains several tasubmittedfiledt
elements you'd need along:
var result = xdocument.load("data.xml").descendants("tasubmittedfiledt") .select(element => element.descendants() .where(element => names.contains(element.name.localname))) .tolist();
Comments
Post a Comment