Michael Joseph
Michael Joseph

Reputation: 47

How to insert new item to channel in xml file

I want modify an existing xml file to insert new element inside the Channel using vb code my xml file looks like :

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>Latest News from Science Magazine</title>
<link>http://www.sciencemag.org/rss/news_current.xml</link>
<description>Editable in modal at Format : RSS Feed | Settings : RSS Description</description>
<pubDate>Wed, 25 Jul 2018 09:10:28 -0400</pubDate

<item>
  <title><![CDATA[this is title]]></title>
  <link>http://karary-001-site1.htempurl.com</link>
  <pubDate>2018/07/26 06:29</pubDate>
  <description><![CDATA[this id desc]]></description>
  <media.thumbnail url="http://karary.com" height="266" width="127" />
</item>

</channel>
</rss>

this is my code :

Dim root = New XElement("item")
    Dim title = New XElement("title", New XCData(TextBox3.Text))
    Dim link = New XElement("link", TextBox6.Text)
    Dim pubDate = New XElement("pubDate", DateTime.Now.ToString("yyy/MM/dd HH:mm"))
    Dim description = New XElement("description", New XCData(TextBox5.Text))
    Dim thumbnail = New XElement("media.thumbnail",
                                  New XAttribute("url", "http://karary-001-site1.htempurl.com/files/" + attac1 + "?itok=YdFLolAU"),
                                  New XAttribute("height", 266),
                                  New XAttribute("width", 127))
    root.Add(title, link, pubDate, description, thumbnail)
    document.Root.Add(root)
    document.Save(FilePath)

my code add new items after channel and rss tag end !!

Upvotes: 0

Views: 247

Answers (1)

David
David

Reputation: 6111

You're currently adding it to the document's root element, which in this case is <rss />. Instead you want to add it to the root element's first element:

document.Root.Elements.First().Add(root)

Upvotes: 1

Related Questions