CSDN博客

img rose001

xml操作

发表于2008/9/28 17:32:00  156人阅读

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Xml;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace WebApplication2
{
    public class xml
    {
        public void SetXml(string name, string city, string sex, string addr)
        {

            string fileName = "D://user.xml";

            XmlDocument xmlDoc = new XmlDocument();
            // 文件已经存在,加载该xml文件
            if (File.Exists(fileName))
            {
                xmlDoc.Load(fileName);
                XmlNode xn1 = xmlDoc.SelectSingleNode("user");
                XmlNodeList xnl = xn1.ChildNodes;
                foreach (XmlNode xn in xnl)
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.Name == "name")
                    {
                        xe.InnerText = name;
                        continue;
                    }//end if (xe.Name == "name")
                    if (xe.Name == "city")
                    {
                        xe.InnerText = city;
                        continue;
                    }//end if (xe.Name == "city")
                    if (xe.Name == "sex")
                    {
                        xe.InnerText = sex;
                        continue;
                    }//end if (xe.Name == "sex")
                    if (xe.Name == "addr")
                    {
                        xe.InnerText = addr;
                        continue;
                    }//end if (xe.Name == "addr")
                  
                }//end foreach (XmlNode xn in xnl)
                xmlDoc.Save(fileName);
            }// end if (File.Exists(fileName))
            else  //文件不存在,创建该xml
            {
                //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
                XmlDeclaration xmldecl = xmlDoc.CreateXmlDeclaration("1.0", "gb2312", null);
                xmlDoc.AppendChild(xmldecl);

                //加入根元素configuration
                XmlElement xmlelem = xmlDoc.CreateElement("", "user", "");
                xmlDoc.AppendChild(xmlelem);

                //创建节点
                XmlNode root = xmlDoc.SelectSingleNode("user");
                //创建一个<name>节点
                XmlElement xe1 = xmlDoc.CreateElement("name");
                XmlText xt1 = xmlDoc.CreateTextNode(name);
                xe1.AppendChild(xt1);
                //添加到<user>节点中
                root.AppendChild(xe1);
                //城市
                XmlElement xe2 = xmlDoc.CreateElement("city");
                XmlText xt2 = xmlDoc.CreateTextNode(city);
                xe2.AppendChild(xt2);
                root.AppendChild(xe2);
                //性别
                XmlElement xe3 = xmlDoc.CreateElement("sex");
                XmlText xt3 = xmlDoc.CreateTextNode(sex);
                xe3.AppendChild(xt3);
                root.AppendChild(xe3);
                //地址
                XmlElement xe4 = xmlDoc.CreateElement("addr");
                XmlText xt4 = xmlDoc.CreateTextNode(addr);
                xe4.AppendChild(xt4);
                root.AppendChild(xe4);

                //保存创建好的XML文档
                xmlDoc.Save(fileName);
                xmlDoc.Load(fileName);
            }
        }

        private void GetXml()
        {
            string fileName = "D://user.xml";
            XmlDocument xmlDoc = new XmlDocument();
            // 文件已经存在,加载该xml文件
            if (File.Exists(fileName))
            {
                xmlDoc.Load(fileName);
                XmlNode xn1 = xmlDoc.SelectSingleNode("user");
                XmlNodeList xnl = xn1.ChildNodes;
                foreach (XmlNode xn in xnl)
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.Name == "name")
                    {
                        this.cmbname.Text = xe.InnerText;
                        continue;
                    }//end if (xe.Name == "name")
                    if (xe.Name == "city")
                    {
                        this.txtcity.Text = xe.InnerText;
                        continue;
                    }//end if (xe.Name == "city")
                    if (xe.Name == "sex")
                    {
                        this.txtsex.Text = xe.InnerText;
                        continue;
                    }//end if (xe.Name == "sex")
                    if (xe.Name == "addr")
                    {
                        this.txtaddr.Text = xe.InnerText;
                        continue;
                    }//end if (xe.Name == "addr")
                }//end foreach (XmlNode xn in xnl)
            }//end if (File.Exists(fileName))
        }

    }
}

0 0

相关博文

我的热门文章

img
取 消
img