LINQ to messy XML

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi Everyone!
I have a messy xml file like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<FOF Name="myName" Previous="00374" Date="20110701
<OB cl="FOMessage" no="1019100851" vr="143" od="1" mn="0
<R no="1019" nm="Data
<RP mn="0
<OB cl="identifier1" no="101" vr="1.43"
od="2" mn="3
<tx no="51" nm="provider1ID" mn="0" ky="1" />
<tx no="51" nm="provider2ID" mn="0" ky="1" vl="0100716" />
<tx no="51" nm="provider3ID" mn="0" ky="1" />
<lg no="247" nm="name" mn="0
<ltx nm="de" mn="0" vl="Umtausch wegen ISIN-Anderung - rest of message" />
</lg>
<lg no="247" nm="description" mn="0" />
<em no="247" nm="statusType" mn="0" dm="CEStatusType" vl="1" />
<dt no="247" nm="date" mn="0"
vl="20.07.2010" />
<em no="247" nm="contentType" mn="0" dm="ContentType" vl="2" />
<em no="247" nm="instanceQualityType" mn="0" dm="InstanceQualityType" />
</OB>
</RP>
</R>
</OB>
<OB cl="FOMessage" no="1019100851" vr="143" od="1" mn="0
<R no="1019" nm="Data
<RP mn="0
<OB cl="identifier2" no="101" vr="1.43" od="2" mn="3
<tx no="51" nm="provider1ID" mn="0" ky="1" />
<tx no="51" nm="provider2ID" mn="0" ky="1" vl="0100716" />
<tx no="51" nm="provider3ID" mn="0" ky="1" />
<lg no="247" nm="name" mn="0
<ltx nm="de" mn="0" vl="Umtausch wegen ISIN-Anderung - rest of message99" />
</lg>
<lg no="247" nm="description" mn="0" />
<em no="247" nm="statusType" mn="0" dm="CEStatusType" vl="1" />
<dt no="247" nm="date" mn="0" vl="20.08.2010" />
<em no="247" nm="contentType" mn="0" dm="ContentType" vl="2" />
<em no="247" nm="instanceQualityType" mn="0" dm="InstanceQualityType" />
</OB>
</RP>
</R>
</OB>
<OB cl="FOMessage" no="1019100851" vr="143" od="1" mn="0
<R no="1019" nm="Data
<OB cl="identifier1" no="101" vr="1.43" od="2" mn="3
<tx no="51" nm="provider1ID" mn="0" ky="1" />
<tx no="51" nm="provider2ID" mn="0" ky="1" vl="0100716" />
<tx no="51" nm="provider3ID" mn="0" ky="1" />
<lg no="247" nm="name" mn="0
<ltx nm="de" mn="0" vl="Umtausch wegen ISIN-Anderung - rest of message4" />
</lg>
<lg no="247" nm="description" mn="0" />
<em no="247" nm="statusType" mn="0" dm="CEStatusType" vl="1" />
<dt no="247" nm="date" mn="0" vl="20.07.2011" />
<em no="247" nm="contentType" mn="0" dm="ContentType" vl="2" />
<em no="247" nm="instanceQualityType" mn="0" dm="InstanceQualityType" />
</OB>
<RP mn="0
<OB cl="identifier1" no="101" vr="1.43" od="3" mn="3
<tx no="51" nm="provider1ID" mn="0" ky="1" />
<tx no="51" nm="provider2ID" mn="0" ky="1" vl="0100716" />
<tx no="51" nm="provider3ID" mn="0" ky="1" />
<lg no="247" nm="name" mn="0
<ltx nm="de" mn="0" vl="Umtausch wegen ISIN-Anderung - rest of message1" />
</lg>
<lg no="247" nm="description" mn="0" />
<em no="247" nm="statusType" mn="0" dm="CEStatusType" vl="1" />
<dt no="247" nm="date" mn="0" vl="20.06.2010" />
<em no="247" nm="contentType" mn="0" dm="ContentType" vl="2" />
<em no="247" nm="instanceQualityType" mn="0" dm="InstanceQualityType" />
</OB>
</RP>
</R>
</OB>
<OB cl="FOMessage" no="1019100851" vr="143" od="1" mn="0
<R no="1019" nm="Data
<RP mn="0
<OB cl="identifier1" no="101" vr="1.43" od="2" mn="3
<tx no="51" nm="provider1ID" mn="0" ky="1" />
<tx no="51" nm="provider2ID" mn="0" ky="1" vl="0100716" />
<tx no="51" nm="provider3ID" mn="0" ky="1" />
<lg no="247" nm="name" mn="0
<ltx nm="de" mn="0" vl="Umtausch wegen ISIN-Anderung - rest of message2" />
</lg>
<lg no="247" nm="description" mn="0" />
<em no="247" nm="statusType" mn="0" dm="CEStatusType" vl="1" />
<em no="247" nm="contentType" mn="0" dm="ContentType" vl="2" />
<em no="247" nm="instanceQualityType" mn="0" dm="InstanceQualityType" />
</OB>
</RP>
</R>
</OB>
</FOF>
I would like to get such an output using LINQ:
1|rest of message|20.07.2010
2|#Null|#Null
3|rest of message1|20.06.2010
4|rest of message2|#Null
In other words...
For each element <OB> in <FOF>:
Check if there is element <OB> where cl="identifier1"
If yes then take values: vl where value begins with "Umtausch wegen ISIN-Anderung - " and value vl where nm="date"
If there is more than one element <OB> where cl="identifier1" in <FOF>.<OB> then take the same values as above for the element <OB> where value od is the highest.
Otherwise return #Null


I wrote something like this:

<div style="color:black; background-color:white
<pre> XDocument xdoc = XDocument.Load(strFilename);
<span style="color:blue var feeds = <span style="color:blue from feed <span style="color:blue in xdoc.Descendants(<span style="color:#a31515 "OB")
<span style="color:blue where feed.Attribute(<span style="color:#a31515 "cl") != <span style="color:blue null && feed.Attribute(<span style="color:#a31515 "cl").Value == <span style="color:#a31515 "identifier1"
<span style="color:blue select <span style="color:blue new
{
val1 = feed.Elements().Where(prop => prop.Attribute(<span style="color:#a31515 "vl") != <span style="color:blue null && prop.Attribute(<span style="color:#a31515 "vl").Value.Contains(<span style="color:#a31515 "Umtausch wegen ISIN-Anderung - ") == <span style="color:blue true),
val2 = feed.Elements().Where(prop => prop.Attribute(<span style="color:#a31515 "vl") != <span style="color:blue null && prop.Attribute(<span style="color:#a31515 "nm").Value == <span style="color:#a31515 "date")
};
<span style="color:blue foreach (<span style="color:blue var feed <span style="color:blue in feeds)
{
<span style="color:blue try
{
Console.WriteLine(<span style="color:#a31515 "{0}t", feed.val1.ElementAt(0).Attribute(<span style="color:#a31515 "vl").Value);
}
<span style="color:blue catch (Exception)
{
}
<span style="color:blue try
{
Console.WriteLine(<span style="color:#a31515 "{0}", feed.val2.ElementAt(0).Attribute(<span style="color:#a31515 "vl").Value);
}
<span style="color:blue catch (Exception)
{
Console.WriteLine(<span style="color:#a31515 "");
}
}
[/code]

<br/>
<br/>

But it is far from my expectations.
I cant get val1 element.
Could you help me with that?
<br/>
<br/>
<br/>
<br/>

View the full article
 
Back
Top