What are invalid characters in XML

后端 未结 15 1604
时光说笑
时光说笑 2020-11-22 03:23

I am working with some XML that holds strings like:

This is a string

Some of the strings that I am passing to the

15条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 04:01

    ampersand (&) is escaped to &
    
    double quotes (") are escaped to "
    
    single quotes (') are escaped to ' 
    
    less than (<) is escaped to < 
    
    greater than (>) is escaped to >
    

    In C#, use System.Security.SecurityElement.Escape or System.Net.WebUtility.HtmlEncode to escape these illegal characters.

    string xml = "it's my \"node\" & i like it 0x12 x09 x0A  0x09 0x0A ";
    string encodedXml1 = System.Security.SecurityElement.Escape(xml);
    string encodedXml2= System.Net.WebUtility.HtmlEncode(xml);
    
    
    encodedXml1
    "<node>it's my "node" & i like it 0x12 x09 x0A  0x09 0x0A <node>"
    
    encodedXml2
    "<node>it's my "node" & i like it 0x12 x09 x0A  0x09 0x0A <node>"
    

提交回复
热议问题