MSSQLTips.com - your daily source for SQL Server tips

Google
 
Web mssqltips.com

ESSENTIALS: Home | Tips | Search | Categories | Top 10 | Products | Authors | Blogs | Forums | Webcasts | Advertise | About
Reading XML documents using LINQ to XML -

in Search

Reading XML documents using LINQ to XML

Last post 07-18-2008 10:10 AM by bass_player. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 06-24-2008 12:30 AM

    Reading XML documents using LINQ to XML

    This post is related to this tip: Reading XML documents using LINQ to XML

    http://www.mssqltips.com/tip.asp?tip=1524

  • 07-18-2008 7:20 AM In reply to

    Re: Reading XML documents using LINQ to XML

    Gentlemen,

    I have just found your forum and I have a Ling to XML question relating to the above post.  How can I return just the values associated with the nodes and not include the child node start and end tags?  I then need to load the values (there are two in each parent node) into temporary storage such as an array.

    <testcenter>

           <testcentercode>12345</testcentercode>

          <zipcode>01945</zipcode>

    There could be up to 200 "<testcenter> nodes".  I need to load just the values of the two children of each into an array.

     

  • 07-18-2008 10:10 AM In reply to

    Re: Reading XML documents using LINQ to XML

    It's just a matter of reading the value of the element name.  Here's a sample code based on the article. Notice that I have commented out the Fax element since there are a few records with no Fax element. Therefore, if you need to iterate thru your records in the XML document, you need to implement your own error handling mechanism.  Paste the code inside the static void Main(string[ ] args)

    // Create the query

    var custs = from c in XElement.Load("Customers.xml").Elements("Customers")

    select new

    {

    CustomerID = c.Element("CustomerID").Value,

    CompanyName = c.Element("CompanyName").Value,

    ContactName = c.Element("ContactName").Value,

    ContactTitle = c.Element("ContactTitle").Value,

    Address = c.Element("Address").Value,

    City = c.Element("City").Value,

    PostalCode = c.Element("PostalCode").Value,

    Country = c.Element("Country").Value,

    Phone = c.Element("Phone").Value,

    //Fax = c.Element("Fax").Value

    };

    // Execute the query

    foreach (var customer in custs)

    {

    Console.WriteLine("CustomerID:" + customer.CustomerID);

    Console.WriteLine("Company Name:" + customer.CompanyName);

    Console.WriteLine("Contact Name:" + customer.ContactName);

    Console.WriteLine("Contact Title:" + customer.ContactTitle);

    Console.WriteLine("Address:" + customer.Address);

    Console.WriteLine("City:" + customer.City);

    Console.WriteLine("Postal Code:" + customer.PostalCode);

    Console.WriteLine("Country:" + customer.Country);

    Console.WriteLine("Phone:" + customer.Phone);

    //Console.WriteLine("Fax:" + customer.Fax);

    Console.WriteLine("========================");

    }

     

    //Pause the application

    Console.ReadLine();

Page 1 of 1 (3 items)