Link to home
Start Free TrialLog in
Avatar of feesu
feesu

asked on

ASP.NET - How to use System.Xml.Linq.XElement to write XML

Hi Experts,

I have got the following code that reads BBC RSS. I need to have this code object write this XML into a file. I used to do that with Dataset.WriteXML with one line, but seem not to know the way with linq:

    Function getRSSPosts(ByVal sLink As String) As Object
        Dim rssFeed As XDocument = XDocument.Load(sLink)

        Dim posts = From item In rssFeed.Descendants("item") _
                    Select Title = item.Element("title").Value, _
                           Published = item.Element("pubDate").Value, _
                           Description = item.Element("description").Value, _
                           Link = item.Element("link").Value
        Return posts

    End Function
Avatar of Dirk Haest
Dirk Haest
Flag of Belgium image

An Example. You should get the idea.


XElement xml = new XElement("RSSFeeds", 
            From item In rssFeed.Descendants("item") 
            select new XElement("RSSFeed", 
                new XElement("Title", item.Element("title").Value), 
                new XElement("Published", item.Element("pubDate").Value), 
                new XElement("Description ", item.Element("description").Value), 
                new XElement("LIink", item.Element("link").Value)
                ) 
            ); 

Open in new window

Avatar of feesu
feesu

ASKER

I still can't get it! Plus, can you write it with vb?
Write an XML File Using LINQ to XML
http://blog.linqexchange.com/index.php/how-to-write-an-xml-file-using-linq-to-xml/
XDocument xdoc = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XComment("List of RSSFeeds"),
            XElement xml = new XElement("RSSFeeds", 
            From item In rssFeed.Descendants("item") 
            select new XElement("RSSFeed", 
                new XElement("Title", item.Element("title").Value), 
                new XElement("Published", item.Element("pubDate").Value), 
                new XElement("Description ", item.Element("description").Value), 
                new XElement("LIink", item.Element("link").Value)
                ) 
            ); 
            // save the document
            xdoc.Save(@"c:temp2sample.xml");

Open in new window

Avatar of feesu

ASKER

Thank you for the code. I will try it out and get back to you!
Avatar of feesu

ASKER

Dhaest,

I tried to convert your code to vb but I got an error on several converters. Can you post the code in vb for me?
ASKER CERTIFIED SOLUTION
Avatar of feesu
feesu

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial