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");

  • 相关阅读:
    块拷贝
    Response.AddHeader函数中文件名的中文乱码问题的解决
    c#:如何往List>里添加 Dictionary<string,string>
    java的map中的containsKey 方法——判断是否包含指定的键名
    C# Hashtable 中的ContainsKey()方法
    C#的List的Contains方法 list的Contains方法是根据其元素类型定义的Equals方法来判断是否重复的
    C#从List Dictionary string, string 中查找指定的key,并修改对应的值
    C# 泛型Dictionary<string,string>的用法 ,ContarnsKey() 来判断键是否存存在
    C#去除list中的重复数据 倒叙遍历
    对象引用类型问题
  • 原文地址:https://www.cnblogs.com/newwind521/p/787371.html
Copyright © 2011-2022 走看看