I have set of XML’s ( varies between 2 and 6) that needs to be processed(traversed and checked for certain data and relations within) – The XML’s have some “Recursive Data”
here is a simple example involving a test data for explanation – 2 files considered as example
File1.xml:
<some root------standard header not entered for the example----->
<parent>
<ID>AB-1234</ID>
<Description>Good book</Description>
<Date_Created>08-10-2011</Date_Created>
<child>
<ID>BC-0001</ID>
<Description>Nice</Description>
</child>
</parent>
<parent>
<ID>BC-0001</ID>
<Description>Work Together</Description>
<Date_Created>08-10-2011</Date_Created>
<child>
<ID>DC-0011</ID>
<Description>Happy</Description>
</child>
</parent>
File2.xml:
<some root------standard header not entered for the example----->
<parent>
<ID>DC-0011</ID>
<Description> book</Description>
<Date_Created>08-10-2011</Date_Created>
<child>
<ID>EF-0001</ID>
<Description>Nice</Description>
</child>
</parent>
<parent>
<ID>EF-0001</ID>
<Description>Work Together</Description>
<Date_Created>08-10-2011</Date_Created>
<child>
<ID>PQ-0011</ID>
<Description>Happy</Description>
</child>
</parent>
code I am using involves 1) loading both the XML files and combining them
XDocument test1doc = XDocument.Load(@"d:\File1.xml");
XDocument test2doc = XDocument.Load(@"d:\File2.xml");
IEnumerable<XElement> testElist1 = test1doc.decendants("parent");
IEnumerable<XElement> testElist2 = test2doc.decendants("parent");
IEnumerable<XElement> testElistcombo = testElist1.union(testElist2);
2) use the testElistcombo to navigate the elements using foreach – 2 foreach loops (one for the parent and second for the child)
3) while traversing use an if condition to check whether parent ID and Child ID are equal.
I am able to build the hierarchy – no problem with that.
I was able to print the hierarchy along with the level value of the hierarchy.by including a counter in each of the foreach loops.
my output looks like
AB-1234[level-0]
>>BC-0001[level-1]
>>DC-0011[level-3]
..... and so on.
as i said no problem with that. –
Following is the area where i would like some help:
1) when the number of files increases to more than 2 to a max 6, i am using a union in the following manner
XDocument test1doc = XDocument.Load(@"d:\File1.xml");
XDocument test2doc = XDocument.Load(@"d:\File2.xml");
XDocument test3doc = XDocument.Load(@"d:\File3.xml");
XDocument test4doc = XDocument.Load(@"d:\File4.xml");
XDocument test5doc = XDocument.Load(@"d:\File5.xml");
XDocument test6doc = XDocument.Load(@"d:\File6.xml");
IEnumerable<XElement> testElist1 = test1doc.decendants("parent");
IEnumerable<XElement> testElist2 = test2doc.decendants("parent");
IEnumerable<XElement> testElist3 = test3doc.decendants("parent");
IEnumerable<XElement> testElist4 = test4doc.decendants("parent");
IEnumerable<XElement> testElist5 = test5doc.decendants("parent");
IEnumerable<XElement> testElist6 = test6doc.decendants("parent");
IEnumerable<XElement> testElistcombo1 = testElist1.union(testElist2);
IEnumerable<XElement> testElistcombo2 = testElistcombo1.union(testElist3);
IEnumerable<XElement> testElistcombo3 = testElistcombo2.union(testElist4);
IEnumerable<XElement> testElistcombo4 = testElistcombo3.union(testElist5);
IEnumerable<XElement> testElistcombo5 = testElistcombo4.union(testElist6);
and use the testElistcombo5.for processing.
help required: an alternative way to load and combine the XML’s to for processing.
2) The process is resource intensive and take a fair bit of time to complete the hierarchy building
help required: is there an alternative way to process the xml’s for building hierarchy in Recursive Data.
Question 1: you can do this using the Enumerable.Aggregate function to aggregate the elements for each document into one set of elements: