悟生慧

 

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.创建文件的声明
XmlDocument xmlDoc = new XmlDocument();//创建Xml文件的声明
XmlDeclaration xmlDeclar = xmlDoc.CreateXmlDeclaration("1.0","UTF-8",null);
XmlElement rootElement = xmlDoc.DocumentElement;//获取文档根
xmlDoc.InsertBefore(xmlDeclar,rootElement);//在文档根元素之前添加声明

XmlElement root = xmlDoc.CreateElement("Configuration");//创建根节点
xmlDoc.AppendChild(root);
3.保存以上创建
xmlDoc.Save(FileName);//保存根节点之后才能向Xml文件中创建子节点
4.加载文件
xmlDoc.Load(FileName);
5.创建节点或元素
XmlElement rootNode = xmlDoc.DocumentElement;
rootNode.InnerText = "*****";
//Todo.....根据Document对象操作

 

/*****docking panel第三方组件 记录********/

posted on 2013-04-18 15:34  悟生慧  阅读(1233)  评论(0编辑  收藏  举报

导航