none
How to escape and unescape greater than and less than sign when creating a xml string

    Question

  • Hello

     

    I am writing a small visual basic  application to transfer data from an old SQL server database to new one.

    ( structure of old and new database is not similar)

     

    so to do the transfer , I extract data row wise and create an xml string like this

     strProductXML = "<Data><ProdID>" & strProdID & "</ProdID>" & _
                 "<ProdColor>" & rowSQL("ProdColor") & "</ProdColor><ProdUser>" & rowSQL("ProdData") & _
                "</ProdUser><ProdYear>" & rowSQL("ProdYear") & "</ProdYear></Data>"

     

    But the problem is rowSQL("ProdData") : this column can have values that contain less than(<) and greater than sign (>)

    Proddata can have data like

    "The front size measure is 6 inches <9rear face size."

    "The units produced > year 1999"

    " rear 9cm>front face at height of 8"

     

    due to such data when I use this   

    xmlDOM.LoadXml(strXML)

     in my code , i get error because those > and < signs make my XML string wrong.

     

    Is there any way to escape it, while creating the XML String but Unescape it when this is being inserted back into database.

    I use a stored procedure to insert my data. That stored procedure is called by my VB code.

     

     

     

     

     

    Tuesday, February 22, 2011 11:05 PM

Answers

All replies

  • You may use CData to wrap the content that may contain < or > signs. There is an example @ http://www.w3schools.com/XML/xml_cdata.asp.
    • Marked as answer by Cimonp Thursday, February 24, 2011 7:33 PM
    Wednesday, February 23, 2011 3:08 AM
    Moderator
  • Ideally, you should not create Xml document by concatenating strings. The best way to create an xml document is to use XmlWriter. It will ensure that the document you create is a valid Xml document. It will always encode characters if necessary/possible. Example:

            writer.WriteStartElement("root");
            writer.WriteString("abc > xyz ?");
            writer.WriteEndElement();
    

    results in the following:

    <root>abc &gt; xyz ?</root>
    

    Which is a valid Xml document where the '>' character was replaced with the built-in entity &gt;

    Pawel 

    Wednesday, February 23, 2011 3:42 AM
    Moderator