views:

537

answers:

2

Hello,

I am new with learning the possibilities of Linq to XML and I recently have found that I can query an xml like a data base (I am quite fascinated by it now).

My question is How can I query an xml file and save the result in another xml file?:

 string url = "employees.xml";

 XElement employees= XElement.Load(url);


 if (employees.Element("employee") != null)

 {

     var query = from f in employees.Element("employee").Elements("item").Take(10)

                 select new { Name = f.Element("name").Value, Surname= f.Element("surname").Value };


     foreach (var feed in query)

     {

        //here... I like to write the result in a different xml file, I tried the 
        //common 
        doc.save("xmlout.xml");


     }

 }

Thanks a lot for your help,

+1  A: 

Well, you could do this by creating an XDocument/XElement instance and then populating it with the results of your query (by passing the query to the constructor of the XDocument/XElement), and then saving that.

However, you might want to consider using an XSLT transformation instead, as that is really what you are trying to do here.

casperOne
Yap, XSLT would probably be the best way when you want to transform one XML document into another.
0xA3
A: 

This article should help you solve your problem. Btw if you new up as XElement and then populate you can use the save method, instead of using anonymous types.

Perpetualcoder