c# - Accessing list in xml -
xml data structure:
<cpt xmlns="http://www.example.org/genericclientprofile" xmlns:ns2="http://www.bosch-si.com/finance/cts/cpt/2" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://www.example.org/genericclientprofile genericclientprofile.xsd"> <header> <serviceid>cpt-uk</serviceid> <versionid>1.0</versionid> <brandcode>rbs.ada</brandcode> <creationtime>2013-09-24t16:56:52.985+02:00</creationtime> </header> <clientprofile bpkey="19933" id="1bb26568-1df3-4206-8cea-fb4614bf0a6a" createdby="barbourt" lastmodifiedby="mannc" documentstructureversion="v3_2" lastmodifiedat="2013-09-23 15:40:49.873" createdat="2013-09-23 10:07:33.608"> <section xmlns="" id="cd21fbb5-da1b-485d-8909-a8392fd5ad5c" name="globalclientfacts"> <attribute name="familyanddependentcomment" type="string">tomas 18 in 2013</attribute> <list name="familyanddependents"> <entry entryid="1"> <attribute name="famdependage" type="decimal">0</attribute> <attribute name="famdependbirthdate" type="date" /> <attribute name="famdependfindep" type="boolean">false</attribute> <attribute name="famdependfindepuntil" type="string" /> <attribute name="famdependname" type="string">tomas</attribute> <attribute name="famdependnat" type="xmlcountry">gb</attribute> <attribute name="famdependrel" type="enumeration">son</attribute> <attribute name="famdependrelto" type="string" /> </entry> <entry entryid="2"></list> <attribute name="subcommentwills" type="string" /> </section>
i have following data structure, i'm looking create sql insert statements each "section" have managed do. if section contains list, need list name , attributes 1 of entries. seems double attributes there 2 entries.
c#:
public void findallnodes(xdocument doc, xnamespace ns) { stringbuilder attribute = new stringbuilder(); stringbuilder values = new stringbuilder(); values.append("("); var db = cptsqlentity(); foreach (var f in doc.descendants(ns + "clientprofile").first().elements()) { if (f.attribute("name") != null) { values.append(")"); attribute.append(")"); attribute.append(values); values.clear(); attribute.append(environment.newline); attribute.append("insert "); values.append(" values ("); oraclecommand oracleq = new oraclecommand("select table_short lookup table_long = " + "'" + f.attribute("name").value + "'" + " , rownum <=1", db); oracledatareader dr = oracleq.executereader(); dr.read(); attribute.append(dr.getstring(0)); attribute.append(" ("); } foreach (var p in f.descendants()) { if (p.attribute("name") != null) { attribute.append(p.attribute("name").value + ","); values.append("'" + p.value + "'" + ","); } } }
i'm not 100% sure schema xml file, wouldn't following do?
replace last foreach
statement following:
foreach (var p in f.elements("attribute") .union(f.elements("list") .selectmany(l => l.elements() .first() .elements("attribute")))) { //... }
this gives attribute
children of section
element attribute
children of first child of list
elements (which child of section
).
does make sense?
Comments
Post a Comment