zoukankan      html  css  js  c++  java
  • C# XML操作

    public static void Main()   
            {
                string ids="1,2,3,4,5,6,9";
                string filePath = "c://c/d/qq.xml";
                string xmlPath = "getsomething/news/list/store";
                string subString = xmlPath.Substring(0,xmlPath.LastIndexOf('/'));
                string now = xmlPath.Substring(xmlPath.LastIndexOf('/')+1);
                
                if (!IsExists(filePath))
                {
                    Exists(filePath);
                }
                doc.Load(filePath);
                xml = (XmlElement)doc.FirstChild;
                string parentPath = xmlPath.Substring(0, xmlPath.LastIndexOf('/'));
                string nowNode = xmlPath.Substring(xmlPath.LastIndexOf('/') + 1);
                XmlNode node = doc.SelectSingleNode(parentPath);
                XmlNode oldNode = doc.SelectSingleNode(xmlPath);
                if (node == null)
                {
                    node = CreateXmlNode(parentPath);
                    XmlElement el = doc.CreateElement(nowNode);
                    XmlAttribute attr = doc.CreateAttribute("ids");
                    attr.Value = ids;
                    el.Attributes.Append(attr);
                    node.AppendChild(el);
                }
                else
                {
                    XmlElement el = doc.CreateElement(nowNode);
                    XmlAttribute attr = doc.CreateAttribute("ids");
                    attr.Value = ids;
                    el.Attributes.Append(attr);
                    if (oldNode == null)
                    {
                        node.AppendChild(el);
                    }
                    else
                    {
                        node.ReplaceChild(el, oldNode);
                    }
                }
                doc.Save(filePath);
                Console.ReadKey();
            }
    
    
     public static XmlNode CreateXmlNode(string xmlPath)
            {
                string[] xmls = xmlPath.Split('/');
                string root = "";
                XmlNode parent =xml;
                for (int i = 0; i < xmls.Length;i++)
                {
                    XmlNode node = doc.DocumentElement.SelectSingleNode(root+"/"+xmls[i]);
                    if (node == null)
                    {
                        XmlElement xmle = doc.CreateElement(xmls[i]);
                        parent.AppendChild(xmle);
                    }
                    root = root + "/" + xmls[i];
                    parent =doc.DocumentElement.SelectSingleNode(root);
                }
                return parent;
            }
  • 相关阅读:
    MySQL之数据表的插入内容 空与非空(六)
    输出杨辉三角形
    输入三个double型的数据,放入到a,b,c三个变量中去,使用条件结构与交换逻辑将这三个变量中的值从小到大排列。
    软件测试
    过程设计工具
    设计原理
    总体设计
    生活,也让别人生活
    计算器案例
    需求分析
  • 原文地址:https://www.cnblogs.com/steben/p/3256312.html
Copyright © 2011-2022 走看看