1

I have a xml structure:

 <pit>
  <ROW TestID="47855" ExecutionID="1510034507" TestName="USHP" AssertionName="Check News" Status="1" TestLatencyMs="5448" Date="2011-11-29 01:43:45.117" HttpCode="" Error="" TestOwner="mdk" AssertionID="119117" /> 
  <ROW TestID="47855" ExecutionID="1510028579" TestName="USHP" AssertionName="Check News" Status="0" TestLatencyMs="7312" Date="2011-11-29 01:41:46.273" HttpCode="" Error="" TestOwner="fdxk" AssertionID="119117" /> 
  <ROW TestID="47855" ExecutionID="1510022153" TestName="USHP" AssertionName="Check News" Status="0" TestLatencyMs="5860" Date="2011-11-29 01:39:44.153" HttpCode="" Error="" TestOwner="klo" AssertionID="119117" /> 
  </pit>

and I am trying to use this query to fetch my ExecutionIDs but to no avail :( I don't know what is wrong.

    List<int> executionIdList = new List<int>();
    try
    {
        executionIdList = (from result in xDoc.Root.Descendants("ROW")
                            where result != null &&
                            result.Attribute("Status").Value.Equals("0", StringComparison.CurrentCultureIgnoreCase)

                           select result.Attributes("ExecutionID")).FirstOrDefault().Select(x => int.Parse( x.Value)).ToList();
    }

I get only the first value with the above query.

Alberto Solano
  • 7,972
  • 3
  • 38
  • 61
Navyseal
  • 891
  • 1
  • 13
  • 36

3 Answers3

1

The reason that you are not getting the results is because

IEnumerable<int> executionIds;

executionIds =
    from result in xDoc.Root.Descendants("ROW").Attributes("ExecutionID")
    select int.Parse(result.Value);

Is what you need. Your function includes a lot of seemingly unnecessary code (but that is for you to judge) in the select portion, including a FirstorDefault statement which will only select the first record or default to a predefined value if no record exists.

Levi Botelho
  • 24,626
  • 5
  • 61
  • 96
1

You can fetch yout ExecutionIds from the XDocument as shown below:

XDocument doc = XDocument.Load(@"C:\Sample1.xml");
List<string> ids = doc.Descendants("pit").Elements().Select(i => i.Attribute("ExecutionID").Value).ToList();

The above code loads the xml from the file Sample1.xml and then fetches all the executionIDs into a list object. You can add more conditions to the query if needed like you are checking if the status value is "0". Below is the code with the condition

List<string> ids = doc.Descendants("pit").Elements().Where(k=>k.Attribute("Status").Value == "0").Select(i => i.Attribute("ExecutionID").Value).ToList();

Hope that helps.

Rajesh
  • 7,766
  • 5
  • 22
  • 35
0

You are getting elements of first row because your query is calling descendants on "ROW" element i.e.

xDoc.Root.Descendants("ROW")

Try by replacing this with following code :

xDoc.Root.Descendants("pit")

This will return all the elements, which are the descendants of element.

Pawan Mishra
  • 7,212
  • 5
  • 29
  • 39
  • nope this doesnt work..I get a object reference exception with this – Navyseal Nov 29 '11 at 10:10
  • He can select "ROW" and it will return a collection of all "ROW" elements. "Pit" would return the parent element which you would have to iterate through afterwards unnecessarily. – Levi Botelho Nov 29 '11 at 10:20
  • I think I got this executionIdList = (from result in xDoc.Root.Descendants("ROW") where result != null && result.Attribute("Status").Value.Equals("Fail", StringComparison.CurrentCultureIgnoreCase) select int.Parse( result.Attribute("ExecutionID").Value)).ToList(); – Navyseal Nov 29 '11 at 10:21