Welcome to MSDN Blogs Sign in | Join | Help

Wriju's BLOG

.NET and everything
LINQ to XML : Creating XML from another XML

Based on my previous post if I have to filter the list of customers only in the country “US” and create another XML which may look like,

 

<?xml version="1.0" encoding="utf-8"?>

<usaCustomers>

  <usaCustomer>

    <title>Marketing Manager</title>

    <name>Howard Snyder</name>

  </usaCustomer>

  <usaCustomer>

    <title>Sales Representative</title>

    <name>Yoshi Latimer</name>

  </usaCustomer>

  <usaCustomer>

    <title>Marketing Manager</title>

    <name>John Steel</name>

  </usaCustomer>

….

 

 

To get this if I have to use the System.Xml namespace then code will become something like,

 

//Conventional XML way to generate another XML out of this XML          

XmlDocument customers = new XmlDocument();

customers.Load(@"C:\XMLData.xml");

 

XmlDocument usaCustomers = new XmlDocument();

XmlElement usaRoot = usaCustomers.CreateElement("usaCustomers");

usaCustomers.AppendChild(usaRoot);

 

foreach (XmlNode n in customers.SelectNodes(

    "/customers/customer[@country='USA']"))

{

    XmlElement usaCustomer = usaCustomers.CreateElement("usaCustomer");

 

    XmlElement usaTitle = usaCustomers.CreateElement("title");

    usaTitle.InnerText = n.Attributes["contactTitle"].Value;

    usaCustomer.AppendChild(usaTitle);

 

    XmlElement usaContact = usaCustomers.CreateElement("name");

    usaContact.InnerText = n.Attributes["contactName"].Value;

    usaCustomer.AppendChild(usaContact);

 

    usaRoot.AppendChild(usaCustomer);

}

 

This approach works perfectly fine but for this you also need to learn XPath and XQuery on top of conventional System.Xml.

 

To achieve this in simple “what you think what you write” scenario you can go for LINQ to XML.

 

//LINQ to XML way

XElement root = new XElement("usaCustomers",

                from c in XElement.Load(@"C:\XMLData.xml").Descendants("customer")

                where (string)c.Attribute("country") == "USA"

                select new XElement("usaCustomer",

                    new XElement("title", (string)c.Attribute("contactTitle")),

                    new XElement("name", (string)c.Attribute("contactName"))));

 

Its simple its magic. Thanks to Mike Taulty for my learning.

 

Namoskar!!!

Posted: Monday, February 18, 2008 8:17 PM by wriju

Comments

Noticias externas said:

Based on my previous post if I have to filter the list of customers only in the country “US” and create

# February 18, 2008 3:48 PM

Christopher Steen said:

ASP.NET .Net and Ext: Coolite has the Answer [Via: Rey Bango ] A Client-side Ajax Login for ASP.NET...

# February 19, 2008 8:19 AM

Christopher Steen said:

Link Listing - February 18, 2008

# February 19, 2008 8:19 AM

Charlie Calvert's Community Blog said:

Welcome to the forty-first Community Convergence. The big news this week is that we have moved Future

# April 19, 2008 4:12 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker