简体   繁体   English

将XML设置为XML节点属性的值

[英]Set XML as value of an XML node attribute

I'm trying to create an XML document in C# , which in one of attribute will get another XML as value: 我正在尝试在C#创建一个XML文档,在其中一个属性中将另一个XML作为值:

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

MyXMLToInsert would be somthing like this: 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">
.
.

How can I prevent the second XML's special characters not to conflict with the main ones? 如何防止第二个XML的特殊字符与主要字符冲突? Thanks. 谢谢。

Different ways how to escape an XML string in C# 如何在C#中转义XML字符串的不同方法

XML encoding is necessary if you have to save XML text in an XML document. 如果必须在XML文档中保存XML文本,则必须使用XML编码。 If you don't escape special chars the XML to insert will become a part of the original XML DOM and not a value of a node. 如果不转义特殊字符,则要插入的XML将成为原始XML DOM的一部分,而不是节点的值。

Escaping the XML means basically replacing 5 chars with new values. 转义XML意味着基本上用新值替换5个字符。

These replacements are: 这些替代品是:

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

Here are 4 ways you can encode XML in C#: 以下是使用C#编码XML的4种方法:

  1. string.Replace() 5 times

This is ugly but it works. 这很丑,但它确实有效。 Note that Replace("&", "&") has to be the first replace so we don't replace other already escaped &. 请注意,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;
  1. System.Web.HttpUtility.HtmlEncode()

Used for encoding HTML, but HTML is a form of XML so we can use that too. 用于编码HTML,但HTML是XML的一种形式,因此我们也可以使用它。 Mostly used in ASP.NET apps. 主要用于ASP.NET应用程序。 Note that HtmlEncode does NOT encode apostrophes ( ' ). 请注意,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;
  1. System.Security.SecurityElement.Escape()

In Windows Forms or Console apps I use this method. 在Windows窗体或控制台应用程序中,我使用此方法。 If nothing else it saves me including the System.Web reference in my projects and it encodes all 5 chars. 如果没有别的东西它可以节省我,包括我的项目中的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;
  1. System.Xml.XmlTextWriter

Using XmlTextWriter you don't have to worry about escaping anything since it escapes the chars where needed. 使用XmlTextWriter,您不必担心转义任何内容,因为它会在需要的地方转义字符。 For example in the attributes it doesn't escape apostrophes, while in node values it doesn't escape apostrophes and qoutes. 例如,在属性中它不会转义撇号,而在节点值中它不会转义撇号和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>
*/

Calling the SetAttribute method will take care of escaping the data. 调用SetAttribute方法将负责转义数据。

Say you read the content of MyXMLToInsert from a file "Text.txt" located in the root directory of your application. 假设您从位于应用程序根目录中的文件“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);

The value of the attribute would automatically be escaped (using XML escape codes) to... 属性的值将自动转义(使用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;" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM