zoukankan      html  css  js  c++  java
  • 解析xml字符串

    < -> &lt;
    > -> &gt;
    " -> &quot;
    ' -> &apos;
    & -> &amp;

    1. 利用string.Replace() 五次替换

    string xml = "<node>it's my \"node\" & i like it<node>";
    encodedXml
    = xml.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
    // RESULT: &lt;node&gt;it&apos;s my &quot;node&quot; &amp; i like it&lt;node&gt;

     

    2. 利用System.Web.HttpUtility.HtmlEncode() 方便

    string xml = "<node>it's my \"node\" & i like it<node>";
    string encodedXml = HttpUtility.HtmlEncode(xml);
    // RESULT: &lt;node&gt;it's my &quot;node&quot; &amp; i like it&lt;node&gt;

    3. 利用System.Security.SecurityElement.Escape() 不常用

    string xml = "<node>it's my \"node\" & i like it<node>";
    string encodedXml = System.Security.SecurityElement.Escape(xml);
    // RESULT: &lt;node&gt;it&apos;s my &quot;node&quot; &amp; i like it&lt;node&gt


    4. 利用 System.Xml.XmlTextWriter

    string xml = "<node>it's my \"node\" & i like it<node>";
    using (XmlTextWriter xtw = new XmlTextWriter(@"c:\xmlTest.xml", Encoding.Unicode))
    {
    xtw.WriteStartElement(
    "xmlEncodeTest");
    xtw.WriteAttributeString(
    "testAttribute", xml);
    xtw.WriteString(xml);
    xtw.WriteEndElement();
    }
    // RESULT:
    /*

    <xmlEncodeTest testAttribute="&lt;node&gt;it's my &quot;node&quot; &amp; i like it&lt;node&gt;">
    &lt;node&gt;it's my "node" &amp; i like it&lt;node&gt;
    </xmlEncodeTest>
    */

  • 相关阅读:
    Ubuntu下安装KVM
    Ubuntu下配置libvirt环境
    漏洞复现-Flask-SSTI服务端模板注入
    CTF-杂项笔记
    Tomcat后台爆破指南
    漏洞复现-CVE-2018-15473-ssh用户枚举漏洞
    漏洞复现-CVE-2018-8715-Appweb
    漏洞复现-CVE-2016-4437-Shiro反序列化
    漏洞复现-fastjson1.2.24-RCE
    漏洞复现-CVE-2017-4971-Spring Web Flow 远程代码执行
  • 原文地址:https://www.cnblogs.com/chenqingwei/p/1778881.html
Copyright © 2011-2022 走看看