Suppose we have a XML file (say
input.xml ) as under
<configuration><configValues first='true' second='false' third='12/29/2016' fourth='this is 4th value'/></configuration>
The objectiveis to read the attribute values. Below is the way
using System;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//the xml string
string xmlDocPath = @"input.xml";
//load the xml string in XDocument
XDocument doc = XDocument.Load(xmlDocPath);
//read the values
foreach (var element in doc.Descendants("configValues"))
{
Console.WriteLine("{0} = {1}", "first", element.Attribute("first").Value);
Console.WriteLine("{0} = {1}", "second", element.Attribute("second").Value);
Console.WriteLine("{0} = {1}", "third", element.Attribute("third").Value);
Console.WriteLine("{0} = {1}", "fourth", element.Attribute("fourth").Value);
}
Console.ReadKey();
}
}
}
Output
------- first = true
second = false
third = 12/29/2016
fourth = this is 4th value