首页 新闻 会员 周边 捐助

c# 生成xml,显示问题。请高手赐教。

0
悬赏园豆:10 [已解决问题] 解决于 2012-12-31 17:28

 

生成xml是,怎么生成的代码是对的,页面显示不对,右键查看源代码,存储为xml文件,就能正常显示,但是把生成的xml文件,直接response.write(xml.tostring()) 出来,就如图片显示,把所有的xml内容显示出来了,没有节点呢。  分析可能是以字符窜方式解析的,问不是xml。请高手赐教。一下是生成的源代码:

是用改了XmlDocument 和XmlTextWriter两种方法生成,输出结果都一样。我还试验了一下,保存一个文件,然后在读取文件,显示结果还是如图片,所以请高手赐教。

 

try
        {
            DataSetWebService GetService = new DataSetWebService();
            Hashtable SqlCollection = new Hashtable();
            SqlCollection.Add("ToAccess", "SELECT * from news where date between #" + DateTime.Now.AddMonths(-6).ToShortDateString() + "# and #" + DateTime.Now.ToShortDateString() + "#");
            DictionaryEntry[] _DictEntry = new DictionaryEntry[SqlCollection.Count];
            SqlCollection.CopyTo(_DictEntry, 0);
            String _Context = ClsCommon.Serializer<DictionaryEntry[]>(_DictEntry);
            DataSet ds = GetService.GetDataSet(_Context);

            string linkStr = "";
            string cssStr = "";
            DataSet dt = GetService.GetDataTable("ToHKIE_APP_CMT", "select * from tbl_XmlSetup where DbName='ToAccess'");
            if (dt != null && dt.Tables[0].Rows.Count > 0)
            {
                linkStr = dt.Tables[0].Rows[0]["Link"].ToString();
                cssStr = dt.Tables[0].Rows[0]["CssContent"].ToString();
            }

            //方法一,用XmlDocument生成
            XmlDocument xmlDoc = new XmlDocument();
            XmlDeclaration declarNode = xmlDoc.CreateXmlDeclaration("1.0", Encoding.Default.ToString(), "yes");
            xmlDoc.AppendChild(declarNode);
            XmlComment xmlCom = xmlDoc.CreateComment("Copyright Innoways.com ");
            xmlDoc.AppendChild(xmlCom);
            XmlNode rootNode = xmlDoc.CreateElement("NEWES");
            if (ds != null)
            {
                for (int i = 0; i < ds.Tables.Count; i++)
                {
                    foreach (DataRow row in ds.Tables[i].Rows)
                    {
                        XmlNode newNode = xmlDoc.CreateElement("NEWS");
                        XmlNode newIDNode = xmlDoc.CreateElement("NEWS_ID");
                        newIDNode.InnerText = row["serialno"].ToString();
                        newNode.AppendChild(newIDNode);

                        XmlNode dateNode = xmlDoc.CreateElement("DATE");
                        dateNode.InnerText = ClsCommon.ConvertDateTimeToString(row["date"], "dd.MMM.yyyy");
                        newNode.AppendChild(dateNode);

                        XmlNode updateDateNode = xmlDoc.CreateElement("UPDATE_DATE");
                        updateDateNode.InnerText = ClsCommon.ConvertDateTimeToString(row["update"], "dd.MMM.yyyy");
                        newNode.AppendChild(updateDateNode);

                        XmlNode deadDateNode = xmlDoc.CreateElement("DEAD_DATE");
                        deadDateNode.InnerText = ClsCommon.ConvertDateTimeToString(row["deaddate"], "dd.MMM.yyyy");
                        newNode.AppendChild(deadDateNode);

                        XmlNode headLineNode = xmlDoc.CreateElement("HEADLINE");
                        XmlCDataSection headLineCdata = xmlDoc.CreateCDataSection(CommonMethods.AddHtmlCode(row["headline"].ToString(), linkStr, linkStr));
                        headLineNode.AppendChild(headLineCdata);
                        newNode.AppendChild(headLineNode);

                        XmlNode htmlNode = xmlDoc.CreateElement("HTML");
                        XmlCDataSection htmlCdata = xmlDoc.CreateCDataSection(CommonMethods.AddHtmlCode(cssStr, row["desc"].ToString(), linkStr, linkStr));
                        htmlNode.AppendChild(htmlCdata);
                        newNode.AppendChild(htmlNode);

                        XmlNode logoNode = xmlDoc.CreateElement("LOGO");
                        XmlCDataSection logoCdata = xmlDoc.CreateCDataSection("http://demo.i-wanna.com/hkie_apps_xml/material/testing_default_logo.jpg");  //对应的logo地址
                        logoNode.AppendChild(logoCdata);
                        newNode.AppendChild(logoNode);

                        XmlNode activeNode = xmlDoc.CreateElement("ACTIVE");
                        activeNode.InnerText = row["active"].ToString();
                        newNode.AppendChild(activeNode);
                        rootNode.AppendChild(newNode);
                    }
                }
            }
            xmlDoc.AppendChild(rootNode);

            MemoryStream stream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(stream, Encoding.Default);
            writer.Formatting = Formatting.Indented;
            xmlDoc.Save(writer);
            StreamReader reader = new StreamReader(stream, Encoding.Default);
            stream.Position = 0;
            String content = reader.ReadToEnd();
            reader.Close();
            stream.Close();
            Response.Write(content);
            ds.Dispose();
            dt.Dispose();


              

            //方法二,用XmlTextWriter生成
            //XmlTextWriter xmlWrite = new XmlTextWriter(Response.OutputStream, Encoding.Default);
            //xmlWrite.WriteStartDocument();
            //xmlWrite.Formatting = Formatting.Indented;
            //xmlWrite.WriteComment("Copyright Innoways.com ");
            //xmlWrite.WriteStartElement("NEWES");
            //if (ds != null)
            //{
            //    for (int i = 0; i < ds.Tables.Count; i++)
            //    {
            //        foreach (DataRow row in ds.Tables[i].Rows)
            //        {
            //            xmlWrite.WriteStartElement("NEWS");
            //            xmlWrite.WriteElementString("NEWS_ID", row["serialno"].ToString());
            //            xmlWrite.WriteElementString("DATE", ClsCommon.ConvertDateTimeToString(row["date"], "dd.MMM.yyyy"));
            //            xmlWrite.WriteElementString("UPDATE_DATE", ClsCommon.ConvertDateTimeToString(row["update"], "dd.MMM.yyyy"));
            //            xmlWrite.WriteElementString("DEAD_DATE", ClsCommon.ConvertDateTimeToString(row["deaddate"], "dd.MMM.yyyy"));

            //            xmlWrite.WriteRaw(@"<HEADLINE>" + CommonMethods.AddHtmlCode(cssStr, row["headline"].ToString(), linkStr, linkStr) + "</HEADLINE>");
            //            //xmlWrite.WriteRaw("<HTML>" + CommonMethods.AddHtmlCode(cssStr, row["desc"].ToString(), linkStr, linkStr) + "</HTML>");
            //            //xmlWrite.WriteRaw("<LOGO>" + CommonMethods.AddCDATA("http://demo.i-wanna.com/hkie_apps_xml/material/testing_default_logo.jpg") + "</LOGO>");

            //            //xmlWrite.WriteElementString("HEADLINE", CommonMethods.AddHtmlCode(cssStr, row["headline"].ToString(), linkStr, linkStr));
            //            //xmlWrite.WriteElementString("HTML", CommonMethods.AddHtmlCode(cssStr, row["desc"].ToString(), linkStr, linkStr));
            //            //xmlWrite.WriteElementString("LOGO", CommonMethods.AddCDATA("http://demo.i-wanna.com/hkie_apps_xml/material/testing_default_logo.jpg")); //暂无此字段,等客户加上

            //            xmlWrite.WriteElementString("ACTIVE", row["active"].ToString());
            //            xmlWrite.WriteEndElement();
            //        }
            //    }
            //}

            //xmlWrite.WriteEndElement();
            //xmlWrite.WriteEndDocument();

            //xmlWrite.Flush();
            //xmlWrite.Close();
            //doc.Load("new.xml");
            //Response.Write(doc.);
            // File.Delete("new.xml");
            //Stream sm = xmlWrite.BaseStream;
            //XmlDocument doc = new XmlDocument();
            //doc.Load(sm);
            //string s = doc.InnerText;
            //ds.Dispose();
            //dt.Dispose();
        }
        catch (Exception ex)
        {

        }

blue_sky_alf的主页 blue_sky_alf | 初学一级 | 园豆:72
提问于:2012-12-31 16:09
< >
分享
最佳答案
0

设置一下Response.ContentType吧

收获园豆:10
I,Robot | 大侠五级 |园豆:9783 | 2012-12-31 17:07

大哥啊,你是我的亲人,我改来改去,改了一天,就是没想到输出类型设置为xml的,改了一下果然好了。但是你们两位都回答对了,我该给谁分你,你回答的早,给你了,谢谢哈。

blue_sky_alf | 园豆:72 (初学一级) | 2012-12-31 17:27
其他回答(1)
0

恩是,设置一下Response.ContentType

妍珊 | 园豆:1169 (小虾三级) | 2012-12-31 17:08

非常谢谢你,真心的感谢你的回答,谢谢,但是楼上回答的早,只能给他了,谢谢你,好人总有好报~!

支持(0) 反对(0) blue_sky_alf | 园豆:72 (初学一级) | 2012-12-31 17:27

@blue_sky_alf: 没事,可以加关注,有问题随时交流嘛是不

支持(0) 反对(0) 妍珊 | 园豆:1169 (小虾三级) | 2013-01-04 10:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册