Solved

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

Posted on 2010-08-24
6
1,127 Views
Last Modified: 2013-11-11
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
0
Comment
Question by:feesu
  • 4
  • 2
6 Comments
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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

0
 

Author Comment

by:feesu
Comment Utility
I still can't get it! Plus, can you write it with vb?
0
 
LVL 53

Expert Comment

by:Dhaest
Comment Utility
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

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:feesu
Comment Utility
Thank you for the code. I will try it out and get back to you!
0
 

Author Comment

by:feesu
Comment Utility
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?
0
 

Accepted Solution

by:
feesu earned 0 total points
Comment Utility
Dhaest,

I solved it by simply adding the following line in the main getPosts function right before the Return!

        rssFeed.Save(Server.MapPath("~\RSS_BBC.xml"))


Thank you for your time!
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This video discusses moving either the default database or any database to a new volume.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now