Question XML Parsing Error - Escaping Characters

sisquo76

Active member
Joined
Dec 1, 2008
Messages
28
Location
Bosnia and Herzegovina
Programming Experience
3-5
Hi all,

How can I replace illegal characters inside XML tags with legal ones eg. & with & etc.

My code is like this:
VB.NET:
Imports System.Security
Imports System.Xml

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim DocXml As New XmlDocument
        DocXml.Load("ticker.xml")
        Dim MyXMLNodes As XmlNodeList = DocXml.SelectNodes("/tickerfield/playlist/element/")
        For Each MyXMLNode As XmlNode In MyXMLNodes
            MyXMLNodes.Item(1).InnerText = System.Security.SecurityElement.Escape(MyXMLNodes.Item(1).InnerText)
        Next
        DocXml.Save("ticker1.xml")
    End Sub

End Class

... and my XML is like this:
VB.NET:
<tickerfeed version="2.4">
 <playlist type="scrooling_carousel" name="News" target="carousel">

  <element>
   <template>News</template>
   <field name="01">S & M</field>
  </element>

  <element>
   <template>News</template>
   <field name="01">x < y</field>
  </element> 

 </playlist>
</tickerfeed>

The code above gives me parsing error.

Thanks...
 
You can't really use Xml tools to fix something that can't be read as Xml. As you are aware those chars are not legal in Xml and shouldn't have been there in the first place. You should fix what outputted that data and then output the data correctly. If that is not possible you will have to use text/string tools to fix it, for example File.ReadAllText and String.Replace methods. As you can imagine that will be very difficult to do for example for the < char even if you create an advanced regex based node parser.
 
Back
Top