Community discussion forum

Asp.net using VB.net to insert into xml document

  • 1 year ago

    Hello,

    I wasn't sure if this should go in the XML or .Net section but since it's the .Net code I'm querying I figure this is the right spot. I'm trying to add to an XML file using VB.net on an ASP.Net web site. Right now, I'm using 3 text boxes on a page that would have the "title", "description", and "link". I'm able to add information to these text boxes and when I click the submit button it updates the XML but overwrites the original content. What I'm trying to do is append the xml with new content and place it at the beginning of the original content because the original content will get older and this is for new changes to the site.

    Here is my xml code:

    <?xml version="1.0" encoding="UTF-8"?>
    <rss>
    <channel>
    <item id="1">
    <title>Test Title</title>
    <description>This is a test link</description>
    <link>http://www.msn.com</link>
    </item>
    </channel>
    <item />
    </rss>


    Here is my source code:

    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btnSend.Click

    'Declarations
    Dim loXMLDoc As XmlDocument = New XmlDocument
    Dim loNode As XmlNode
    Dim loNode2 As XmlNode
    Dim loNode3 As XmlNode

    loXMLDoc.Load(HttpContext.Current.Server.MapPath("news.xml")) 'loads the xml file
    loNode2 = loXMLDoc.SelectSingleNode("//rss/channel/item/title") 'path to node that's changed
    loNode2.InnerText = txtTitle.Text 'text box that users enter the information to add to the xml file
    loNode = loXMLDoc.SelectSingleNode("//rss/channel/item/description") 'path to node that's changed
    loNode.InnerText = txtDescription.Text 'text box that users enter the information to add to the xml file
    loNode3 = loXMLDoc.SelectSingleNode("//rss/channel/item/link") 'path to node that's changed
    loNode3.InnerText = txtLink.Text 'text box that users enter the information to add to the xml file

    loXMLDoc.Save(HttpContext.Current.Server.MapPath("news.xml"))
    loNode = Nothing
    loNode2 = Nothing
    loNode3 = Nothing
    loXMLDoc = Nothing

    Response.Redirect("http://theredirectpagegoeshere")
    End Sub

    So, I'm trying to add more title, description, link but it's not working out at planned. The item has an id but it doesn't have to as I'm checked modified it and viewed it on the web and it just displays them in the order of the document. Any help would be appreciated. I know this is probably a simple fix like modifying the loXMLDoc.Save to something else but I just can't put my finger on it.

    Thanks!

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Want to stay in touch with what's going on? Follow us on twitter!