Could someone assist with this .NET Linq query issue. It should work, but it doesn't and fails to find the element. The query should result in a value = 106492, but just returns null. Tnx so much!
Here's the .Net Fiddle code:
using System;
using System.Xml.Linq;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static void Main()
{
String rxml = @"<TestMessage>
<TestIdentification>
<TestID>106491</TestID>
<TestType>TESTID1</TestType>
</TestIdentification>
<TestIdentification>
<TestID>106492</TestID>
<TestType>TESTID2</TestType>
</TestIdentification>
<TestIdentification>
<TestID>106493</TestID>
<TestType>TESTID3</TestType>
</TestIdentification>
<TestIdentification>
<TestID>106494</TestID>
<TestType>TESTID4</TestType>
</TestIdentification>
</TestMessage>
";
XElement xml = XElement.Parse(rxml);
// Read all response fields responseLog class
var testObj = (from el in xml.DescendantsAndSelf()
select new TestClassObj
{
TestID = el.Elements("TestIdentification").Elements("TestType").
Where( c => c.Value == "TESTID2" ).Elements("TestID").
Where( c => !string.IsNullOrWhiteSpace(c.Value)).FirstOrDefault().Value,
}).FirstOrDefault();
if( testObj != null )
Console.WriteLine(testObj.TestID);
}
}
public class TestClassObj
{
public String TestID{ get; set; }
public String TestText{ get; set; }
}