Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2010-08-24
6
Medium Priority
?
1,145 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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Loops Section Overview
Suggested Courses

972 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