5

I need to parse a simple XML string using C#

<items>
    <item1 value="value1" />
    <item2 value="value2" />
    <item3 value="value3" />
    <itme4 value="value4" />
    <item5 value="value5" />
    <itme6 value="value6" />
</items>

I'm trying to do it as follow:

    XMLParser XMLParserObj = new XMLParser();
    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.LoadXml(xmlString);

   string item1 = xmlDoc.Attributes.GetNamedItem("item1").Value;
   string item2 = xmlDoc.Attributes.GetNamedItem("item2").Value;
   string item3 = xmlDoc.Attributes.GetNamedItem("item3").Value;
   string item4 = xmlDoc.Attributes.GetNamedItem("item4").Value;
   string item5 = xmlDoc.Attributes.GetNamedItem("item5").Value;
   string item6 = xmlDoc.Attributes.GetNamedItem("item6").Value;

with no luck

please advise..

1
  • 1
    some of your xml items are named itme and not item as your code has them. Commented Nov 17, 2013 at 18:27

5 Answers 5

6

A possible solution would be to use LINQ2XML:

// retrieve a single element by tag name
private XElement getElement(XDocument xmlDocument, string elementName)
{
    var element = xmlDocument.Descendants("items").Elements().Where (x => x.Name == elementName).FirstOrDefault();
    return element != null ? element : null;
}

// retrieve attribute by name
private string attributeValue(XElement item, string attributeName)
{
    var attribute = item.Attribute(attributeName);
    return attribute != null ? attribute.Value : string.Empty;
}

The two functions can be used like this:

// input for the example
var input = @"<items>
<item1 value=""value1"" />
<item2 value=""value2"" />
<item3 value=""value3"" />
<itme4 value=""value4"" />
<item5 value=""value5"" />
<item6 value=""value6"" />
</items>";

var xml = XDocument.Parse(input);
var element = getElement(xml, "item2");
if (element != null)
{
    Console.WriteLine(attributeValue(element, "value"));
}

The output is:

value2

For a repetative tasks like this it is often advantageous to implement the methods as extensions:

public static class ProjectExtensions
{
    // retrieve a single element by tag name
    public static XElement GetElementByName(this XDocument xmlDocument, string parentElementName, string elementName)
    {
        var element = xmlDocument.Descendants(parentElementName).Elements().Where (x => x.Name == elementName).FirstOrDefault();
        return element != null ? element : null;
    }

    // retrieve attribute by name
    public static string GetAttributeValueByName(this XElement item, string attributeName)
    {
        var attribute = item.Attribute(attributeName);
        return attribute != null ? attribute.Value : string.Empty;
    }
}

The usage is IMO much more easier and cleaner:

var xml = XDocument.Parse(input);
var element = xml.GetElementByName("items", "item2");
if (element != null)
{
    Console.WriteLine(element.GetAttributeValueByName("value"));
}

You can of course (as already suggested) use XPath directly.

Sign up to request clarification or add additional context in comments.

Comments

3

leave all your code the same but change this :

string item1 = xmlDoc.GetElementsByTagName("item1")[0].Attributes[0].Value;

do the same for other items , and you will get the value.

Comments

2

Assuming you want the value attribute of each of your item elements, you could use Xpath:

        string xmlString = "<items><item1 value=\"value1\" /><item2 value=\"value2\" /><item3 value=\"value3\" /><item4 value=\"value4\" /><item5 value=\"value5\" /><item6 value=\"value6\" /></items>";

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xmlString);

        string item1 = xmlDoc.SelectSingleNode("items/item1/@value").InnerText;
        string item2 = xmlDoc.SelectSingleNode("items/item2/@value").InnerText;
        string item3 = xmlDoc.SelectSingleNode("items/item3/@value").InnerText;
        string item4 = xmlDoc.SelectSingleNode("items/item4/@value").InnerText;
        string item5 = xmlDoc.SelectSingleNode("items/item5/@value").InnerText;
        string item6 = xmlDoc.SelectSingleNode("items/item6/@value").InnerText;

Comments

0

You missunderstand the term 'Attribute', you are trying to catch "item1" and it is actually a Node.

Use this:

string item1 = xmlDoc.SelectSingleNode("//items/item1").Attributes["value"].value;

There are other options, but this will give you the values you are looking for.

Cheers...

Comments

0

this will get you all those attributes values:

1)Load xml with XDocument.Load() if from a string XDocument.Parse() then do this:

IEnumerable<string> result = from node in xmlDoc.Root.Descendants()
                             select node.Attribute("value").Value;

Comments

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.