2015-05-09 116 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> 
    */