?
Solved

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

Posted on 2010-08-24
6
Medium Priority
?
1,144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 

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

PowerShell Core for Advanced Linux Administrators

Understand advanced principals around Powershell Core with a focus on the Linux Administrator.  This course covers how to administer numerous environments across multiple platforms including Linux, Azure, AWS, and Google Cloud from a single shell instance.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

719 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