XML文件是在开发中操作最为平常的文件。无论是WEB还是客户端应用程序的开发,都有平凡操作XML文件的事情。本人是初学者,现将自己对XML文件的增、删、改基本操作学习做一下总结,以便日后参考之用。现假如有一个XML文件,文件路径为:Channels.xml。文件内容如下:

<?xmlversion="1.0"encoding="utf-8"?>
<profile>
<channel>
<type>rss</type>
<title>体育新闻</title>
<description>体育新闻频道</description>
<link>http://www.CCTV5.com</link>>
<article>
<title>狗狗引擎</title>
<url>http://www.gougou.com</url>
</article>
</channel>
</profile>

一、追加节点并保存XML文件

String path="Channels.xml";
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(path);
//根节点
XmlNode root=xmlDoc.SelectSingleNode("profile");
//频道节点
XmlElement element=xmlDoc.CreateElement("channel");
//频道类型节点
XmlElement elementType=xmlDoc.CreateElement("type");
elementType.InnerText="rss";
element.AppendChild(elementType);
//频道名称节点
XmlElement elementTitle=xmlDoc.CreateElement("title");
elementTitle.InnerText="新浪新闻";
element.AppendChild(elementTitle);
//频道描述节点
XmlElement elementDesc=xmlDoc.CreateElement("description");
elementDesc.InnerText="新浪新闻频道";
element.AppendChild(elementDesc);
//频道地址节点
XmlElement elementLink=xmlDoc.CreateElement("link");
elementLink.InnerText="http://www.sina.com.cn";
element.AppendChild(elementLink);
//Article
XmlElement elementArticle=xmlDoc.CreateElement("article");
 
XmlElement eATitile=xmlDoc.CreateElement("Title");
eATitile.InnerText="百度引擎";
elementArticle.AppendChild(eATitile);
 
XmlElement eAUrl=xmlDoc.CreateElement("url");
eAUrl.InnerText="http://www.baidu.com";
elementArticle.AppendChild(eAUrl);
element.AppendChild(elementArticle);
 
root.AppendChild(element);
xmlDoc.Save(path);

那么XML文件就是:

<?xmlversion="1.0"encoding="utf-8"?>
<profile>
<channel>
<type>rss</type>
<title>体育新闻</title>
<description>体育新闻频道</description>
<link>http://www.CCTV5.com</link>>
<article>
<title>狗狗引擎</title>
<url>http://www.gougou.com</url>
</article>
</channel>
<channel>
<type>rss</type>
<title>新浪新闻</title>
<description>新浪新闻频道</description>
<link>http://www.sina.com.cn</link>>
<article>
<title>百度引擎</title>
<url>http://www.baidu.com</url>
</article>
</channel>
</profile>

二、修改XML文件

XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(path);
//找到title为指定字符的频道节点
string title="新浪新闻";
string xpath=string.Format("descendant::channel[title='{0}']",title);
XmlElement selectElement=(XmlElement)xmlDoc.SelectSingleNode(xpath);
if(selectElement==null) return;
selectElement.ChildNodes[1].InnerText="新浪";
selectElement.ChildNodes[2].InnerText="新浪频道";
selectElement.ChildNodes[3].InnerText="http://www.sina.com.cn";
xmlDoc.Save(path);

那么XML文件就是:

<?xmlversion="1.0"encoding="utf-8"?>
<profile>
<channel>
<type>rss</type>
<title>体育新闻</title>
<description>体育新闻频道</description>
<link>http://www.CCTV5.com</link>>
<article>
<title>狗狗引擎</title>
<url>http://www.gougou.com</url>
</article>
</channel>
<channel>
<type>rss</type>
<title>新浪</title>
<description>新浪频道</description>
<link>http://www.sina.com.cn</link>>
<article>
<title>百度引擎</title>
<url>http://www.baidu.com</url>
</article>
</channel>
</profile>

三、删除节点

XmlDocumentnew XmlDocument();
xmlDoc.Load(path);
string"体育新闻";
stringstring.Format(@"descendant::channel[title='{0}']",title);
XmlElementXmlElement)xmlDoc.SelectSingleNode(xpath);
if(selectElement==null) return;
xmlDoc.DocumentElement.RemoveChild(selectElement);
xmlDoc.Save(path);

那么XML文件就是:

<?xmlversion="1.0"encoding="utf-8"?>
<profile>
<channel>
<type>rss</type>
<title>新浪</title>
<description>新浪频道</description>
<link>http://www.sina.com.cn</link>>
<article>
<title>百度引擎</title>
<url>http://www.baidu.com</url>
</article>
</channel>
</profile>

对XML文件的操作其实就是对XML文件节点的操作。