0
<test-case name="SuccessfulOneTimePayment" executed="True" result="Success" success="True" time="211.262" asserts="9">
  <categories>
    <category name="Regression" />
  </categories>
  <properties>
    <property name="TestcaseId" value="70592" />
  </properties>
</test-case>

Can anyone help me to fetch TestcaseId value=70592 from this xml?

  var testcaseid = xml.Root.Descendants("test-case").Elements("categories").Elements("properties")

 .Where(s => s.Attribute("name") != null)
 .ToList();

I tried the above code which is not helping me.

6
  • Your query suggests that properties is a child of categories while it is not. Try xml.Root.Descendants("test-case").Elements("properties"). Commented Aug 29, 2013 at 8:14
  • Wouldn't XPath serve this well? Why LINQ? Commented Aug 29, 2013 at 8:22
  • @Shahkalpesh Take a look at Cuong Le's Answer below and you find how intuitive and nice is Linq on many sources from Databse to XML. Who really wants to learn one another language to work with XML? Commented Aug 29, 2013 at 8:50
  • @Alireza: Well, XPath has been the way to query xml (before LINQ). Unless there is a reason (such as joining XML with data coming from DB), I dont know why one would use LINQ for a trivial scenario. If the OP is familiar with LINQ, he/she should go for it. Commented Aug 29, 2013 at 8:55
  • @shahkalpesh Comparing Linq2XML with XPath is like comparing EntityFramework and Linq2Sql with naitive SQL. Commented Aug 29, 2013 at 8:59

5 Answers 5

2
XDocument.Load(xml)
     .Descendants("property")
     .Where(e => (string)e.Attribute("name") == "TestcaseId")
     .Select(e => (string)e.Attribute("value"))
     .FirstOrDefault();
Sign up to request clarification or add additional context in comments.

2 Comments

What if there's extra descendants with a name of property and a value?
@It'sNotALie: Could you please elaborate more your question
0
    XDocument newTrial = XDocument.Load(@"xxxxxxxxxxx\trial.xml");

     var value = from name in newTrial.Descendants("properties")
                    where name.Element("property").Attribute("name").Value != null && name.Element("property").Attribute("name").Value == "TestcaseId"  
                    select  name.Element("property").Attribute("value").Value; 

Comments

0
yourXDocument
    .Root
    .Element("properties")
    .SelectMany(x => x.Elements("property"))
    .Where(e => (string)e.Attribute("name") == "TestcaseId")
    .Select(e => (string)e.Attribute("value"))
    .FirstOrDefault(s => !string.IsNullOrEmpty(s));

Comments

0

I think you need to get list of the attribute "value" with in the element "property" whose other attribute "name" should note be null

You can try below code:

var testcaseid1 = xdoc.Root.Descendants("property").Where(s => s.Attribute("name") != null).Select(s => s.Attribute("value").Value).ToList();

Or you can select the value of first occurrence using below code:

string testcaseid = xdoc.Root.Descendants("property").Where(s => s.Attribute("name") != null).Select(s => s.Attribute("value").Value).First();

Comments

-1

To get the value attribute you can use the following:

var foo = (from n in xml.Descendants("property")
           where n.Attribute("name").Value == "TestcaseId"
           select n.Attribute("value").Value).FirstOrDefault();

Gives: 70592

6 Comments

Seriously?! xml.Descendants().Elements("property")?!
Relax? The query is fine. I've cleaned it up for you
The fact that it returns correct results does not mean it's fine. Descendants().Elements("property") is highly inefficient...
But it still uses Descendants(), which will check whole XML tree (including all <category> tags in given XML example) before finding <property> elements. Still can be done better...
We are likely only seeing a small portion of the OP's XML structure. It's likely that the test-case node is a child of some other node or more than one other node, perhaps test-cases. In which case, your code fails (with an exception) and this succeeds. I think it's a matter of getting a complete sample of the XML before we can claim a right or wrong answer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.