C# 寫入XML文檔三種方法詳細(xì)介紹
更新時(shí)間:2012年12月04日 11:31:41 作者:
如何使用LINQ to XML對(duì)XML進(jìn)行操作,我們分別用這三個(gè)類將同樣的xml內(nèi)容寫入文檔,需要了解的朋友可以參考下
我在以前的博客中介紹了如何使用XmlDocument類對(duì)XML進(jìn)行操作,以及如何使用LINQ to XML對(duì)XML進(jìn)行操作。它們分別使用了XmlDocument類和XDocument類。在本文中,我再介紹一個(gè)類,XmlTextWriter。我們分別用這三個(gè)類將同樣的xml內(nèi)容寫入文檔,看一看哪種寫法最直觀、簡便。
我們要寫入的XML文檔內(nèi)容為
<?xml version="1.0" encoding="UTF-8"?>
<Contacts>
<Contact id="01">
<Name>Daisy Abbey</Name>
<Gender>female</Gender>
</Contact>
</Contacts>
(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)來構(gòu)造
// encoding默認(rèn)為 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 </contact>
xmlWriter.WriteEndElement();
// close contacts </contact>
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}
從上面的代碼基本上還是可以看出來,使用LINQ to XML是最簡便的。
我們要寫入的XML文檔內(nèi)容為
復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="UTF-8"?>
<Contacts>
<Contact id="01">
<Name>Daisy Abbey</Name>
<Gender>female</Gender>
</Contact>
</Contacts>
(1)使用XmlDocument類:
復(fù)制代碼 代碼如下:
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類:
復(fù)制代碼 代碼如下:
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類:
復(fù)制代碼 代碼如下:
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)來構(gòu)造
// encoding默認(rèn)為 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 </contact>
xmlWriter.WriteEndElement();
// close contacts </contact>
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}
從上面的代碼基本上還是可以看出來,使用LINQ to XML是最簡便的。
相關(guān)文章
WPF設(shè)置窗體可以使用鼠標(biāo)拖動(dòng)大小的方法
這篇文章主要介紹了WPF設(shè)置窗體可以使用鼠標(biāo)拖動(dòng)大小的方法,涉及針對(duì)窗口的操作與設(shè)置技巧,具有很好的借鑒價(jià)值,需要的朋友可以參考下2014-11-11unity 文件流讀取圖片與www讀取圖片的區(qū)別介紹
這篇文章主要介紹了unity 文件流讀取圖片與www讀取圖片的對(duì)比分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2021-04-04C# WinForm窗體編程中處理數(shù)字的正確操作方法
這篇文章主要介紹了C# WinForm窗體編程中處理數(shù)字的正確操作方法,本文給出了正確示例,并解釋了為什么要這么做,需要的朋友可以參考下2014-08-08WPF實(shí)現(xiàn)3D翻牌式倒計(jì)時(shí)特效
這篇文章主要為大家詳細(xì)介紹了WPF實(shí)現(xiàn)3D翻牌式倒計(jì)時(shí)特效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09C#實(shí)現(xiàn)的AES加密解密完整實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)的AES加密解密,結(jié)合完整實(shí)例形式分析了C#實(shí)現(xiàn)的AES算法進(jìn)行加密與解密的相關(guān)技巧,需要的朋友可以參考下2016-07-07