创建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");
}
其实还可以变通方法的,但是细想都一样,只是写法不同,比如方法三中的注释语句可以替换其他的语句也可以实现.