using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Xml.XPath;
using System.Xml.Xsl;
using System.IO;namespace testXML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }       
                   private void Form1_Load(object sender, EventArgs e)
        {
             try
            {
                // 创建XmlTextWriter类的实例对象
                XmlTextWriter textWriter = new XmlTextWriter("d:\\config.xml", null);
                textWriter.Formatting = Formatting.Indented;                // 开始写过程,调用WriteStartDocument方法
                textWriter.WriteStartDocument();                // 写入说明
                textWriter.WriteComment("读写XML文件");
                textWriter.WriteComment("config.xml in root dir");                //创建一个节点   油轮信息
                textWriter.WriteStartElement("student");
               
                textWriter.WriteElementString("name", "刘杰");
                textWriter.WriteElementString("sex", "女");
                textWriter.WriteEndElement();                //textWriter.WriteStartElement("username");
                //textWriter.WriteElementString("yanyuan", "1");
                //textWriter.WriteElementString("王磊", "1");
                //textWriter.WriteEndElement();                //我想在这里添加一个节点 (USERNAME)在这里报错了 怎么添加节点了                 // 写文档结束,调用WriteEndDocument方法
                textWriter.WriteEndDocument();                // 关闭textWriter
                textWriter.Close();            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }            //}
        }
       
    }
}

解决方案 »

  1.   

    http://www.cnblogs.com/sunrack/articles/1081714.html
      

  2.   

    public static void Insert(string path, string node, string element, string attribute, string value)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    if (element.Equals(""))
                    {
                        if (!attribute.Equals(""))
                        {
                            XmlElement xe = (XmlElement)xn;
                            xe.SetAttribute(attribute, value);
                        }
                    }
                    else
                    {
                        XmlElement xe = doc.CreateElement(element);
                        if (attribute.Equals(""))
                        {
                            xe.InnerText = value;
                        }
                        else
                        {
                            xe.SetAttribute(attribute, value);
                        }
                        xn.AppendChild(xe);
                    }
                    doc.Save(path);
                }
                catch { }
            }
      

  3.   


       xtw.WriteStartElement("username");
                    xtw.WriteEndElement();
      

  4.   

    参看这里,XML完全操作(增删改查),希望对你有帮助!
    Reply by CSDN小秘书