Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I want to insert new element inside <channel> tag 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>

</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 !!

What I have tried:

add my element's to the root element's first element using :
document.Root.Elements.First().Add(root)
Posted
Updated 3-Aug-18 8:22am
Comments
ICEFLOWER2 2-Aug-18 22:17pm    
May be: document.DocumentElement.Item("channel").AppendChild(root)
Member 13366692 3-Aug-18 3:51am    
error DocumentElement is not member of 'System.Xml.Linq.XDocument'

1 solution

document.Root is the <rss> element; you need to add your new element to the <channel> element within the root element.

Replace:
document.Root.Add(root)
With:
document.Root.Element("channel").Add(root)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900