2015-05-09 119 views
1

我想在C#创建XML文件,该文件在属性之一将得到另一个XML作为价值的价值:组XML作为XML节点属性

XmlDocument doc = new XmlDocument(); 
XmlElement nodElement = doc.CreateElement(string.Empty, "node", string.Empty); 
       nodElement.SetAttribute("text", MyXMLToInsert); 
doc.AppendChild(nodElement); 

MyXMLToInsert将财产以后这样的:

<xml xmlns:o="urn:schemas-microsoft-com:office:office" 
xmlns:w="urn:schemas-microsoft-com:office:word" 
xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" 
xmlns="http://www.w3.org/TR/REC-html40"> 

<head> 
<meta http-equiv=Content-Type content="text/html; charset=utf-8"> 
. 
. 

如何防止第二个XML的特殊字符不与主要字符冲突? 谢谢。

回答

1

调用SetAttribute方法将负责转义数据。

假设您从位于应用程序根目录下的文件“Text.txt”中读取了MyXMLToInsert的内容。

var doc = new XmlDocument(); 
     var nodElement = doc.CreateElement(string.Empty, "node", string.Empty); 
     nodElement.SetAttribute("text", File.ReadAllText("text.txt")); 
     doc.AppendChild(nodElement); 

会自动(使用XML转义码)进行转义的属性值...

<node text="&lt;xml xmlns:o=&quot;urn:schemas-microsoft-com:office:office&quot;&#xD;&#xA;xmlns:w=&quot;urn:schemas-microsoft-com:office:word&quot;&#xD;&#xA;xmlns:m=&quot;http://schemas.microsoft.com/office/2004/12/omml&quot;&#xD;&#xA;xmlns=&quot;http://www.w3.org/TR/REC-html40&quot;&gt;&#xD;&#xA;&#xD;&#xA;&lt;head&gt;&#xD;&#xA;&lt;meta http-equiv=Content-Type content=&quot;text/html; charset=utf-8&quot;&gt;" /> 
2

Different ways how to escape an XML string in C#

XML编码是必要的,如果你要保存XML文本一个XML文档。如果您不转义特殊字符,则要插入的XML将成为原始XML DOM的一部分,而不是节点的值。

转义XML意味着基本上用新值替换5个字符。

这些替代品是:

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

这里有4种方式,你可以在C#编码XML:

  1. string.Replace() 5 times

这是丑陋的,但它的工作原理。请注意,替换(“&”,“&”)必须是第一个替换,所以我们不会替换其他已经转义的&。

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; 
  • System.Web.HttpUtility.HtmlEncode()
  • 用于编码HTML,但HTML是XML的一种形式,所以我们可以使用这一点。主要用于ASP.NET应用程序。请注意,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; 
    
  • System.Security.SecurityElement.Escape()
  • 在Windows窗体或控制台应用我使用此方法。如果没有其他东西可以节省我在我的项目中包括System.Web引用,并且它编码所有5个字符。

    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; 
    
  • System.Xml.XmlTextWriter
  • 使用XmlTextWriter的你不必担心逃避任何事情,因为它避开了在需要的字符。例如,在属性中,它不会撇开撇号,而在节点值中,它不会逃脱撇号和qoutes。

    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> 
    */