C#操作XML文件总结篇

要对XML文件进行操作,须要声明以下命名空间:
using System.Xml;

一、检查指定XML文件是否存在

            System.IO.File.Exists(文件路径及名称);

二、利用C#编程创建XML文件 

        我在网上找到了这样的两段代码:
        代码一:
            XmlDocument xmldoc = new XmlDocument();
            XmlNode xmlnode;
            XmlElement xmlelem;
            XmlElement xmlelem2;
            XmlText xmltext;

            //加入XML的声明段落
            xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            xmlnode.InnerText+=" encoding=/"GB2312/"";
            xmldoc.AppendChild(xmlnode);

            //加入一个根元素
            xmlelem = xmldoc.CreateElement("", "ROOT", "");
            xmltext = xmldoc.CreateTextNode("Root Text");
            xmlelem.AppendChild(xmltext);
            xmldoc.AppendChild(xmlelem);

            //加入另外一个元素
            xmlelem2 = xmldoc.CreateElement("SampleElement");
            xmlelem2 = xmldoc.CreateElement("","SampleElement","");
            xmltext = xmldoc.CreateTextNode("The text of the sample element");
            xmlelem2.AppendChild(xmltext);
            xmldoc.ChildNodes.Item(1).AppendChild(xmlelem2);

            //保存创建好的XML文档
            try
            {
                xmldoc.Save("data.xml");
            }
            catch (Exception f)
            {
                //显示错误信息
                MessageBox.Show(f.Message);
            }
            //Console.ReadLine();
        这段代码在win2003ser+vs2005环境下测试成功,但是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环境下测试通过,出来的效果很好,也比较容易理解,我一般就是用这段代码创建XML文件。

三、读取、修改XML文件的某个节点的值

            string path = "phone.xml";  
            XmlDocument doc = new XmlDocument();
            doc.Load(path);
            //读所有节点表
            XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
            //读取节点值
            XmlNode node = doc.SelectSingleNode("/PhoneBook/Name", xnm);  //node.InnerText 就是读取出来的值

            //修改节点值
            node.InnerText="要修改的内容";

            //保存修改后的内容
            doc.Save(path);
 

你可能感兴趣的:(xml)