zoukankan      html  css  js  c++  java
  • xml追加节点

    添加方法

    public void XmlAppend(VisitM vm)
    {
    XmlDocument xmldoc = new XmlDocument();
    string path = Server.MapPath("~/Content/VisitFile/VisitFile.xml");
    xmldoc.Load(path);

    XmlElement node = xmldoc.CreateElement("VisitRecord");
    node.SetAttribute("Ip", vm.Ip);
    node.SetAttribute("VisitTime", vm.VisitTime);
    node.SetAttribute("Word", vm.Word);
    //node.SetAttribute("isMobile", vm.isMobile);
    node.InnerText=vm.Word;

    //将节点加入到指定的节点下
    XmlNode xml = xmldoc.DocumentElement.PrependChild(node);
    xmldoc.Save(path);
    }

      protected void Button3_Click(object sender, EventArgs e)
        {

            //加载xml文档
            XmlDocument doc = new XmlDocument();
            string path = Server.MapPath("~/Title.xml");
            doc.Load(path);
            //创建节点
            XmlElement xmlElement = doc.CreateElement("Title");

            //添加属性
            xmlElement.SetAttribute("ID", "21");
            xmlElement.SetAttribute("Name","王六");
            //将节点加入到指定的节点下
            XmlNode xml = doc.DocumentElement.PrependChild(xmlElement);
            doc.Save(path);
        }

    或者是

     protected void Button3_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            string path = Server.MapPath("~/Title.xml");
            doc.Load(path);
            //创建节点
            XmlElement xmlElement = doc.CreateElement("Title");
            ////将节点加入到指定的节点下
            XmlNode xmlTitle = doc.DocumentElement.PrependChild(xmlElement);
            //为该节点加入属性
            XmlAttribute xmlID = doc.CreateAttribute("ID");
            xmlID.Value = "22";
            xmlTitle.Attributes.Append(xmlID);

            XmlAttribute xmlName = doc.CreateAttribute("Name");
            xmlName.InnerText = "小三";
            xmlTitle.Attributes.Append(xmlName);

            doc.Save(path);
        }

    如果不需要创建节点,直接通过SelectSingleNode(string path)来获取XmlNode,然后再添加属性或者文本节点等,如下:

       protected void Button2_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            string path = Server.MapPath("~/XMLFile.xml");
            doc.Load(path);
            //创建一个book节点
            XmlNode xml = doc.SelectSingleNode("//TiTles//TiTle");
            XmlAttribute xmlAttribute = doc.CreateAttribute("ss");
            xmlAttribute.InnerText = "bb";
            xml.Attributes.Append(xmlAttribute);
            doc.Save(path);
        }

  • 相关阅读:
    C#获取Excel Sheet名称,对特殊字符、重名进行了处理
    10个你必须知道的jQueryMobile代码片段
    HTML 5 学习之应用程序缓存
    JS取地址栏参数的两种方法
    关于AJAX+HTML5+ASHX进行全静态页面的数据交互
    重病后的重生
    非常值得学习的java 绘图板源代码
    C#开发者通用性代码审查清单
    【week3】四人小组项目—东师论坛
    【week2】结对编程-四则运算 及感想
  • 原文地址:https://www.cnblogs.com/jinhaoObject/p/4860021.html
Copyright © 2011-2022 走看看