-1

Below is sample of my XML file

<?xml version="1.0" encoding="UTF-8"?>
<summary>  
  <testresult>    
    <result value="10" name="long">100</result>
    <result value="12" name="short">200</result>
    <result value="14" name="long">300</result>
  </testresult>
  <testresult>   
    <result value="10" name="short">50</result>
    <result value="12" name="short">60</result>
    <result value="14" name="long">70</result>
  </testresult>
</summary>

I need to get attribute values for result elements.

I done it using foreach loop as below.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(item.Value);
XmlNodeList nodelist = xmlDoc.SelectNodes("//testresult");

for (int i = 0; i < nodelist.Count; i++)
{
    foreach (XmlElement child in nodelist[i])
    {
        if (child.HasAttributes)
        {
            result.Add(child.Attributes["value"].Value); //This is working fine.
        }       
    }
}

My ultimate goal is to identify the name and get value if name = "long" only.

for that I need to get the value of name Attribute. I need to do that without using foreach loop. Any suggestion to achieve my task inside the for loop ?

Thanks.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
WEAPI
  • 101
  • 4
  • 9

3 Answers3

0

You could parse your xml easy with XDocument:

var xDoc  = XDocument.Load(@"YourXmlFile");
var result = xDoc.Descendants("result")
              .Where(x=>x.Attribute("name").Value=="long")    
              .Select(x=>x.Value);
Maksim Simkin
  • 9,561
  • 4
  • 36
  • 49
0

If you can use LINQ to XML then you even can get results as integers

var xdoc = XDocument.Load(item.Value);

var results = from r in xdoc.Descendants("result")
              where (string)r.Attribute("name") == "long"
              select (int)r.Attribute("value");

Output:

[10, 14, 14]

If you have to use XmlDocument

var result = from XmlElement tr in xmlDoc.SelectNodes("//testresult")
             from XmlElement r in tr
             where r.Attributes["name"].Value == "long"
             select r.Attributes["value"].Value;

You can also provide more preciese XPath

var result = from XmlElement r in xmlDoc.SelectNodes("//testresult/result[@name='long']")
             select r.Attributes["value"].Value;    
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
0

Using xml linq to get all the values

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication43
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var results = doc.Descendants("testresult").Select(x => new {
                result = x.Elements("result").Select(y => new {
                    value = (int)y.Attribute("value"),
                    name = (string)y.Attribute("name"),
                    text = (int)y
                }).ToList()
            }).ToList();
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20