Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse XML string using C#

Tags:

c#

xml

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..

like image 676
Mostafa Moatassem Avatar asked Nov 17 '13 14:11

Mostafa Moatassem


3 Answers

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.

like image 61
keenthinker Avatar answered Sep 18 '22 14:09

keenthinker


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.

like image 20
T-D Avatar answered Sep 19 '22 14:09

T-D


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;
like image 36
geedubb Avatar answered Sep 20 '22 14:09

geedubb