XMLData.xml
<?xml version="1.0" encoding="utf-8" ?> 
- <XMLData>
  <CL LKey="2011-4-1 19:47:11" LValue="192.640716552734" /> 
  <CL LKey="" LValue="" /> 
  <CL LKey="" LValue="" /> 
  <CL LKey="" LValue="" /> 
  <CL LKey="" LValue="" /> 
  <CL LKey="" LValue="" /> 
<XMLData>
如何在.cs(c#)后台向XMLData.xml文件批量插入数据????类似第一条那样。求大侠相助!!!!
谢谢了!!!!在线等!!!!

解决方案 »

  1.   

    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml;namespace PuTianCheng
    {
        /// <summary>
        /// XmlHelper 的摘要说明
        /// </summary>
        public class XmlHelper
        {
            public XmlHelper()
            {
            }        /// <summary>
            /// 读取数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
            /// <returns>string</returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Read(path, "/Node", "")
             * XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
             ************************************************/
            public static string Read(string path, string node, string attribute)
            {
                string value = "";
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
                }
                catch { }
                return value;
            }        /// <summary>
            /// 插入数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
            /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
            /// <param name="value">值</param>
            /// <returns></returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Insert(path, "/Node", "Element", "", "Value")
             * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
             * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
             ************************************************/
            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 { }
            }        /// <summary>
            /// 修改数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="attribute">属性名,非空时修改该节点属性值,否则修改节点值</param>
            /// <param name="value">值</param>
            /// <returns></returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Insert(path, "/Node", "", "Value")
             * XmlHelper.Insert(path, "/Node", "Attribute", "Value")
             ************************************************/
            public static void Update(string path, string node, string attribute, string value)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    XmlElement xe = (XmlElement)xn;
                    if (attribute.Equals(""))
                        xe.InnerText = value;
                    else
                        xe.SetAttribute(attribute, value);
                    doc.Save(path);
                }
                catch { }
            }        /// <summary>
            /// 删除数据
            /// </summary>
            /// <param name="path">路径</param>
            /// <param name="node">节点</param>
            /// <param name="attribute">属性名,非空时删除该节点属性值,否则删除节点值</param>
            /// <param name="value">值</param>
            /// <returns></returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Delete(path, "/Node", "")
             * XmlHelper.Delete(path, "/Node", "Attribute")
             ************************************************/
            public static void Delete(string path, string node, string attribute)
            {
                try
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);
                    XmlNode xn = doc.SelectSingleNode(node);
                    XmlElement xe = (XmlElement)xn;
                    if (attribute.Equals(""))
                        xn.ParentNode.RemoveChild(xn);
                    else
                        xe.RemoveAttribute(attribute);
                    doc.Save(path);
                }
                catch { }
            }
        }
    }参考
      

  2.   

     /// <summary>
            /// 插入数据
            /// </summary>
            /// <param name="node">节点</param>
            /// <param name="element">元素名,非空时插入新元素,否则在该元素中插入属性</param>
            /// <param name="attribute">属性名,非空时插入该元素属性值,否则插入元素值</param>
            /// <param name="value">值</param>
            /// <returns></returns>
            /**************************************************
             * 使用示列:
             * XmlHelper.Insert(path, "/Node", "Element", "", "Value")
             * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value")
             * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value")
             ************************************************/
            public  void Insert(string node, string element, string attribute, string value)
            {
                try
                {
                    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(_XmlPath);
                }
                catch { }
            }
      

  3.   

    XmlDocument xml=new XmlDocument();
    xml.Load(Server.MapPath("~/test.xml"));
    XmlNodeList cls=xml.GetElementsByTagName("CL");
    for(int i=0;i<cls.Count;i++)
    {
        cls[i].Attributes["LKey"].Value="LKey"+i.ToString();
        cls[i].Attributes["LValue"].Value="LValue"+i.ToString();
    }
    xml.Save(Server.MapPath("~/test.xml"));
      

  4.   


    插入数据之前代码:
    <?xml version="1.0" encoding="utf-8"?>
    2<Expressions>
    3  <Expression id="1" name="a+b" re="Test One">
    4    <Param name="a" re="test1">
    5    </Param>
    6    <Param name="b" re="test2">
    7    </Param>
    8  </Expression>
    9</Expressions>
    数操作插入数据的代码:string sFileName = "TectDemo/ExpXml.xml";
     2            XmlDocument xmldoc = new XmlDocument();
     3            xmldoc.Load(sFileName);
     4            XmlNode node = xmldoc.DocumentElement.ChildNodes[0].CloneNode(true);
     5            node.Attributes["id"].Value = "2";
     6            node.Attributes["name"].Value = "Test Two";
     7            node["Param"].Attributes["name"].Value = "test1";
     8            node["Param"].Attributes["name"].Value = "test2";
     9            xmldoc.DocumentElement.AppendChild(node);
    10            xmldoc.Save(sFileName);
    11
    12            Response.Write("插入成功");
    操作成功后XML文件代码为:
    <?xml version="1.0" encoding="utf-8"?>
     2<Expressions>
     3  <Expression id="1" name="a+b" re="Test One">
     4    <Param name="a" re="test1">
     5    </Param>
     6    <Param name="b" re="test2">
     7    </Param>
     8  </Expression>
     9  <Expression id="2" name="Test Two" re="Test One">
    10    <Param name="test2" re="test1">
    11    </Param>
    12    <Param name="b" re="test2">
    13    </Param>
    14  </Expression>
    15</Expressions>http://www.cnblogs.com/jamsewang/archive/2009/08/11/1543668.html