c# - Reading XML Nodes -
c# - Reading XML Nodes -
i have next xml file. trying read startdate node of dept element don't want read "startdate" node of of rest of kid elements "deptroles".
<dept operationalstatus="active" primaryrole="admin" depchangedate="20130420"> <startdate type="legal">20130401</startdate> <enddate type="legal"></enddate> <startdate type="operational">20130320</startdate> <enddate type="operational"></enddate> <deptroles> <deptrole name="other dept" status="active"> <startdate type="legal">20130401</startdate> <enddate type="legal"></enddate> <startdate type="operational">20130320</startdate> <enddate type="operational"/> <isprimary/> </deptrole> </deptroles> </dept>
this c# code. code getting deptrole startdate element, dont want.
public static list<organisation> getorgdata(string data) { list<organisation> listorgdata = new list<organisation>(); xmlreader xmlreader = xmlreader.create(new stringreader(data)); while (xmlreader.read()) { if (xmlreader.nodetype == xmlnodetype.element) { organisation record = new organisation(); if (xmlreader.hasattributes && xmlreader.name == "dept") { record.orgoperationalstatus = xmlreader.getattribute("operationalstatus"); record.orglegalstatus = xmlreader.getattribute("legalstatus"); } else if (xmlreader.name == "name") { record.orgname = xmlreader.readelementstring("name"); } else if (xmlreader.name == "startdate" || xmlreader.name == "enddate") { if (xmlreader.getattribute("type") == "legal") { record.orglegalstartdate = xmlreader.readelementstring("startdate"); record.orglegalenddate = xmlreader.readelementstring("enddate"); } else if (xmlreader.getattribute("type") == "operational") { record.orgoperationalstartdate = xmlreader.readelementstring("startdate"); record.orgoperationalenddate = xmlreader.readelementstring("enddate"); listorgdata.add(record); } } } } homecoming listorgdata; }
i utilize linq xml (as others have suggested). below sample code matches posted code - explanation of code follows.
public static list<organisation> getorgdata(string data) { list<organisation> listorgdata = new list<organisation>(); xdocument xdoc = xdocument.parse(data); listorgdata = xdoc.root.elements("dept") .select(x => new organisation() { orgoperationalstatus = (string)x.attribute("operationalstatus"), orglegalstartdate = (string)x.elements("startdate") .where(x1 => (string)x1.attribute("type") == "legal") .first(), orgoperationalstartdate = (string)x.elements("startdate") .where(x1 => (string)x1.attribute("type") == "operational") .first() }).tolist(); homecoming listorgdata; }
the first thing above code load xml xdocument xdocument.parse
method.
the query parses xml document , returns list of populated organization
objects. goes this.
<dept>
elements (and kid elements). with each <dept>
element, create new instance of organisation
, assign value of "operationalstatus" attribute operationalstatus
property of organisation
. (string)
cast gracefully handle situation specified attribute not present. for legal start date, query on <startdate>
children of <dept>
element, taking first 1 has attribute "type" equal "legal". for operational start date, similar query number 3, except it's looking "operational". convert ienumerabale<t>
result list calling .tolist()
extension method. this may not efficient, , doesn't cover properties have in original code, should @ to the lowest degree going in right direction. example, if wanted "primaryrole" attribute, you'd add together next line in new organisation()
block:
orgprimaryrole = (string)x.attribute("primaryrole"),
if need info <deptroles>
can to, it's little more involved.
if prefer query syntax on method syntax, this:
listorgdata = (from x in xdoc.root.elements("dept") select new { orgoperationalstatus = (string)x.attribute("operationalstatus"), orglegalstartdate = (from x1 in x.elements("startdate") (string)x1.attribute("type") == "legal" select (string)x1).first(), orgoperationalstartdate = (from x1 in x.elements("startdate") (string)x1.attribute("type") == "operational" select (string)x1).first() }).tolist();
c# asp.net xml linq-to-xml
Comments
Post a Comment