生成和读取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);
        }

创新互联www.cdcxhl.cn,专业提供香港、美国云服务器,动态BGP最优骨干路由自动选择,持续稳定高效的网络助力业务部署。公司持有工信部办法的idc、isp许可证, 机房独有T级流量清洗系统配攻击溯源,准确进行流量调度,确保服务器高可用性。佳节活动现已开启,新人活动云服务器买多久送多久。


当前标题:生成和读取XML文件的方法-创新互联
URL分享:http://azwzsj.com/article/dhshgp.html