Linking 3 XML, grouping 3 XML

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi Linq to XML experts,
I have what I think is a fairly simple request - to link a ListView to some XML output that happens to exist in 3 separate XML files. Ive been trying to use LINQ to XML to get these joined and grouped together and output to the ListView. Here
are the 3 example XML files:
XML 1:
<wantedlist>
<br/>
<wanted> <nameid>1</nameid>
<br/>
<name>NAME1</name>
<br/>
<age>38</age> <height>6 ft. 00 in.</height>
<br/>
<weight>180</weight>
<br/>
<hair>BLK</hair> <eyes>BRO</eyes>
<br/>
<image>NAME1.JPG</image> </wanted>
<br/>

<br/>
<wanted> <nameid>2</nameid>
<br/>
<name>NAME2</name>
<br/>
<age>34</age> <height>6 ft. 00 in.</height>
<br/>
<weight>200</weight>
<br/>
<hair>BLN</hair> <eyes>BLU</eyes>
<br/>
<image>NAME2.JPG</image> </wanted>
<br/>

<br/>
<wanted> <nameid>3</nameid>
<br/>
<name>NAME3</name>
<br/>
<age>44</age> <height>5 ft. 10 in.</height>
<br/>
<weight>230</weight>
<br/>
<hair>BLK</hair> <eyes>BRO</eyes>
<br/>
<image>NAME3.JPG</image> </wanted>
<br/>

<br/>
<wanted> <nameid>4</nameid>
<br/>
<name>NAME4 </name>
<br/>
<age>37</age> <height>5 ft. 04 in.</height>
<br/>
<weight>150</weight>
<br/>
<hair>BRO</hair> <eyes>BRO</eyes>
<br/>
<image>NAME4.JPG</image> </wanted>
<br/>
</wantedlist>
XML 2:
<aliaslist>
<br/>
<alias> <nameid>1</nameid>
<br/>
<a_name>BAD GUY 1 </a_name> </alias>
<br/>

<br/>
<alias> <nameid>2</nameid>
<br/>
<a_name>BAD GUY 2 </a_name> </alias>
<br/>

<br/>
<alias> <nameid>2</nameid>
<br/>
<a_name>REALLY BAD GUY </a_name> </alias>
<br/>

<br/>
<alias> <nameid>2</nameid>
<br/>
<a_name>SERIOUSLY BAD GUY </a_name> </alias>
<br/>

<br/>
<alias> <nameid>3</nameid>
<br/>
<a_name>BAD GUY 3 </a_name> </alias>
<br/>

<br/>
<alias> <nameid>3</nameid>
<br/>
<a_name>PRETTY MEAN GUY </a_name> </alias> <br/>
</aliaslist>
XML 3:
<chargelist>
<br/>
<charges> <nameid>1</nameid>
<br/>
<charge>JAYWALKING 1</charge>
<br/>
<bail>$10,000</bail> </charges>
<br/>

<br/>
<charges> <nameid>2</nameid>
<br/>
<charge>LITTER 2</charge>
<br/>
<bail>$25,000</bail> </charges>
<br/>

<br/>
<charges> <nameid>2</nameid>
<br/>
<charge>GUM CHEWING</charge>
<br/>
<bail>$500,000</bail> </charges>
<br/>

<br/>
<charges> <nameid>2</nameid>
<br/>
<charge>INFRACTION 1</charge> <br/>
<bail>$5,000</bail> </charges>
<br/>

<br/>
<charges> <nameid>3</nameid>
<br/>
<charge>JAYWALKING 2</charge>
<br/>
<bail>$100,000</bail> </charges>
<br/>

<br/>
<charges> <nameid>3</nameid>
<br/>
<charge>NAME CALLING</charge>
<br/>
<bail>$2,500</bail> </charges>
<br/>

<br/>
<charges> <nameid>4</nameid>
<br/>
<charge>INFRACTION 3</charge>
<br/>
<bail>$5,000</bail> </charges><br/>
</chargelist>
I have been using C# in VS 2010 and some of the following code:<span style="font-size:small <span lang="
<span style="font-size:small chrgs = <span style="color:#2b91af; font-size:small <span style="color:#2b91af; font-size:small XElement<span style="font-size:small .Load(abspath3);<span style="font-size:small
<br/>
aname = <span style="color:#2b91af; font-size:small <span style="color:#2b91af; font-size:small XElement<span style="font-size:small .Load(abspath2);<br/>
<span style="font-size:small w = <span style="color:#2b91af; font-size:small <span style="color:#2b91af; font-size:small XElement<span style="font-size:small .Load(abspath1);<br/>

<span style="font-size:small <span style="font-size:small <span lang="EN <span style="color:#2b91af; font-size:small <span style="color:#2b91af; font-size:small <span lang=" Var new =
<span style="font-size:small <span style="font-size:small <span style="color:#2b91af; font-size:small <span style="color:#2b91af; font-size:small <span lang=" <span style="font-size:small <span style="font-size:small <span style="color:#2b91af; font-size:small <span style="color:#2b91af; font-size:small <span style="font-size:small <span style="font-size:small <span style="color:#2b91af; font-size:small <span style="color:#2b91af; font-size:small <span lang=" <span lang="EN <span style="color:#0000ff from<span style="font-size:small
wnt <span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small in<span style="font-size:small w.Descendants(<span style="color:#a31515; font-size:small <span style="color:#a31515; font-size:small "wanted"<span style="font-size:small )<br/>
<span style="font-size:small <span style="font-size:small <span lang="EN j<span lang=" <span lang=" <span lang=" <span lang=" <span style="font-size:small c
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small in<span style="font-size:small chrgs.Descendants(<span style="color:#a31515; font-size:small <span style="color:#a31515; font-size:small "charges"<span style="font-size:small )<br/>
<span style="font-size:small <span style="font-size:small <span lang="EN <span lang="EN <span lang="EN <span lang="EN <span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small <span lang=" on<span lang=" <span lang=" <span lang=" <span style="font-size:small
wnt.Element(<span style="color:#a31515; font-size:small <span style="color:#a31515; font-size:small "nameid"<span style="font-size:small ).Value
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small equals<span style="font-size:small c.Element(<span style="color:#a31515; font-size:small <span style="color:#a31515; font-size:small "nameid"<span style="font-size:small ).Value<br/>
<span style="font-size:small <span style="font-size:small <span lang="EN <span lang="EN <span lang="EN <span lang="EN <span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small <span lang=" join<span lang=" <span lang=" <span lang=" <span style="font-size:small
a <span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small in<span style="font-size:small aname.Descendants(<span style="color:#a31515; font-size:small <span style="color:#a31515; font-size:small "alias"<span style="font-size:small )<br/>
<span style="font-size:small <span style="font-size:small <span lang="EN <span lang="EN <span lang="EN <span lang="EN <span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small <span lang=" on<span lang=" <span lang=" <span style="font-size:small
wnt.Element(<span style="color:#a31515; font-size:small <span style="color:#a31515; font-size:small "nameid"<span style="font-size:small ).Value
<span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small equals<span style="font-size:small a.Element(<span style="color:#a31515; font-size:small <span style="color:#a31515; font-size:small "nameid").Value
group wnt by new { name = (string)wnt.Element("name"),<br/>
age = (string)wnt.Element("age"),<br/>
height = (string)wnt.Element("height"),<br/>
weight = (string)wnt.Element("weight"),<br/>
hair = (string)wnt.Element("hair"),<br/>
eyes = (string)wnt.Element("eyes"),<br/>
image = (string)wnt.Element("image"),<br/>
charge = (string)c.Element("charge"),<br/>
bail = (string)c.Element("bail"),<br/>
alias = (string)a.Element("a_name")} into g
<br/>
select new<br/>
{<br/>
g.Key.name,<br/>
//how to iterate through alias names grouped by single name
<span style="font-size:small <span style="font-size:small <span style="color:#2b91af; font-size:small <span style="color:#2b91af; font-size:small <span style="font-size:small <span style="font-size:small <span style="color:#2b91af; font-size:small <span style="color:#2b91af; font-size:small <span style="font-size:small <span style="font-size:small <span style="color:#2b91af; font-size:small <span style="color:#2b91af; font-size:small <span lang=" <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="font-size:small <span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small <span style="font-size:small <span style="font-size:small <span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small <span style="font-size:small <span style="font-size:small <span style="color:#0000ff; font-size:small <span style="color:#0000ff; font-size:small
//how to iterate through charges and bail <br/>
g.Key.age,<br/>
g.Key.height,<br/>
g.Key.weight,<br/>
g.Key.hair,<br/>
g.Key.eyes,<br/>
g.Key.image <br/>

<br/>
};<br/>
ListView.Datasource = new.ToList();<br/>
ListView.Bind();
<span lang="EN How can I get this formatted in order to get the unique name, all aliases associated with that nameid, and all charges as one distinct row into a ListView? Please let me know - Ive gotten close but I dont have the joining
and grouping correct.
<span lang="EN Thanks for your assistance with this.
<span lang="EN Jim
<span style="font-size:small <span style="font-size:small <span lang="EN

View the full article
 
Back
Top