locked
& in XML RRS feed

  • Question

  • I use Javascript to write out a XML file.  This file is then read by a C# Office Word application.  When I have an '&' in my XML file, the application gives me an error.  I know that this is because I must use '&amp' instead of '&', but how can I write this value correctly to an XML document?  

     

    Please vote if you find my post useful. Add me on Skype @ christo.skype1

    Wednesday, April 25, 2012 11:45 AM

Answers

  • You may try to use the following JavaScript function to encode any value that you want to save to file. 

    function htmlEncode(str) {
        return String(str)
                .replace(/&/g, '&')
                .replace(/'/g, ''')
                .replace(/"/g, '"')
                .replace(/</g, '&lt;')
                .replace(/>/g, '&gt;');
    }

    Daniel Cai | http://danielcai.blogspot.com | @danielwcai

    Wednesday, April 25, 2012 4:52 PM

All replies

  • Hi,

    How are you generating this xml through code ??, if so while writing your content to xml you can varify if there is '&' sign them change it ot '&amp' before writing to xml file.


    Mahain : Check My Blog
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    • Proposed as answer by MubasherSharif Wednesday, April 25, 2012 11:51 AM
    Wednesday, April 25, 2012 11:49 AM
    Moderator
  • I use FetchXML and SOAP and some of the XRM library functions.  To verify if there is an '&' sign and change it to '&amp' will be the perfect sollution, but how will I achieve this? 

    Please vote if you find my post useful. Add me on Skype @ christo.skype1

    Wednesday, April 25, 2012 11:53 AM
  • You don't specify if the section of XML containing the ampersand is node text or an attribute.  As a general rule, if the value you are writing to your xml document could contain xml characters (such as HTML snippets, javascript, ampersands, etc.) it is best to write it to a text node and enclose it in a CDATA section:  <![CDATA[myNodeText]]>.  This way it will not be misinterpreted by the XML parser.
    Wednesday, April 25, 2012 12:34 PM
  • Share you process flow here then we can help you where and how to do this conversion.

    Mahain : Check My Blog
    Follow me on Twitter
    Make sure to "Vote as Helpful" and "Mark As Answer",if you get answer of your question.

    Wednesday, April 25, 2012 4:44 PM
    Moderator
  • For any value that you use in XML document, it should be encoded. You can use an online HTML encoder to encode for you. Note that you should only need to encode the value that has special characters, not the entire XML document. 

    [EDIT] Sorry, I didn't read carefully. I will follow up on this. 


    Daniel Cai | http://danielcai.blogspot.com | @danielwcai

    Wednesday, April 25, 2012 4:47 PM
  • You may try to use the following JavaScript function to encode any value that you want to save to file. 

    function htmlEncode(str) {
        return String(str)
                .replace(/&/g, '&amp;')
                .replace(/'/g, '&#39;')
                .replace(/"/g, '&quot;')
                .replace(/</g, '&lt;')
                .replace(/>/g, '&gt;');
    }

    Daniel Cai | http://danielcai.blogspot.com | @danielwcai

    Wednesday, April 25, 2012 4:52 PM