C#中对XML文件的一些基本操作


//定义XMLDocument
            XmlDocument xmlDocument = new XmlDocument();
            
//定义XML文档头文件

            XmlDeclaration xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0","utf-8",null);
            
//增加XML文档头

            xmlDocument.AppendChild(xmlDeclaration);
            
//定义XML的根

            XmlElement xmlRoot = xmlDocument.CreateElement("userdata");
            
//添加根的属性

            xmlRoot.SetAttribute("createuser","true");
            
//修改根属性的值

            xmlRoot.GetAttributeNode("createuser").Value = "false";
            
//添加子节点并设置子节点属性

            xmlDocument.AppendChild(xmlRoot);
            XmlElement dataconnection 
= xmlDocument.CreateElement("dataconnection"
);
            XmlElement server 
= xmlDocument.CreateElement("server"
);
            server.InnerText 
= "localhost"
;
            XmlElement uid 
= xmlDocument.CreateElement("uid"
);
            uid.InnerText 
= "sa"
;
            XmlElement pwd 
= xmlDocument.CreateElement("pwd"
);
            xmlRoot.AppendChild(dataconnection);
            dataconnection.AppendChild(server);
            dataconnection.AppendChild(uid);
            dataconnection.AppendChild(pwd);
            
//保存XML文档

            xmlDocument.Save("book.xml");
            
//读取子节点server的值

            XmlNode xnserver = xmlDocument.SelectSingleNode("userdata/dataconnection/server");
            Console.WriteLine(
"node server's value is "+
 xnserver.InnerText);
            Console.ReadLine();

生成的XML代码如下:

 


<?xml version="1.0" encoding="utf-8"?>
<userdata createuser="false">
  
<dataConnection>
    
<server>localhost</server>
    
<uid>sa</uid>
    
<pwd />
  
</dataConnection>
</userdata>

 

 

代码
这是以前自己做过的一个测试。楼主可以参考一下。

======================= bookstore.XML内容 ====================
<? xml version = " 1.0 "  encoding = " gb2312 " ?>
< bookstore >
  
< book >
  
</ book >
  
< book >
  
</ book >
  
< book genre = " 李赞红 "  ISBN = " 2-3631-4 " >
    
< title > CS从入门到精通 </ title >
    
< author > 候捷 </ author >
    
< price > 58.3 </ price >
  
</ book >
  
< book genre = " 李赞红 "  ISBN = " 2-3631-4 " >
    
< title > CS从入门到精通 </ title >
    
< author > 候捷 </ author >
    
< price > 58.3 </ price >
  
</ book >
</ bookstore >

======================= 读取 ========================
            XmlDocument doc 
=   new  XmlDocument();
            doc.Load(
@" d:\bookstore.xml " );
            XmlElement root 
=  doc.DocumentElement;


            MessageBox.Show(root.SelectNodes(
" book " )[ 0 ].InnerText);

======================= 添加 =========================
            XmlDocument xmlDoc 
=   new  XmlDocument();
            xmlDoc.Load(
@" d:\bookstore.xml " );
            XmlNode root 
=  xmlDoc.SelectSingleNode( " bookstore " ); // 查找<bookstore>
            XmlElement xe1  =  xmlDoc.CreateElement( " book " ); // 创建一个<book>节点
            xe1.SetAttribute( " genre " " 李赞红 " ); // 设置该节点的genre属性
            xe1.SetAttribute( " ISBN " " 2-3631-4 " ); // 设置该节点的ISBN属性

            XmlElement xesub1 
=  xmlDoc.CreateElement( " title " ); // 添加一个名字为title的子节点
            xesub1.InnerText  =   " CS从入门到精通 " ; // 设置文本
            xe1.AppendChild(xesub1); // 把title添加到<book>节点中

            XmlElement xesub2 
=  xmlDoc.CreateElement( " author " );
            xesub2.InnerText 
=   " 候捷 " ;
            xe1.AppendChild(xesub2);

            XmlElement xesub3 
=  xmlDoc.CreateElement( " price " );
            xesub3.InnerText 
=   " 58.3 " ;
            xe1.AppendChild(xesub3);

            root.AppendChild(xe1);
// 把book添加到<bookstore>根节点中
            xmlDoc.Save( @" d:\bookstore.xml " );

====================== 修改 ========================
            XmlDocument xmlDoc 
=   new  XmlDocument();
            xmlDoc.Load(
@" d:\bookstore.xml " );
            XmlNodeList nodeList 
=  xmlDoc.SelectSingleNode( " bookstore " ).ChildNodes; // 获取bookstore节点的所有子节点
             foreach  (XmlNode xn  in  nodeList) // 遍历所有名字为bookstore的子节点
            {
                XmlElement xe 
=  (XmlElement)xn; // 将子节点类型转换为XmlElement类型
                 if  (xe.GetAttribute( " genre " ==   " 李赞红 " ) // 如果genre属性值为“李赞红”
                {
                    xe.SetAttribute(
" genre " " update李赞红 " ); // 则修改该属性为“update李赞红”

                    XmlNodeList nls 
=  xe.ChildNodes; // 继续获取xe(xn)子节点的所有子节点
                     foreach  (XmlNode xn1  in  nls) // 遍历
                    {
                        XmlElement xe2 
=  (XmlElement)xn1; // 转换类型
                         if  (xe2.Name  ==   " author " ) // 如果找到
                        {
                            xe2.InnerText 
=   " 亚胜 " ; // 则修改
                             break ; // 找到退出来
                        }
                    }
                    
break ;
                }
            }
            xmlDoc.Save(
@" d:\bookstore.xml " ); // 保存。

======================== 删除 =========================

            XmlDocument xmlDoc 
=   new  XmlDocument();
            xmlDoc.Load(
@" d:\bookstore.xml " );
            XmlNodeList xnl 
=  xmlDoc.SelectSingleNode( " bookstore " ).ChildNodes;

            
foreach  (XmlNode xn  in  xnl)
            {
                XmlElement xe 
=  (XmlElement)xn;


                
if  (xe.GetAttribute( " genre " ==   " fantasy " )
                {
                    xe.RemoveAttribute(
" genre " ); // 删除genre属性
                }
                
else   if  (xe.GetAttribute( " genre " ==   " update李赞红 " )
                {
                    xe.RemoveAll();
// 删除该节点的全部内容
                }
            }
            xmlDoc.Save(
@" d:\bookstore.xml " ); 

 


 

你可能感兴趣的:(基本操作)