04 July 2008

Simple using LINQ for read XML file

I have xml format for store connection attribute.
<configuration>
<profile>
<host>my host</host>
<dbnm>Database Name</dbnm>
<usernm>User name</usernm>
<pwd>My PAssword</pwd>
</profile>
</configuration>


How to read this xml file ?. I try with new technologi from Microsoft.
I am using LINQ.

using System.Xml;
using System.Xml.Linq;

var custs = from c in XElement.Load("dbprofile.xml").Elements("profile")
where c.Element("host").Value == "DATABASE"
select c;

foreach (var customer in custs)
{
Console.WriteLine("Host : " + customer.Element("host").Value);
Console.WriteLine("Database : " + customer.Element("dbnm").Value);
Console.WriteLine("User Name : " + customer.Element("usernm").Value);
Console.WriteLine("Password : " + customer.Element("pwd").Value);
}


Console.ReadLine();


enjoyy

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home