c# - How should I organize two objects in order to be able to join them on a key? -
c# - How should I organize two objects in order to be able to join them on a key? -
so basically, reading in 2 xml docs. first has 2 values need stored: name , value. sec has 4 values: name, defaultvalue, type, , limit. when reading in docs, want store each object. need able combine 2 objects 1 has 5 values stored in it. xml docs different lengths, sec @ to the lowest degree size of first.
example:
<xml1> <item1> <name>cust_no</name> <value>10001</value> </item1> <item4> item4 name , value </item4> <item7> item 7 name , value </item7> </xml1> <xml2> <item1> <name>cust_no</name> <defaultvalue></defaultvalue> <type>varchar</type> <limit>15</limit> </item1> 6 more times items 2-7 </xml2>
i have code looping through xml. need thoughts on best way store info it. ultimately, want able bring together 2 objects on name key. tried string[]
, arraylist[]
, ran difficulty combining them. read on dictionary
, had problem implementing that, (i've never used dictionary
before).
here linq xml query, bring together 2 xdocuments , select anonymous objects joined items. each object have 5 properties:
var query = i1 in xdoc1.root.elements() bring together i2 in xdoc2.root.elements() on (string)i1.element("name") equals (string)i2.element("name") g allow j = g.singleordefault() // joined element sec file, if select new { name = g.key, value = (int)i1.element("value"), defaultvalue = (j == null) ? null : (string)j.element("defaultvalue"), type = (j == null) ? null : (string)j.element("type"), limit = (j == null) ? null : (string)j.element("limit") };
xdocuments created this:
var xdoc1 = xdocument.load(path_to_xml1); var xdoc2 = xdocument.load(path_to_xml2);
usage of query:
foreach(var item in query) { // utilize string item.name // integer item.value // string item.defaultvalue // string item.type // string item.limit }
c# xml
Comments
Post a Comment