创建XML文件

创建XML文件的多种方法:(首先都要加上语句: using System.Xml;)

创建如下格式XML文件:

 

<? xml version="1.0" encoding="gb2312" ?>
< bookstore >
  
< book  ISBN ="2-3631-4"  genre ="fantasy" >
    
< title > C# Program </ title >
    
< author > Michael </ author >
    
< price > $49 </ price >
  
</ book >
</ bookstore >

 

第一种方法:

 

private   void  button1_Click( object  sender, System.EventArgs e)
        
{
            XmlDocument xmlDoc 
= new XmlDocument();
            xmlDoc.LoadXml(
"<?xml version='1.0' encoding='gb2312'?>"+
                
"<bookstore>"+
                
"<book ISBN='2-3631-4' genre='fantasy'>"+
                
"<title>C# Program</title>"+
                
"<author>Michael</author>"+
                
"<price>$49</price>"+
                
"</book>"+
                
"</bookstore>");
            xmlDoc.Save(
@"D:MyTestMyTestinDebugXmlFiles ile1.xml");
        }

 

第二种方法:

 

private   void  button2_Click( object  sender, System.EventArgs e)
        
{
            XmlDocument xmlDoc 
= new XmlDocument();
            XmlText xmlText;
            
//加入XML的声明段落
            XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0","gb2312","");
            xmlDoc.AppendChild(xmlDecl);
            
//加入一个根元素
            XmlElement xmlElem = xmlDoc.CreateElement("","bookstore","");
            xmlDoc.AppendChild(xmlElem);
            
//加入一个子元素
            XmlElement xmlElem1 = xmlDoc.CreateElement("","book","");
            
//为子元素book增加两个属性
            xmlElem1.SetAttribute("ISBN","","2-3631-4");
            xmlElem1.SetAttribute(
"genre","","fantasy");
            xmlDoc.ChildNodes.Item(
1).AppendChild(xmlElem1);
            
//创建子元素的三个子元素
            XmlElement xmlElem2 = xmlDoc.CreateElement("","title","");
            
//为子元素title增加文本
            xmlText = xmlDoc.CreateTextNode("C# Program");
            xmlElem2.AppendChild(xmlText);
            xmlDoc.ChildNodes.Item(
1).AppendChild(xmlElem1).AppendChild(xmlElem2);
            XmlElement xmlElem3 
= xmlDoc.CreateElement("","author","");
            xmlText 
= xmlDoc.CreateTextNode("Michael");
            xmlElem3.AppendChild(xmlText);
            xmlDoc.ChildNodes.Item(
1).AppendChild(xmlElem1).AppendChild(xmlElem3);
            XmlElement xmlElem4 
= xmlDoc.CreateElement("","price","");
            xmlText 
= xmlDoc.CreateTextNode("$49");
            xmlElem4.AppendChild(xmlText);
            xmlDoc.ChildNodes.Item(
1).AppendChild(xmlElem1).AppendChild(xmlElem4);
            xmlDoc.Save(
@"D:MyTestMyTestinDebugXmlFiles ile2.xml");
        }

 

第三种方法:

 

private   void  button3_Click( object  sender, System.EventArgs e)
        
{
            XmlDocument xmlDoc 
= new XmlDocument();
            
//加入XML的声明段落
            XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0","gb2312","");
            xmlDoc.AppendChild(xmlDecl);
            
//加入一个根元素
            XmlElement xmlElem = xmlDoc.CreateElement("","bookstore","");
            
//xmlDoc.AppendChild(xmlElem);
            
//
            
//XmlNode xmlNode = xmlDoc.SelectSingleNode("bookstore");
            
//加入一个子元素
            XmlElement xmlElem1 = xmlDoc.CreateElement("","book","");
            xmlElem1.SetAttribute(
"ISBN","","2-3631-4");
            xmlElem1.SetAttribute(
"genre","","fantasy");
            
//创建子元素的三个子元素
            XmlElement xmlElem2 = xmlDoc.CreateElement("","title","");
            xmlElem2.InnerText 
= "C# Program";
            XmlElement xmlElem3 
= xmlDoc.CreateElement("","author","");
            xmlElem3.InnerText 
= "Michael";
            XmlElement xmlElem4 
= xmlDoc.CreateElement("","price","");
            xmlElem4.InnerText 
= "$49";
            xmlElem1.AppendChild(xmlElem2);
            xmlElem1.AppendChild(xmlElem3);
            xmlElem1.AppendChild(xmlElem4);
            
//xmlNode.AppendChild(xmlElem1);
            xmlElem.AppendChild(xmlElem1);
            xmlDoc.AppendChild(xmlElem);
            xmlDoc.Save(
@"D:MyTestMyTestinDebugXmlFiles ile3.xml");
        }

 

其实还可以变通方法的,但是细想都一样,只是写法不同,比如方法三中的注释语句可以替换其他的语句也可以实现.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值