using System.Xml;string sFileName =Application.StartupPath+"\\file.xml";
XmlDocument myDoc = new XmlDocument();
   if(System.IO.File.Exists(ss)) //如果存在
{
  myDoc.Load(ss);
           //读写该文件
          int i=1;
          XmlNode myNode;
  myNode = myDoc.DocumentElement.ChildNodes[i];
   //读写myNode  }
   else
{
        //新建元素
         XmlElement tempElement = myDoc.CreateElement("新建1");
 ...
        }  myDoc.Save("file.xml");

解决方案 »

  1.   

    /// <summary>
    /// XmlConfig 的摘要说明。
    /// </summary>
    public class XmlConfig
    {
    private XmlDocument doc;
    private string xmlFileName;
    private MemoryStream xmlStream;
    private byte[] xmlcontent = new byte[0];
    public byte[] Content
    {
    get
    {
    return System.Text.Encoding.Default.GetBytes(doc.InnerXml);
    }
    set
    {
    }
    }
    public XmlConfig(string filename)
    {

    xmlFileName = filename;
    doc = new XmlDocument();
    try
    {
    doc.Load(xmlFileName);
    }
    catch
    {
    doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><Settings></Settings>");
    }
    } public XmlConfig(byte[] contain)
    {
    xmlStream = new MemoryStream(); xmlStream.Write(contain,0,contain.Length);
    // MemoryStream stream =new MemoryStream(contain);
    // xmlStream = (Stream) stream;
    doc = new XmlDocument();

    try
    {
    if(contain.Length==0)
    doc.Load(xmlStream);
    else
    {
    string xml = System.Text.Encoding.Default.GetString(contain);
    doc.LoadXml(xml);
    }
    }
    catch(Exception ee)
    {
    doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><Settings></Settings>");
    }
    }
    public void Save()
    {
    try
    {
    doc.Save(xmlFileName);
    }
    catch
    {
    }
    }
    public void StreamSave()
    {
    try
    {
    doc.Save(xmlStream);
    }
    catch(Exception ee)
    {
    Console.WriteLine(ee.ToString()); } } public string Read(string key, string value)
    {
    XmlNode node = doc.DocumentElement.SelectSingleNode(key);
    if (node != null)
    return node.InnerText;
    else
    return value;
    } public void Write(string key, string value)
    {
    XmlNode node = doc.DocumentElement.SelectSingleNode(key);
    if (node != null)
    {
    node.InnerText = value;
    }
    else
    {
    node = doc.DocumentElement;
    string[] path = key.Split(new char[] {'/'});
    for (int i = 0; i < path.Length; i++)
    {
    XmlNode node2;
    if ( (node2 = node.SelectSingleNode(path[i])) == null)
    {
    node2 = doc.CreateElement(path[i]);
    node.AppendChild(node2);
    }
    node = node2;
    }      
    node.InnerText = value;
    }
    }

    //增加一个父节点
    public void Write(string key)
    {
    XmlNode node = doc.DocumentElement.SelectSingleNode(key);
    if (node != null)
    {
    }
    else
    {
    node = doc.DocumentElement;
    string[] path = key.Split(new char[] {'/'});
    for (int i = 0; i < path.Length; i++)
    {
    XmlNode node2;
    if ( (node2 = node.SelectSingleNode(path[i])) == null)
    {
    node2 = doc.CreateElement(path[i]);
    node.AppendChild(node2);
    }
    node = node2;
    }   
    }

    }
    public void ChildWrite(string key,string Childkey,string value)
    {
    XmlNode node = doc.DocumentElement.SelectSingleNode(key);
    XmlNode node2=node.SelectSingleNode(Childkey);
    if(node2!=null)
    {
    node2.InnerText=value;
    }
    else
    {
    XmlNode node3=doc.CreateElement(Childkey);

    node.PrependChild(node3);
    node3.InnerText=value;

    }
    }
    //key为父节点的值,chlidkey为子节点的值。
    public string Read(string key,string Childkey,string value)
    {

    XmlNode node = doc.DocumentElement.SelectSingleNode(key);
    if(node!=null)
    {
    XmlNode node2=node.SelectSingleNode(Childkey);
    if(node2!=null)
    {
    return node2.InnerText;
    }
    else
    return value;
    }
    else
    return value;
    }

    public XmlNodeList NodeRead()
    {
    XmlNode node = doc.ChildNodes[1];
    XmlNodeList nodelist = node.ChildNodes;
    return nodelist;
    }
    }
      

  2.   

    我每次对XML操作几乎都用XmlDocument类,功能强大易用:
    XmlDocument doc=new XmlDocument();
    doc.load(filename);
    里面有足够多的方法和属性
    也可以用XmlTextWriter,控制更灵活,功能更强大
      

  3.   

    如果会使用dataset,简单的直接使用dataset.ReadXml把xml文件的内容读到dataset中,对dataset操作结束后使用dataset.writexml在写回xml文件就可以了