Extract all the elements from an XELEMENT var that meet a certain condition + LINQ to XML

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<pre>I have two XELEMENTS XR and XTemp .[/code]
<pre> MY XR Contains :[/code]
<pre><pre lang="x-xml <result xmlns="http://abc.com/test>
<report>
<unit unit_number="1" id="S1 <classification>Subject</classification>
<claim_reported count="0" status="No claims reported" />
</unit>
<unit unit_number="2" id="S2 <classification>Subject</classification>
<claim_reported count="0" status="No claims reported" />
</unit>
<unit unit_number="3" id="V1 <classification>Vehicle</classification>
<claim_reported count="1" status="Claims reported" />
</unit>
......
</report>
</result> [/code]
[/code]
<div style="background-color:white; color:black My XTemp contains :
<div style="background-color:white; color:black
<pre lang="x-xml <result xmlns="http://abc.com/test
<report>
<unit unit_number="1" id="S3 <classification>Subject</classification>
<claim_reported count="1" status="Claims reported" />
</unit> <unit unit_number="2" id="S4 <classification>Subject</classification>
<claim_reported count="4" status="Claims reported" />
</unit>
<unit unit_number="3" id="V2 <classification>Vehicle</classification>
<claim_reported count="0" status="No claims reported" />
</unit> <unit unit_number="3" id="V3 <classification>Vehicle</classification>
<claim_reported count="2" status="Claims reported" /> </unit>
......
</report>
</result> [/code]

<div style="background-color:white; color:black Trying to extract all the
<unit> elements from XTemp based on a certain condtion and add them to
XR .
<pre>The condition is : get all the <unit> elements whose id begins with " V ".[/code]
<pre>Below code gives me " Object reference not set to an instance " Error. [/code]
<pre><div style="background-color:white; color:black <pre> XR.Descendants(xmlns + <span style="color:#a31515 "unit").LastOrDefault().AddAfterSelf(XTemp.Descendants(xmlns + <span style="color:#a31515 "unit") [/code]
<pre> .Where(x => x.Element(xmlns + <span style="color:#a31515 "unit").Attribute(<span style="color:#a31515 "id").Value.ToUpper().Contains(<span style="color:#a31515 "V")));



[/code]

[/code]
<pre>Thanks in advance [/code]
<
Gauranga<br/>
<br/>
<br/>
<br/>
<br/>
<br/>

View the full article
 
Back
Top