帮助:XMLDocument不会对双引号进行编码 [英] Help: XMLDocument won't encode double quot

查看:60
本文介绍了帮助:XMLDocument不会对双引号进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在以下代码段中:

In the code snippet below:

myValue = "hello""world & me"
myNode = xmlDoc.CreateElement("Hello")
myText = xmlDoc.CreateTextNode(myValue.replace("""","&quo t;"))
myNode.AppendChild(myText)



如果对于articleText = doc.CreateTextNode(myValue.replace("",& quo t;"))
结果是:您好& quot;世界&我

如果对于articleText = doc.CreateTextNode(myValue)
结果是:您好"世界与我

要求是:双引号必须正确编码

注意我在双引号的编码值中添加了空格,以防止预览时在代码项目中将其转换为双引号. :)



If for articleText = doc.CreateTextNode(myValue.replace("""","& quo t;"))
result is: hello"world & me

If for articleText = doc.CreateTextNode(myValue)
result is: hello"world & me

The requirements is that: the double quote must be encoded correctly

Notei have added spaces in the encoded value of the double quote to prevent it from conversion here in code project when preview. :)

推荐答案

这对我来说很好.尝试它希望对您有帮助

This is working fine for me. Try it hope it helps

XmlDocument xmlDoc = new XmlDocument();
            string   myValue = '"'+ "Hello" + '"' + '"'+"World & me " +'"';
            XmlNode myNode = xmlDoc.CreateElement("Hello");
            myNode.InnerText = myValue;
            xmlDoc.AppendChild(myNode);
            xmlDoc.Save("c:/test-doc.xml");


.
.
.
doc.Save("MySampleXML.xml");

//Open the saved xml file as text and update the " to "
System.TextStringBuilder newFile = new System.Text.StringBuilder();
string tempString = "";
string []fileString = File.ReadAllLines("MySampleXML.xml");
foreach(string line in fileString)
{
    if (line.Contains("& amp;quot;"))
    {
        tempString = line.Replace("& amp;quot;","& quot;");
        newFile.AppendLine(tempString);
        continue for;
    }
}
File.WriteAllText("MySampleXML.xml", newFile.ToString());



注意:请删除&之间的空格.和".我增加了空间,所以codeproject不会对其进行编码.不知道如何禁用它... hehehe:)



Note: Please remove the space between & and amp;quot;. I added space so codeproject won''t encode it. Don''t know how to disable it... hehehe :)


这篇关于帮助:XMLDocument不会对双引号进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆