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").Valu e, _
Published = item.Element("pubDate").Va lue, _
Description = item.Element("description" ).Value, _
Link = item.Element("link").Value
Return posts
End Function
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").Valu
Published = item.Element("pubDate").Va
Description = item.Element("description"
Link = item.Element("link").Value
Return posts
End Function
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/
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");
ASKER
Thank you for the code. I will try it out and get back to you!
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?
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window