在C#中使用三种不同的方法写入XML文档
2012-12-09 11:33
561 查看
在本文中,我再介绍一个类,XmlTextWriter.我们分别用这三个类将同样的xml内容写入文档,看一看哪种写法最直观、简便.
我们要写入的XML文档内容为
<?xml version=“1.0” encoding=“UTF-8”?>
<Contacts>
<Contact id=“01”>
<Name>Daisy Abbey</Name>
<Gender>female</Gender>
</Contact>
</Contacts>
Daisy Abbey
female
(1)使用XmlDocument类:
var xmlDoc = new XmlDocument();
//Create the xml declaration first
xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration(“1.0”, “utf-8”, null));
//Create the root node and append into doc
var el = xmlDoc.CreateElement(“Contacts”);
xmlDoc.AppendChild(el);
// Contact
XmlElement elementContact = xmlDoc.CreateElement(“Contact”);
XmlAttribute attrID = xmlDoc.CreateAttribute(“id”);
attrID.Value = “01”;
elementContact.Attributes.Append(attrID);
el.AppendChild(elementContact);
// Contact Name
XmlElement elementName = xmlDoc.CreateElement(“Name”);
elementName.InnerText = “Daisy Abbey”;
elementContact.AppendChild(elementName);
// Contact Gender
XmlElement elementGender = xmlDoc.CreateElement(“Gender”);
elementGender.InnerText = “female”;
elementContact.AppendChild(elementGender);
xmlDoc.Save(“test1.xml”);
(2)使用LINQ to XML 的XDocument类:
var doc = new XDocument(
new XElement(“Contacts”,
new XElement(“Contact”,
new XAttribute(“id”, “01”),
new XElement(“Name”, “Daisy Abbey”),
new XElement(“Gender”, “female”)
)
)
);
doc.Save(“test2.xml”);
(3) 使用XmlTextWriter类:
String filename = String.Concat(“test3.xml”);
using (StreamWriter sw = new StreamWriter(filename))
{
// Create Xml Writer.
XmlTextWriter xmlWriter = new XmlTextWriter(sw);
// 也可以使用public XmlTextWriter(string filename, Encoding encoding)来构造
// encoding默认为 UTF-8.
//XmlTextWriter writer = new XmlTextWriter(“test3.xml”, null);
// Set indenting so that its easier to read XML when open in Notepad and such apps.
xmlWriter.Formatting = Formatting.Indented;
// This will output the XML declaration
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement(“Contacts”);
xmlWriter.WriteStartElement(“Contact”);
xmlWriter.WriteAttributeString(“id”, “01”);
xmlWriter.WriteElementString(“Name”, “Daisy Abbey”);
xmlWriter.WriteElementString(“Gender”, “female”);
// close contact
xmlWriter.WriteEndElement();
// close contacts
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}
从上面的代码基本上还是可以看出来,使用LINQ to XML是最简便的.
我们要写入的XML文档内容为
<?xml version=“1.0” encoding=“UTF-8”?>
<Contacts>
<Contact id=“01”>
<Name>Daisy Abbey</Name>
<Gender>female</Gender>
</Contact>
</Contacts>
Daisy Abbey
female
(1)使用XmlDocument类:
var xmlDoc = new XmlDocument();
//Create the xml declaration first
xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration(“1.0”, “utf-8”, null));
//Create the root node and append into doc
var el = xmlDoc.CreateElement(“Contacts”);
xmlDoc.AppendChild(el);
// Contact
XmlElement elementContact = xmlDoc.CreateElement(“Contact”);
XmlAttribute attrID = xmlDoc.CreateAttribute(“id”);
attrID.Value = “01”;
elementContact.Attributes.Append(attrID);
el.AppendChild(elementContact);
// Contact Name
XmlElement elementName = xmlDoc.CreateElement(“Name”);
elementName.InnerText = “Daisy Abbey”;
elementContact.AppendChild(elementName);
// Contact Gender
XmlElement elementGender = xmlDoc.CreateElement(“Gender”);
elementGender.InnerText = “female”;
elementContact.AppendChild(elementGender);
xmlDoc.Save(“test1.xml”);
(2)使用LINQ to XML 的XDocument类:
var doc = new XDocument(
new XElement(“Contacts”,
new XElement(“Contact”,
new XAttribute(“id”, “01”),
new XElement(“Name”, “Daisy Abbey”),
new XElement(“Gender”, “female”)
)
)
);
doc.Save(“test2.xml”);
(3) 使用XmlTextWriter类:
String filename = String.Concat(“test3.xml”);
using (StreamWriter sw = new StreamWriter(filename))
{
// Create Xml Writer.
XmlTextWriter xmlWriter = new XmlTextWriter(sw);
// 也可以使用public XmlTextWriter(string filename, Encoding encoding)来构造
// encoding默认为 UTF-8.
//XmlTextWriter writer = new XmlTextWriter(“test3.xml”, null);
// Set indenting so that its easier to read XML when open in Notepad and such apps.
xmlWriter.Formatting = Formatting.Indented;
// This will output the XML declaration
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement(“Contacts”);
xmlWriter.WriteStartElement(“Contact”);
xmlWriter.WriteAttributeString(“id”, “01”);
xmlWriter.WriteElementString(“Name”, “Daisy Abbey”);
xmlWriter.WriteElementString(“Gender”, “female”);
// close contact
xmlWriter.WriteEndElement();
// close contacts
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}
从上面的代码基本上还是可以看出来,使用LINQ to XML是最简便的.
相关文章推荐
- C# 写入XML文档三种方法详细介绍
- C# 写入XML文档三种方法详细介绍
- C# 三种写入XML文档的方法
- C# 写入XML文档三种方法详细介绍
- 使用DOM方法实现xml文档内容的解析与写入
- C#中使用DOM解析XML文档的方法
- C# 操作XML文档 使用XmlDocument类方法
- C# 操作XML文档 使用XmlDocument类方法
- python 6-4 如何构建xml文档使用标准库中的xml.etree.ElementTree 构建ElementTree,使用write方法写入文档
- C#操作读取、写入XML文档的实用方法
- C# 计时器的三种使用方法
- C#使用iTextSharp从PDF文档获取内容的方法
- C#中XML的写入方法
- 简单介绍使用Python解析并修改XML文档的方法
- C#中处理XML文档的方法
- C#利用XML创建Excel文档的实现方法
- C#使用DataSet Datatable更新数据库的三种实现方法
- 使用eclipse生成文档(javadoc)主要有三种方法:
- C# 中使用using的三种方法
- 【转】C#中处理XML文档的方法