<?xml version="1.0" encoding="utf-8"?>
  <DCWeb>
    <HttpCompress compressionType="GZip">
      <IncludedMimeTypes>
        <!--我要在这里新增或删除这个:<add mime="text/html" />-->
      </IncludedMimeTypes>
    </HttpCompress>
  </DCWeb>不知如何制作?
类如下:
public class XmlUtility
     {
         protected string strXmlFile;
         protected XmlDocument objXmlDoc = new XmlDocument();
 
         public XmlUtility(string XmlFile)
         {
             try
             {
                if (!File.Exists(XmlFile))
                {
                    CreatXmlFile(XmlFile);
                }
                 objXmlDoc.Load(XmlFile);
 
             }
             catch (System.Exception ex)
             {
                 throw ex;
             }
             strXmlFile = XmlFile;
         }
         protected void CreatXmlFile(string file)
         {
             XmlTextWriter writer = new
             XmlTextWriter(file, Encoding.UTF8);
 
             // start writing!
             writer.WriteStartDocument();
             writer.WriteStartElement("Root");
             writer.WriteEndElement();
             writer.WriteEndDocument();
             writer.Close();
         }
         /// <summary>
         /// 查找数据。返回一个DataSet 多条 
         /// </summary>
         /// <param name="XmlPathNode">结点路径</param>
         /// <returns></returns>
         public DataSet GetData(string XmlPathNode)
         {
             //查找数据。返回一个DataSet 
             DataSet ds = new DataSet();
             //=========多个=================
             foreach(XmlNode xmlnode in objXmlDoc.SelectNodes(XmlPathNode))
             {
                 StringReader read = new StringReader(xmlnode.OuterXml);
                 ds.ReadXml(read);
             }
             //==============================
             return ds;
         }
         /// <summary>
         /// 查找数据。返回一个DataSet 单条
         /// </summary>
         /// <param name="XmlPathNode"></param>
         /// <returns></returns>
         public DataSet GetDataSingle(string XmlPathNode)
         {
             //查找数据。返回一个DataSet 
             DataSet ds = new DataSet();
             //==========单个================
             StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
             ds.ReadXml(read);
             //==============================
             return ds;
         }
         /// <summary>
         /// 更新节点内容
         /// </summary>
         /// <param name="XmlPathNode"></param>
         /// <param name="Content"></param>
         public void Replace(string XmlPathNode, string Content)
         {
             //更新节点内容。 
             objXmlDoc.SelectSingleNode(XmlPathNode).InnerText = Content;
         }
         /// <summary>
         /// 删除一个节点
         /// </summary>
         /// <param name="Node"></param>
         public void Delete(string Node)
         {
             //删除一个节点。 
             string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
             objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
         }
         /// <summary>
         /// 插入一个节点和此节点的一子节点
         /// </summary>
         /// <param name="MainNode"></param>
         /// <param name="ChildNode"></param>
         /// <param name="Element"></param>
         /// <param name="Content"></param>
         public void InsertNode(string MainNode, string ChildNode, string Element, string Content)
         {
             //插入一个节点和此节点的一子节点。 
             XmlNode objRootNode = objXmlDoc.SelectSingleNode(MainNode);
             XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
             objRootNode.AppendChild(objChildNode);
             XmlElement objElement = objXmlDoc.CreateElement(Element);
             objElement.InnerText = Content;
             objChildNode.AppendChild(objElement);
         }
         /// <summary>
         /// 插入一个节点,带一属性
         /// </summary>
         /// <param name="MainNode"></param>
         /// <param name="Element"></param>
         /// <param name="Attrib"></param>
         /// <param name="AttribContent"></param>
         /// <param name="Content"></param>
         public void InsertElement(string MainNode, string Element, string Attrib, string AttribContent, string Content)
         {
             //插入一个节点,带一属性。 
             XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
             XmlElement objElement = objXmlDoc.CreateElement(Element);
             objElement.SetAttribute(Attrib, AttribContent);
             objElement.InnerText = Content;
             objNode.AppendChild(objElement);
         }
         /// <summary>
         /// 插入一个节点,不带属性
         /// </summary>
         /// <param name="MainNode"></param>
         /// <param name="Element"></param>
         /// <param name="Content"></param>
         public void InsertElement(string MainNode, string Element, string Content)
         {
             XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
             XmlElement objElement = objXmlDoc.CreateElement(Element);
             objElement.InnerText = Content;
             objNode.AppendChild(objElement);
         }
         /// <summary>
         /// 保存文件
         /// </summary>
         public void Save()
         {
             //保存文件。 
             try
             {
                 objXmlDoc.Save(strXmlFile);
             }
             catch (System.Exception ex)
             {
                 throw ex;
             }
             objXmlDoc = null;
         }
     } 

解决方案 »

  1.   

       XmlUtility xmlUtil = new XmlUtility("test.xml");   // 添加:
       xmlUtil.InsertElement("DCWeb/HttpCompress/IncludedMimeTypes", "add", "mine", "text/html", "");
       xmlUtil.Save();
       
       // 删除:
       xmlUtil.Delete("DCWeb/HttpCompress/IncludedMimeTypes/add");
       xmlUtil.Save();
      

  2.   

      xmlUtil.InsertElement("DCWeb/HttpCompress/IncludedMimeTypes", "add", "mine", "text/html", ""); 
    这么写,出错啊,在InsertElement方法的objNode.AppendChild(objElement);,说未将对象引用设置到对象的实例。
      

  3.   

    <?xml version="1.0" encoding="utf-8"?> 
    <DCWeb> 
         <HttpCompress compressionType="GZip"> 
           <IncludedMimeTypes> 这个XML结构没有变吧。DCWeb/HttpCompress/IncludedMimeTypes 是个XPath,印射XML结构。
      

  4.   

    在InsertElement方法的objNode.AppendChild(objElement);,说未将对象引用设置到对象的实例。就是用“DCWeb/HttpCompress/IncludedM”没有查到这个节点。你查查你的XML是不是你开始写的那样?
      

  5.   

    其实 这个xml是web.config
    开头是:
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
          <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
            <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
            <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
              <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere" />
              <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
              <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
            </sectionGroup>
          </sectionGroup>
        </sectionGroup>
        <sectionGroup name="DCWeb">
          <section name="HttpCompress" type="DC.Web.HttpCompress.Configuration,DC.Web.HttpCompress" />
        </sectionGroup>
      </configSections>
      <DCWeb>
        <HttpCompress compressionType="GZip">
          <IncludedMimeTypes>
            <!--<add mime="text/html" />-->
          </IncludedMimeTypes>
        </HttpCompress>
      </DCWeb>
    ........
      

  6.   

     public class Class2
        {
            public Class2()
            {
            }
            //写用户聊天内容到XML
            //==================================================================================================================
            [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
            public static void writeXML(string username, string id, string content,string value,string datetime,string contentURL)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(contentURL);
                XmlNode xmlNode = xmlDoc.SelectSingleNode("content");
                XmlElement el = xmlDoc.CreateElement("contents"); //添加节点 
                el.SetAttribute("name", username); //添加节点的属性 
                el.SetAttribute("id", id);
                el.SetAttribute("content", content);
                el.SetAttribute("toname",value);
                el.SetAttribute("datetime",datetime);
                el.SetAttribute("biaoji","0");
                xmlNode.AppendChild(el);
                xmlDoc.Save(contentURL);
            }         
            //读聊天纪录
            //==================================================================================================================
            [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
            public static List<string> readerContentXML(string id,string contentURL)
            {
                List<string> listContent = new List<string>();
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(contentURL);
                XmlNode xmlNode = xmlDoc.SelectSingleNode("content");
                XmlNodeList xmlNodeList = xmlNode.ChildNodes;
                foreach (XmlNode xn in xmlNodeList)
                {
                    XmlElement xmlElement = (XmlElement)xn;
                    if (xmlElement.GetAttribute("biaoji").Equals("0"))
                    {
                        //群聊
                        if (xmlElement.GetAttribute("toname") == "")
                        {
                            listContent.Add(xmlElement.GetAttribute("name") + "|" + xmlElement.GetAttribute("content"));
                            //xmlElement.Attributes();
                            xmlElement.RemoveAttributeAt(5);
                            
                            
                        }                    //判断是不是对某个人说
                        else if (xmlElement.GetAttribute("toname") == id)
                        {
                            listContent.Add(xmlElement.GetAttribute("name") + "对您" + "|" + xmlElement.GetAttribute("content"));
                            // xmlElement.Value("biaoji", "1");
                        }
                    }
                    else
                    {
                        continue;
                    }
                }
                xmlDoc.Save(contentURL);
                return listContent;
            }
            //读用户
            //==================================================================================================================
            [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
            public static List<string> readerXML(string usersURL)
            {
                List<string> liststring = new List<string>();
                XmlDocument xmlDocument = new XmlDocument();
                xmlDocument.Load(usersURL);
                XmlNode xmlNode = xmlDocument.SelectSingleNode("users");
                XmlNodeList xmlNodeList = xmlNode.ChildNodes;
                foreach (XmlNode xnf in xmlNodeList)
                {
                    XmlElement xe = (XmlElement)xnf; 
                    liststring.Add(xe.GetAttribute("name")+"|"+xe.GetAttribute("id"));
                }
                return liststring;          
            }
            //用户离开,删除用户XML
            //==================================================================================================================
            [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
            public static void delectXML(string username,string usersURL)
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(usersURL);
                XmlNode xmlNode = xmlDoc.SelectSingleNode("users");
                XmlNodeList xmlNodeList = xmlNode.ChildNodes;
                foreach (XmlNode xn in xmlNodeList)
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.GetAttribute("name")==username)
                    {
                        xmlNode.RemoveChild(xn);
                    }
                }
                xmlDoc.Save(usersURL);
            }
            //删除用户自己聊天纪录
            [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
            public static void delectContenXML(string username,string contentURL)
            {                    List<string> lists = new List<string>();            XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(contentURL);
                XmlNode xmlNode = xmlDoc.SelectSingleNode("content");
                XmlNodeList xmlNodeList = xmlNode.ChildNodes;
                int flag = xmlNodeList.Count;
                for (int i = flag-1; i >=0; i--)
                {
                    XmlNode xn = xmlNodeList[i];
                    XmlElement xe = (XmlElement)xn;
                    if (xe.GetAttribute("name") == username)
                    {
                        xmlNode.RemoveChild(xn);
                    }            }
                xmlDoc.Save(contentURL);
            }    }
    }
    这个都有了
      

  7.   

    找到了,原来开头少了configuration,没看到,唉,tks fangxinggood
      

  8.   

    具体的忘记了
    参考下msdn的XmlDocument和XmlElement这两个的说明就能搞定了