110,534
社区成员
发帖
与我相关
我的任务
分享
<?xml version= "1.0" encoding= "UTF-8"?>
<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);
}
<?xml version="1.0" encoding="UTF-8" ?>
<article>
<author>张三</author>
<body><![CDATA[李四€]]></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);
<?xml version="1.0" encoding="UTF-8"?><article><author>张三</author><body><![CDATA[李四€]]></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("李四€");
temp.AppendChild(temptext);
node.AppendChild(temp);
<?xml version="1.0" encoding="UTF-8" ?>
<article>
<author>张三</author>
<body>李四€</body>
</article>