动态生成xml内容,显示到页面上,在ie中查看,无法显示xml页,提示内容应该是编码有问题,不知道怎么解决
代码大概如下
XmlDocument xmldoc = new XmlDocument();
//声明
XmlDeclaration xmldecl;
xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null); ;
xmldecl.Encoding = "GB2312";
//加入一个根元素
XmlElement xmlelem = xmldoc.CreateElement("", "YFBGLIST", "");
xmldoc.AppendChild(xmlelem);
xmldoc.InsertBefore(xmldecl, xmlelem);
//加入另外一个元素
for (int i = 0; i < rsint; i )
{
string[] k = new string[k2];
k = rs[i].Split('@');
XmlNode root = xmldoc.SelectSingleNode("YFBGLIST");//查找<Employees>
XmlElement xe1 = xmldoc.CreateElement("YFBG");//创建一个<Node>节点
XmlElement xesub1 = xmldoc.CreateElement("yfbgtitle");
xesub1.InnerText = k[0];
xe1.AppendChild(xesub1);//添加到<Node>节点中
XmlElement xesub2 = xmldoc.CreateElement("researcher");
xesub2.InnerText = k[3];
xe1.AppendChild(xesub2);
XmlElement xesub3 = xmldoc.CreateElement("url");
xesub3.InnerText = k[4];
XmlElement xesub4 = xmldoc.CreateElement("content");
xesub4.InnerText = k[5];
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//
}
MemoryStream stream = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(stream, new System.Text.UTF8Encoding(false));
writer.Formatting = Formatting.Indented;
xmldoc.Save(writer);
StreamReader sr = new StreamReader(stream, new System.Text.UTF8Encoding(false));
stream.Position = 0;
string XMLString = sr.ReadToEnd();
return XMLString;
sr.Close();
stream.Close();
郎朗坤
牧羊人nacy
RISEBY
米琪卡哇伊