1。不太清楚2.用这样的方法可以访问
XmlNode currNode = doc.DocumentElement;
currNode.ChildNodes[i].AppendChild3,4。其他相关方法PrependChild 将指定的节点添加到该节点子级列表的开头。 
RemoveAll 移除当前节点的所有子级和/或属性。 
RemoveChild 移除指定的子节点。 
ReplaceChild 用 newChild 节点替换子节点 oldChild。 

解决方案 »

  1.   

    例如:我要移去<last-name>Atwood</last-name>,采用什么方法呀?
    用RemoveAll的方法,好像只能清除Atwood,不能清除<last-name></last-name>
    真的不懂,弄了很长时间了!郁闷之极!
      

  2.   

    1. you cannot just add the same instance, you need to create a new node:currNode.FirstChild.AppendChild(newElem);
    currNode.LastChild.AppendChild(newElem.CloneNode(true));>>>我要移去<last-name>Atwood</last-name>,采用什么方法呀XmlNode node = doc.SelectSingleNode("//last-name[.='Atwood']");
    if (node != null)
     node.ParentNode.RemoveChild(node);
      

  3.   

    public class XmlConfig
    {
    private XmlDocument doc;
    private string xmlFileName;
    private MemoryStream xmlStream;
    private byte[] xmlcontent = new byte[0];
    public byte[] Content
    {
    get
    {
    // xmlcontent = new Byte[xmlStream.Length];
    // bool a  = xmlStream.CanRead;
    // byte[] buffer = xmlStream.GetBuffer();
    // System.Array.Copy(buffer,0,xmlcontent,0,xmlcontent.Length);
    // return xmlcontent; }
    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.ASCII.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;
    }
    }