C#操作XmlDocument对象 报缺少根节点 一一道来
C# 操作XmlDocument文檔
1.第一步選擇文件目錄
string FileName=string.Empty;
SaveFileDialog sfd=new SaveFileDialog();
if(sfd.ShowDialog() == DialogResult.OK)
{
??? FileName = sfd.FileName;
}
FileStream fs;//判斷文件是否存在
if(!File.Exists(FileName))
{
??? fs = File.Create(FileName);
??? fs.Close();
}
2.創(chuàng)建文件的聲明
XmlDocument xmlDoc = new XmlDocument();//創(chuàng)建Xml文件的聲明
XmlDeclaration xmlDeclar = xmlDoc.CreateXmlDeclaration("1.0","UTF-8",null);
XmlElement rootElement = xmlDoc.DocumentElement;//獲取文檔根
xmlDoc.InsertBefore(xmlDeclar,rootElement);//在文檔根元素之前添加聲明
XmlElement root = xmlDoc.CreateElement("Configuration");//創(chuàng)建根節(jié)點(diǎn)
xmlDoc.AppendChild(root);
3.保存以上創(chuàng)建
xmlDoc.Save(FileName);//保存根節(jié)點(diǎn)之后才能向Xml文件中創(chuàng)建子節(jié)點(diǎn)
4.加載文件
xmlDoc.Load(FileName);
5.創(chuàng)建節(jié)點(diǎn)或元素
XmlElement rootNode = xmlDoc.DocumentElement;
rootNode.InnerText = "*****";
//Todo.....根據(jù)Document對(duì)象操作
?
/*****docking panel第三方組件 記錄********/
總結(jié)
以上是生活随笔為你收集整理的C#操作XmlDocument对象 报缺少根节点 一一道来的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 梦到姐姐怀二胎预示什么
- 下一篇: 冒泡排序 C#