有人说,xml是水,是空气,程序员当然也离不开水和空气。
请看下面的一个例子:
------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<article>
<author>张三</author>
<body><![CDATA[李四€]]></body>
</article>
------------------------------------------------
死活报错(包含有十六进制字符)。有人说用body Base64加密,这样每次加载时还要解密。
从性能的角度,这样值吗?从google中搜了n条,没有找到答案。
请看下面的一个例子:
------------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<article>
<author>张三</author>
<body><![CDATA[李四€]]></body>
</article>
------------------------------------------------
死活报错(包含有十六进制字符)。有人说用body Base64加密,这样每次加载时还要解密。
从性能的角度,这样值吗?从google中搜了n条,没有找到答案。
如果少量信息,自己加密后保存成文件也可以的啊
帮UP~~
xmlDocument.LoadXml("<?xml version= \"1.0\" encoding= \"UTF-8\"?> <article></article>");
XmlNode node = xmlDocument.SelectSingleNode("descendant::article");
XmlElement temp = xmlDocument.CreateElement("author");
XmlText temptext = xmlDocument.CreateTextNode("张三");
temp.AppendChild(temptext);
node.AppendChild(temp); temp = xmlDocument.CreateElement("body");
temptext = xmlDocument.CreateTextNode("李四€");
temp.AppendChild(temptext);
node.AppendChild(temp);RETURN <?xml version="1.0" encoding="UTF-8" ?>
<article>
<author>张三</author>
<body>李四€</body>
</article>
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml("<?xml version= \"1.0\" encoding= \"UTF-8\"?> <article></article>");
XmlNode node = xmlDocument.SelectSingleNode("descendant::article");
XmlElement temp = xmlDocument.CreateElement("author");
XmlText temptext = xmlDocument.CreateTextNode("张三");
temp.AppendChild(temptext);
node.AppendChild(temp); temp = xmlDocument.CreateElement("body");
temptext = xmlDocument.CreateTextNode("<![CDATA[李四€]]>");
temp.AppendChild(temptext);
node.AppendChild(temp);RETURN
string:<?xml version="1.0" encoding="UTF-8"?><article><author>张三</author><body><![CDATA[李四€]]></body></article><?xml version="1.0" encoding="UTF-8"?><article><author>张三</author><body><![CDATA[李四€]]></body></article>
<article>
<author>张三</author>
<body><![CDATA[李四€]]></body>
</article>
<article>
<author>张三</author>
<body><![CDATA[李四€]]></body>
</article>还是张三?
要是张三可以用我的方法Load要是XML请用我的方法LOAD完存入数据库..
谢谢大家的回答。
如果要支持ASCII码里的字符,不知有没有比较好的解决方案?
<article>
<author> 张三 </author>
<body> <![CDATA[李四€]]> </body>
</article>
-----------------------------------------------
报错就对了,不报错那就麻烦了,8位二进制不可以表示汉字的,那必须用16位的
如下<?xml version= "1.0 " encoding= "utf-16"?>
<article>
<author> 张三 </author>
<body> <![CDATA[李四€]]> </body>
</article>
document.Load("test.xml");还是报"无效字符"的错误,这可能是xmlDocument不支持ASCII的原因。
看来不能用XmlDocument
<article>
<author> 张三 </author>
<body> <![CDATA[李四€]]> </body>
</article>执行通过
那你说人家xml是怎么存图片,怎么存音乐的?base64
CDATA 节中的内容必须在 XML 内容允许的字符范围内;控制字符和兼容性字符不能通过这种方式转义。
这是MSDN上的说明。所以lz为什么一定要在CData里写这些字符呢?在普通节点里写是没有问题的。
<article>
<author> 张三 </author>
<body> <![CDATA[李四€]]> </body>
</article> private string ConvertHex(string responseXml)
{
UTF8Encoding encoding = new UTF8Encoding();
Byte[] responseByteArr = encoding.GetBytes(responseXml);
for (int i = 0; i < responseByteArr.Length; i++)
{
if (responseByteArr[i] < 32)
{
responseByteArr[i] = 32;
}
}
return encoding.GetString(responseByteArr);
} protected void Page_Load(object sender, EventArgs e)
{
string FileName = @"D:\Solution\test-utf8.xml";
string xmlstr = "";
string[] s = null;
StringBuilder temp = new StringBuilder();
XmlDocument doc = new XmlDocument();
try{//直接加载xml
doc.Load(FileName);
}catch{//xml可能有十六进制字符,过滤掉这些这符再加载xml字符串
s = File.ReadAllLines(FileName);
for (int i = 0; i < s.Length; i++)
{
temp.Append(s[i]);
}
xmlstr = ConvertHex(temp.ToString());
doc.LoadXml(xmlstr);//重新加载LoadXml not Load
}finally {
}
XmlNodeReader reader = new XmlNodeReader(doc);
Response.Write("作者:");
Response.Write(doc.SelectSingleNode("/article/author").InnerText);
Response.Write("<br><br><br>正文:<br>");
Response.Write(doc.SelectSingleNode("/article/body").InnerText);
}