Solved

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

Posted on 2010-08-24
6
1,132 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
ID: 33509725
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
ID: 33509767
I still can't get it! Plus, can you write it with vb?
0
 
LVL 53

Expert Comment

by:Dhaest
ID: 33509826
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

Author Comment

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

Author Comment

by:feesu
ID: 33710453
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
ID: 33710607
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Jquey and footrable 2 34
c# LinkButton OnClientClick 2 33
Angular JS Route 3 44
IIS issues - Convert to Application - APS.NET v4.0 3 17
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

932 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