网站建设资讯

NEWS

网站建设资讯

生成和读取XML文件的方法

自己根据网上现有的代码,改写而成的。

公司主营业务:成都网站制作、成都网站建设、移动网站开发等业务。帮助企业客户真正实现互联网宣传,提高企业的竞争能力。成都创新互联公司是一支青春激扬、勤奋敬业、活力青春激扬、勤奋敬业、活力澎湃、和谐高效的团队。公司秉承以“开放、自由、严谨、自律”为核心的企业文化,感谢他们对我们的高要求,感谢他们从不同领域给我们带来的挑战,让我们激情的团队有机会用头脑与智慧不断的给客户带来惊喜。成都创新互联公司推出万年免费做网站回馈大家。

生成XML文件的代码:

///


        ///
        ///

        /// 需要传的参数
        public static void getXml(DataTable dt)
        {
            string path = System.Web.HttpContext.Current.Server.MapPath("~/XML//");//生成文件所在的位置
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path += "Plant.xml";//文件名称
            exportToXml(path, dt);
        }
        ///
        /// 生成XML文件
        ///

        /// 文件路径
        /// 数据源
        public static void exportToXml(string filename, DataTable dt)
        {
            StreamWriter sr = new StreamWriter(filename, false, System.Text.Encoding.UTF8);
            //写文件头部内容
            //下面是生成RSS的OPML文件
            sr.WriteLine("");
            sr.WriteLine("");
            sr.WriteLine("1");
            int i = 0;
           
            if (dt.Rows.Count > 0)
            {
                for (int k = 0; k < dt.Rows.Count;k++ )
                {
                    i++;
                    if (k == 0)
                    {
                        sr.WriteLine("");
                        sr.WriteLine("" + i + "");
                        sr.WriteLine("" + Convert.ToDateTime(dt.Rows[k]["Addtime"].ToString()).ToLongDateString() + "");
                        string shen = "";
                        string unit = "";
                        string m = dt.Rows[k]["Height"].ToString();
                        Regex reg = new Regex(@"[^0-9,^.]+");
                        Regex reg2 = new Regex(@"[^A-Za-z]+");
                        shen = reg.Replace(m, "");
                        unit = reg2.Replace(m, "");
                        sr.WriteLine(""+unit+"");
                        sr.WriteLine("" + shen + "");
                        sr.WriteLine("
");
                    }
                    else
                    {
                        sr.WriteLine("");
                        sr.WriteLine("" + i + "");
                        sr.WriteLine("" + Convert.ToDateTime(dt.Rows[k-1]["Updatetime"].ToString()).ToLongDateString() + "");

                        string shen = "";
                        string unit = "";
                        string m = dt.Rows[k]["Height"].ToString();
                        Regex reg = new Regex(@"[^0-9,^.]+");
                        Regex reg2 = new Regex(@"[^A-Za-z]+");
                        shen = reg.Replace(m, "");
                        unit = reg2.Replace(m, "");
                        sr.WriteLine("" + unit + "");
                        sr.WriteLine("" + shen + "");
                        sr.WriteLine("");
                    }
                }
            }

            //写文件尾部内容
            sr.WriteLine("");
            sr.WriteLine("");
            sr.Close();
        }

读取xml文件的代码:

///


        /// 获取xml中的数据()
        ///

        /// x轴数据
        /// y轴数据
        /// 单位
        public static void getXML(string xdata, string ydata, string unit)
        {
            XmlDocument myDoc = new XmlDocument();
            myDoc.Load(System.Web.HttpContext.Current.Server.MapPath("~/XML//Plant.xml"));

            XmlNode xn = myDoc.SelectSingleNode("//VoteInfo[ID='1']");//读取数据源

            XmlNodeList xnl = xn.SelectNodes("Item");

            for (int i = 0; i < xnl.Count; i++)
            {
                XmlNode xn0 = xnl.Item(i);
                ydata += xn0.SelectSingleNode("Count").InnerText + ",";
                xdata += "\'" + xn0.SelectSingleNode("Title").InnerText + "\'" + ",";
                unit = xn0.SelectSingleNode("Unit").InnerText;
            }
            ydata = ydata.Remove(ydata.Length - 1, 1);
            xdata = xdata.Remove(xdata.Length - 1, 1);
        }


当前名称:生成和读取XML文件的方法
标题路径:http://njwzjz.com/article/pjeehe.html