Results 1 to 1 of 1

Thread: Editing a CDataSection

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Post Editing a CDataSection

    Hi,

    What's the best way of editing a CDataSection in an XML file?

    At the moment, the structure of my file is something like this:
    Code:
    <?xml version="1.0"?>
    <news>
      <newsItems>
        <newsItem ccid="000000">
          <headline>Headline</headline>
          <date>14/03/06</date>
          <body><![CDATA[Some text...]]></body>
          <author>Oliver</author>
          <email>[email protected]</email>
        </newsItem>
      </newsItems>
    </news>
    My edit code looks like this (where lstExistingNews is a ListBox and the txt[SomeName] objects are TextBoxes):
    VB Code:
    1. Dim xmlDoc As XmlDocument = New XmlDocument()
    2.     xmlDoc.Load(Server.MapPath("/CHC/xml/news.xml"))
    3.  
    4.       Dim xmlEdit As XmlNode = _
    5.        xmlDoc.SelectSingleNode("/news/newsItems/newsItem[@ccid='" & _
    6.        lstExistingNews.SelectedItem.Value & "']")
    7.  
    8.       xmlEdit.SelectSingleNode("headline").InnerText = txtHeadline.Text
    9.       Dim xmlCData As XmlCDataSection = _
    10.        xmlDoc.CreateCDataSection(Replace(txtBody.Text, vbCrLf, "<br />"))
    11.       xmlEdit.SelectSingleNode("body").ReplaceChild(xmlCData, _
    12.         xmlEdit.SelectSingleNode("body").FirstChild)
    13.       xmlEdit.SelectSingleNode("author").InnerText = txtAuthor.Text
    14.       xmlEdit.SelectSingleNode("email").InnerText = txtEmail.Text
    15.  
    16.     xmlDoc.Save(Server.MapPath("/CHC/xml/news.xml"))

    This works well enough, but shouldn't there be a way to access the contents of the CDataSection directly? I think I read somewhere that it counts as normal text under one model, but as a node under DOM, or something like that(?)

    I was hoping to do something like:
    VB Code:
    1. xmlEdit.SelectSingleNode("body/text()[2]").InnerText = _
    2.    Replace(txtBody.Text, vbCrLf, "<br />")

    Any ideas?
    Last edited by olamm2k; Mar 15th, 2006 at 05:49 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width