asp.net动态生成xml编码问题

动态生成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();

哆啦的时光机
浏览 405回答 4
4回答

郎朗坤

xmldecl = xmldoc.CreateXmlDeclaration("1.0", "UTF-8", null); ;xmldecl.Encoding = "GB2312"; 将xmldecl.Encoding改为UTF-8试试。

牧羊人nacy

你的编码方式是GB2312的,如果你的浏览器支持的是UTF-8就会乱码了(一般默认为UTF-8) 所以调整方式有两种 1 将浏览器支持GB2312 2 将你的页面输出为UTF-8的

RISEBY

我一般是用StringBuilder拼的   没有出过问题

米琪卡哇伊

看看xml的命名空间有问题不
打开App,查看更多内容
随时随地看视频慕课网APP