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:
My edit code looks like this (where lstExistingNews is a ListBox and the txt[SomeName] objects are TextBoxes):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>
VB Code:
Dim xmlDoc As XmlDocument = New XmlDocument() xmlDoc.Load(Server.MapPath("/CHC/xml/news.xml")) Dim xmlEdit As XmlNode = _ xmlDoc.SelectSingleNode("/news/newsItems/newsItem[@ccid='" & _ lstExistingNews.SelectedItem.Value & "']") xmlEdit.SelectSingleNode("headline").InnerText = txtHeadline.Text Dim xmlCData As XmlCDataSection = _ xmlDoc.CreateCDataSection(Replace(txtBody.Text, vbCrLf, "<br />")) xmlEdit.SelectSingleNode("body").ReplaceChild(xmlCData, _ xmlEdit.SelectSingleNode("body").FirstChild) xmlEdit.SelectSingleNode("author").InnerText = txtAuthor.Text xmlEdit.SelectSingleNode("email").InnerText = txtEmail.Text 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:
xmlEdit.SelectSingleNode("body/text()[2]").InnerText = _ Replace(txtBody.Text, vbCrLf, "<br />")
Any ideas?


Reply With Quote