Link to home
Start Free TrialLog in
Avatar of vpekulas
vpekulas

asked on

Save XML node as CDATA rather then just a regular node

How can I save each node as CDATA rather then default text which gets processed by VB and removes all potentially invalid characters ?
The code I have now is below, where can I set the node being saved as CDATA ?


Dim I As Integer = 0
Dim FileLocation As String = StatusStrip1.Items(0).Text
Dim loXMLDoc As XmlDocument = New XmlDocument, loNode As XmlNode        
loXMLDoc.Load(FileLocation)


For I = 0 To dgPublic.Rows.Count - 1
    ID = dgPublic.Rows(I).Cells(0).Value.ToString
    sValue = dgPublic.Rows(I).Cells(1).Value.ToString
    Try
        loNode = loXMLDoc.SelectSingleNode("//Root/PS/KeyWord[@id=" & ID & "]")
        loNode.InnerText = sValue
    Catch ex As Exception
        blDONE = False
        strERROR = strERROR & "Node ID " & ID & " failed to update." & vbCrLf
    End Try
Next

loXMLDoc.Save(FileLocation)
Avatar of pradeepsudharsan
pradeepsudharsan

Use  WriteCData method of XmlWriter Class
Dim writer As XmlTextWriter = Nothing
writer = New XmlTextWriter(filename, Nothing)
writer.Formatting = Formatting.Indented

writer.WriteStartDocument()
 writer.WriteCData("value")
writer.WriteEndDocument()
writer.Flush()
   writer.Close()


Avatar of vpekulas

ASKER

OK, but how I can use this to update specific node ? As you can see in the code above I'm asking it
to update node:

loNode = loXMLDoc.SelectSingleNode("//Root/PS/KeyWord[@id=" & ID & "]")

How can I do the same for the WriteCData  method ?
ASKER CERTIFIED SOLUTION
Avatar of Bob Learned
Bob Learned
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial