服務(wù)熱線
153 8323 9821
引用命名空間:using System.Xml
1.檢查所要操作的xml文件是否存在:
System.IO.File.Exists(文件路徑及名稱);
2.得到xml文件:
(1)在asp.net中可以這樣得到:
XmlDocument xmlDoc = new XmlDocument();
//導(dǎo)入xml文檔
xmlDoc.Load( Server.MapPath("xmlTesting.xml"));
//導(dǎo)入字符串
//xmlDoc.LoadXml("<bookStore> <book id="01" price="3.5元"> 讀者</book></bookStore>");
注:Server.MapPath("xmlTesting.xml")此時(shí)的xmlTesting.xml文件必須是在當(dāng)前的解決方案里;同樣可以寫成完整的物理路徑xmlDoc.Load (@"E:"軟件學(xué)習(xí)"測試"myNoteWeb"xmlTesting.xml")
(2)在windForm中 直接用物理路徑得到所要操作的xml文件具體實(shí)現(xiàn)方法同上
3.創(chuàng)建xml文件:
XmlDocument xmlDoc = new XmlDocument(); //創(chuàng)建xml文檔(實(shí)例化一個(gè)xml)
XmlNode root = xmlDoc.CreateElement("bookStore");//創(chuàng)建根節(jié)點(diǎn)
//創(chuàng)建第1個(gè)子結(jié)點(diǎn):
XmlNode bookNode = xmlDoc.CreateElement("book");
bookNode.InnerText = "讀者";
//為此節(jié)點(diǎn)添加屬性
法1:
bookPublishNode.SetAttribute("id", "01")
root.AppendChild(bookNode);
法2:
XmlAttribute xmlattribute = tempXmlDoc.CreateAttribute("price");
xmlattribute.Value = "3.5元";
tempRoot .Attributes .Append (xmlattribute )
//創(chuàng)建第2個(gè)根節(jié)點(diǎn)的子結(jié)點(diǎn):
XmlNode tempBookNode = xmlDoc.CreateElement("tempbook ");
tempBookNode.InnerText ="文摘";
root.AppendChild(tempBookNode);
xmlDoc.AppendChild(root); //將根節(jié)點(diǎn)添加到xml文檔中
try
{
xmlDoc.save(“bookInfo.xml”);//xml將保存到當(dāng)前解決方案的目錄下
}
catch (Exception ex)
{
MessageBox.Show(ex.Message); //顯示錯(cuò)誤信息
}
得到的xml文檔如下:
<?xml version="1.0" encoding="utf-8" ?>
<bookStore>
<book id ="01" price="3.5元">
讀者
</book>
<tempbook id ="02">
文摘
</tempbook >
</bookStore>
4.讀取,修改xml信息
tempTesting.xml文件的內(nèi)容如下:
?<?xml version="1.0" encoding="utf-8"?>
<bookStore>
<book id="01" price="3.5元">
讀者
</book>
<book id="02" price="5元">
<bookname>百家講壇</bookname>
<bookpublish>文學(xué)出版社</bookpublish>
</book>
<tempbook id="0000">漫畫tempbook>
</bookStore>
(1) 得到xml文件的xml信息
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load( Server.MapPath("xmlTesting.xml"));
XmlNode root=xmlDoc.DocumentElement;
(2) 得到節(jié)點(diǎn)
//得到id為2book節(jié)點(diǎn)下的所有節(jié)點(diǎn)
XmlNodeList xmlNodes = root.SelectNodes("//book[@id='02']//×");
XmlNode tempNode = xmlNodes[0];//得到第一個(gè)節(jié)點(diǎn)
//將tempbook強(qiáng)制轉(zhuǎn)化為XmlElement
XmlElement xmlelement = (XmlElement)root.SelectSingleNode("tempbook");
(3) 修改節(jié)點(diǎn)內(nèi)容
XmlNode tempBook = root.SelectSingleNode("tempbook");//修改節(jié)點(diǎn)內(nèi)容
tempBook. InnerText="漫畫";
(4)得到節(jié)點(diǎn)的屬性值并修改:
//得到節(jié)點(diǎn)的屬性
//法1
XmlAttributeCollection attrbute= tempBook.Attributes;
string bookID = attrbute[0].Value;
//法2:
string bookid = tempBook.Attributes[0].Value.ToString();
//修改節(jié)點(diǎn)屬性的值
tempBook.Attributes[0].Value = "0000";
(5)添加一個(gè)根節(jié)點(diǎn)
XmlNode node = xmlDoc.CreateElement("testing");
node.InnerText = "testing";
root.AppendChild(node);
(6)保存修改后的文檔
xmlDoc.Save(Server.MapPath("tempTesting.xml"));
××××××其他:
(一)顯示xml信息
1以xml文檔的樣式顯示:xmlDoc.outerxml;
2.顯示各個(gè)節(jié)點(diǎn)
XmlNode root = xmlDoc.DocumentElement;//得到根節(jié)點(diǎn):
if (root.ChildNodes.Count > 0)
{
XmlNode xmlnode = root.FirstChild;//得到子結(jié)點(diǎn):
for (int i = 0; i < root.ChildNodes.Count; i++)
{
MessageBox.Show(xmlnode.FirstChild .InnerText);
xmlnode = xmlnode.NextSibling;或mlNode subNode2 = root.LastChild;
}
}
else
MessageBox.Show("沒有內(nèi)容!");
(二)將數(shù)據(jù)庫的信息導(dǎo)入為xml
XmlDocument xmlDocItem = new XmlDocument();
DataSet ds = GetDataSet(strtsql);//調(diào)用方法從數(shù)據(jù)庫中得到DataSet。
xmlDocItem.LoadXml(ds .GetXml ());//導(dǎo)入xml中;
×××××××××××××××××××××××××××××以下是在網(wǎng)絡(luò)上找到的關(guān)于C#操作xml文件的總結(jié)×××
一:創(chuàng)建并保存xml文件
string FileName =Application.StartupPath+"""phone.xml";
XmlTextWriter objXmlTextWriter = new XmlTextWriter(FileName,Encoding.Default);
objXmlTextWriter.Formatting = Formatting.Indented;
objXmlTextWriter.Indentation = 6;
objXmlTextWriter.WriteStartDocument();
objXmlTextWriter.WriteStartElement("", "PhoneBook", "");
objXmlTextWriter.WriteStartElement("", "Name", "");
objXmlTextWriter.WriteString("加菲爾德");
objXmlTextWriter.WriteEndElement();
objXmlTextWriter.WriteStartElement("", "Number", "");
objXmlTextWriter.WriteString("5555555");
objXmlTextWriter.WriteEndElement();
objXmlTextWriter.WriteStartElement("", "City", "");
objXmlTextWriter.WriteString("紐約");
objXmlTextWriter.WriteEndElement();
objXmlTextWriter.WriteStartElement("", "DateOfBirth", "");
objXmlTextWriter.WriteString("26/10/1978");
objXmlTextWriter.WriteEndElement();
objXmlTextWriter.WriteEndElement();
objXmlTextWriter.WriteEndDocument();
objXmlTextWriter.Flush();
objXmlTextWriter.Close();
這段代碼在win2003ser+vs2005環(huán)境下測試通過,出來的效果很好,也比較容易理解,我一般就是用這段代碼創(chuàng)建XML文件。
二、讀取、修改XML文件的某個(gè)節(jié)點(diǎn)的值
string path = "phone.xml";
XmlDocument doc = new XmlDocument();
doc.Load(path);
//讀所有節(jié)點(diǎn)表
XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
//讀取節(jié)點(diǎn)值
XmlNode node = doc.SelectSingleNode("/PhoneBook/Name", xnm); //node.InnerText 就是讀取出來的值
//修改節(jié)點(diǎn)值
node.InnerText="要修改的內(nèi)容";
//保存修改后的內(nèi)容
doc.Save(path);