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..
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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With