Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 仅转义XML中节点的内容_C#_.net_Xml_.net 2.0_C# 2.0 - Fatal编程技术网

C# 仅转义XML中节点的内容

C# 仅转义XML中节点的内容,c#,.net,xml,.net-2.0,c#-2.0,C#,.net,Xml,.net 2.0,C# 2.0,我有下面提到的部分代码 //Reading from a file and assign to the variable named "s" string s = "<item><name> Foo </name></item>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(s); ,标记()也会转义,因此会发生错误 如何解决此问题?假设您的内容永远不会包含字

我有下面提到的部分代码

    //Reading from a file and assign to the variable named "s"
    string s = "<item><name> Foo </name></item>";
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(s);
,标记(<,>)也会转义,因此会发生错误


如何解决此问题?

假设您的内容永远不会包含字符“]]>”,您可以使用CDATA

string s = "<item><name><![CDATA[ Foo > Bar ]]></name></item>";
string s=“Bar]]>”;
否则,您需要对特殊字符进行html编码,并在使用/显示它们之前对它们进行解码(除非它在浏览器中)

string s=“Foo-Bar”;

将字符串的内容分配给节点的
InnerXml
属性

 var node = doc.CreateElement("root");
 node.InnerXml = s;
看看-

一个棘手的解决方案:

    string s = "<item><name> Foo > Bar </name></item>";
    s = Regex.Replace(s, @"<[^>]+?>", m => HttpUtility.HtmlEncode(m.Value)).Replace("<","ojlovecd").Replace(">","cdloveoj");
    s = HttpUtility.HtmlDecode(s).Replace("ojlovecd", "&gt;").Replace("cdloveoj", "&lt;");
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(s);
string s=“Foo>Bar”;
s=Regex.Replace(s,@“]+?>”,m=>HttpUtility.HtmlEncode(m.Value)).Replace(“,”cdloveoj”);
s=HttpUtility.HtmlDecode.Replace(“ojlovecd”).Replace(“cdloveoj”);
XmlDocument doc=新的XmlDocument();
doc.LoadXml;

您生成的字符串似乎是字符串,而不是有效的XML。您可以获取作为有效XML生成的字符串,或者如果您知道字符串始终是名称,则不要在数据中包含XML
标记

然后,当您创建
XMLDocument
时。在重新保存结果之前,执行
CreateElement
并分配字符串

XmlDocument doc = new XmlDocument(); 
XmlElement root = doc.CreateElement("item");
doc.AppendChild(root);
XmlElement name = doc.CreateElement("name");
name.InnerText = "the contents from your file";
root.AppendChild(name);

正如我的评论所说,这些字符串是从文件中读取的。它们没有在我的代码中分配。我知道应该对内容进行编码,但文件中有很多内容,我想可能需要很多正则表达式来替换字符串中的所有内容。我不想做那些单调乏味的正则表达式检查。那么,我的问题是如何解决这个问题?对不起,有什么区别?除了有额外的根节点之外,它是相同的。无论如何,分配给节点时将遇到错误。Innerxml?不会吗?@Kai如果你已经有了DocumentElement(根节点),就不需要了。很难对无效的XML进行推理(它本质上是指示文本)。在继续这条路线之前,请想一想在下面的情况下您希望程序做什么…@AlexeiLevenkov同意Alexei的观点,但另一种思考方式是您应该尝试阻止“Foo>Bar”成为XML的一部分。在它被添加的时候,你可以简单地逃避它。XML无效后,情况就不同了。
 var node = doc.CreateElement("root");
 node.InnerXml = s;
    string s = "<item><name> Foo > Bar </name></item>";
    s = Regex.Replace(s, @"<[^>]+?>", m => HttpUtility.HtmlEncode(m.Value)).Replace("<","ojlovecd").Replace(">","cdloveoj");
    s = HttpUtility.HtmlDecode(s).Replace("ojlovecd", "&gt;").Replace("cdloveoj", "&lt;");
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(s);
XmlDocument doc = new XmlDocument(); 
XmlElement root = doc.CreateElement("item");
doc.AppendChild(root);
XmlElement name = doc.CreateElement("name");
name.InnerText = "the contents from your file";
root.AppendChild(name);