创建XML文件
来源:互联网 发布:dw软件 编辑:程序博客网 时间:2024/05/21 08:44
创建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>
<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");
}
...{
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");
}
...{
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");
}
...{
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");
}
其实还可以变通方法的,但是细想都一样,只是写法不同,比如方法三中的注释语句可以替换其他的语句也可以实现.
- C#创建XML文件
- 创建XML文件
- java创建XML文件
- XmlTextWriter创建XML文件
- 创建XML文件
- 创建XML文件
- C#创建xml文件
- 创建xml文件
- C#创建Xml文件
- 创建XML文件
- Android创建XMl文件
- java创建xml文件
- 创建xml文件
- XML文件的创建
- .NET创建XML文件
- java创建xml文件
- Android创建XMl文件 .
- 创建XML文件
- Drupal 介绍
- 如何理解Perl语言中的Glob
- Servlet调用SQL Server2000存储过程实例
- SQL的系统存储过程xp_cmdshell
- Hibernate学习笔记!- -
- 创建XML文件
- 无参数调用存储过程
- vs.net中web services入门
- 一条短信引发的感想
- 数据库删除列
- 框架与系统架构
- hibernat学习
- Ruby and Rails 当时
- 第五周--星期四