zoukankan      html  css  js  c++  java
  • C#操作xml文件

    //创建xml文件(添加根节点的属性)

    StreamWriter sw = File.CreateText(AppDomain.CurrentDomain.BaseDirectory + "Xml\\" + xmlFilename + ".xml");

    sw.WriteLine("<?xml version='1.0' encoding='utf-8' ?>");

    sw.WriteLine("<media></media>");

    sw.Close();

    XmlDocument doc = new XmlDocument();

    doc.Load(AppDomain.CurrentDomain.BaseDirectory + "Xml\\" + xmlFilename + ".xml");

    XmlNode nodePath = doc.SelectSingleNode("//media");

    if (!(nodePath == null))

    {

          XmlNode titleAttribute;

          titleAttribute = doc.CreateAttribute("title");

          titleAttribute.Value = TextBox1.Text;

          nodePath.Attributes.SetNamedItem(titleAttribute);

           XmlNode imgAttribute;

           imgAttribute = doc.CreateAttribute("imagesUrl");

           imgAttribute.Value = "~/images/parent.gif";

           nodePath.Attributes.SetNamedItem(imgAttribute);

           RootTitle = TextBox1.Text;

     }

    doc.Save(AppDomain.CurrentDomain.BaseDirectory + "Xml\\" + xmlFilename + ".xml");

    //xml(添加子节点、子节点的属性)

    XmlDocument doc = new XmlDocument();

    doc.Load(AppDomain.CurrentDomain.BaseDirectory + "Xml\\" + xmlFilename + ".xml");

    XmlNode find = doc.SelectSingleNode("//media[@title=\"" + tmp.Text + "\"]");

    if (find == null)

    {

      XmlNode xmlroot = doc.SelectSingleNode("//media[@title=\"" + TreeView1.SelectedNode.Text + "\"]");

      XmlElement parentXmlNode = doc.CreateElement("media");

      xmlroot.AppendChild(parentXmlNode);

      parentXmlNode.SetAttribute("title", TextBox1.Text);

      parentXmlNode.SetAttribute("imagesUrl", "~/images/node.gif");

     doc.Save(AppDomain.CurrentDomain.BaseDirectory + "Xml\\" + xmlFilename + ".xml");

     }

    //读节点属性

    XmlNode aa= doc.SelectSingleNode("//"+addr+"[@code=\"" + code + "\"]");

    private string getXmlAttribute(XmlNode aa)

        {       

            if (aa != null)

            {

                XmlElement aaNode = (XmlElement)aa;

                if (aaNode.HasAttribute("url"))

                {

                   navtmp = "<a href=" + aaNode.Attributes["url"].Value + ">" + aaNode.Attributes["name"].Value + "</a>" + "--->" + navtmp;

       ;   getXmlAttribute(aaNode.ParentNode);

                }

            }

            return navtmp;

        }

    //删除节点

    XmlDocument doc = new XmlDocument();

    doc.Load(AppDomain.CurrentDomain.BaseDirectory + "Xml\\" + xmlFilename + ".xml");

    XmlNode xmlroot = doc.SelectSingleNode("//media[@title=\"" + selectText + "\"]");

    xmlroot.ParentNode.RemoveChild(xmlroot);

    doc.Save(AppDomain.CurrentDomain.BaseDirectory + "Xml\\" + xmlFilename + ".xml");

  • 相关阅读:
    :::宁夏汇川服装有限公司:::
    Zzgen@2015
    新中装
    柯桥有了高级西服定制机构上万元的西服已订出10多套(图)_网易新闻中心
    联系方式-北京美达琳服装有限责任公司
    THOMAS MASON--英国顶级衬衫面料品牌
    tessilstrona
    古特曼上海
    老舍茶馆|北京老舍茶馆|老舍茶馆门票|老舍茶馆官方网站
    SANSA 上上洛可可 贾伟作品 高山流水 香炉 香插香台香具 高端商务礼品 黑色【正品 价格 图片 折扣 评论】_尚品网ShangPin.com
  • 原文地址:https://www.cnblogs.com/newwind521/p/787371.html
Copyright © 2011-2022 走看看