c# 创建、读取、添加、修改xml文件

添加命名空间
usingSystem.Xml;

C#读取已有XML文件并保存

    XmlDocumentxmlDoc = new XmlDocument();
   xmlDoc.Load("D:\\xmlname.xml");
    XmlNodexnRoot = xmlDoc.DocumentElement;//获取唯一表示的根节点.
    stringfirstchildname=xnRoot.FirstChild.Name.ToString();
    if(firstchildname == "firstelementname")
    {
        int num=100;
        xnRoot.FirstChild.InnerText =num.ToString();//向第一个节点赋值,注意xml接受的是字符型
        xmlDoc.Save("D:\\xmlname.xml");
    }
   //修改根节点下节点的值
    XmlNodeListxnlUserInfo = xnRoot.ChildNodes;//获取根节点的子节点.
   xnRoot.FirstChild.InnerText=....;
   xnlUserInfo[1].InnerText=....;
   xnlUserInfo[2].InnerText=....;
    .....
   xnRoot.LastChild.InnerText=....;

           //循环查找根节点下的节点
            foreach (XmlNode xnTheUser in xnlUserInfo)
            {
               XmlNodeList xnlUserSub = xnTheUser.ChildNodes;
              //子节点下的节点
               foreach (XmlNode xnSub in xnlUserSub)
               {
                   this.text_second.Text=xnSub.InnerText;//显示子节点点文本
                }
                this.text_second.Text = xnTheUser.InnerText;
             }
C#创建XML文件并保存
               XmlDocument xmlDoc= new XmlDocument();
               XmlNode[] xmlnode = new XmlNode[n];
               XmlElement[] xmlelem = new XmlElement[n];
               XmlText xmltext;

               xmlnode[0] = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "","");
               xmlDoc .A(xmlnode[0]);
               //创建根节点
               xmlelem[0] = xmlDoc.createElement("", "Root", "");
               xmltext = xmlDoc.CreateTextNode("");
               xmlelem[0].A(xmltext);
               xmlDoc.A(xmlelem[0]);

               xmlelem[1] = xmlDoc.createElement("", "firstelementname", "");
               xmltext = xmlDoc.CreateTextNode("firstelementvalue");
               xmlelem[1].A(xmltext);
               xmlDoc.ChildNodes.Item(1).A(xmlelem[1]);

               xmlelem[2] = xmlDoc.createElement("", "secondelementname", "");
               xmltext = xmlDoc.CreateTextNode("secondelementvalue");
               xmlelem[2].A(xmltext);
               xmlDoc.ChildNodes.Item(1).A(xmlelem[2]);

               xmlelem[3] = xmlDoc.createElement("", "thirdelementname", "");
               xmltext = xmlDoc.CreateTextNode("thirdelementvalue");
               xmlelem[3].A(xmltext);
               xmlDoc.ChildNodes.Item(1).A(xmlelem[3]);

               xmlelem[4] = xmlDoc.createElement("", "forthelementname", "");
               xmltext = xmlDoc.CreateTextNode("forthelementvalue");
               xmlelem[4].A(xmltext);
               xmlDoc.ChildNodes.Item(1).A(xmlelem[4]);
               ......
               //保存xml文件
               xmlDoc.Save("xmlname.xml");//保存于工程根目录

C#判断xml文件是否存在
添加命名空间
using System.IO;

if (File.Exists("xmlname.xml"))
{
    if(MessageBox.Show("存在xml文件,是否读取!","提示",
   MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load("xmlname.xml");
    }
}


http://hi.baidu.com/brotherzhuo/blog/item/40621f0c75bccf980b7b8254.html
http://blog.chinaunix.net/space.php?uid=22123801&do=blog&id=403143

你可能感兴趣的:(其他)