zoukankan      html  css  js  c++  java
  • c# 生成、读取xml

    生成:

    <?xml version="1.0" encoding="utf-8"?>
    <CategoryList>
    <Category ID="01">
    <MainCategory>XML</MainCategory>
    <Description>This is a list my XML articles.</Description>
    <Active>true</Active>
    </Category>
    </CategoryList>

    XmlDocument xmlDoc = new XmlDocument();
    XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0","utf-8",null);
    XmlElement rootNode  = xmlDoc.CreateElement("CategoryList");
    xmlDoc.InsertBefore(xmlDeclaration, xmlDoc.DocumentElement);
    xmlDoc.AppendChild(rootNode);
    XmlElement parentNode  = xmlDoc.CreateElement("Category");
    parentNode.SetAttribute("ID", "01");
    xmlDoc.DocumentElement.PrependChild(parentNode);
    XmlElement mainNode  = xmlDoc.CreateElement("MainCategory");
    XmlElement descNode  = xmlDoc.CreateElement("Description");
    XmlElement activeNode  = xmlDoc.CreateElement("Active");
    XmlText categoryText= xmlDoc.CreateTextNode("XML");
    XmlText descText  = xmlDoc.CreateTextNode("This is a list my XML articles.");
    XmlText activeText  = xmlDoc.CreateTextNode("true");
    parentNode.AppendChild(mainNode);
    parentNode.AppendChild(descNode);
    parentNode.AppendChild(activeNode);
    mainNode.AppendChild(categoryText);
    descNode.AppendChild(descText);
    activeNode.AppendChild(activeText);
    xmlDoc.Save(Server.MapPath("categories.xml"));

    读取:

    try
                {
                    if (File.Exists(versionPath))
                    {
                        XmlDocument doc = new XmlDocument();
                        doc.Load(versionPath);
                        foreach (XmlNode node in doc.SelectSingleNode("citylist").ChildNodes)
                        {
                            versionDic.Add(node.Attributes["code"].Value, Convert.ToInt64(node.Attributes["version"].Value));
                        }
                    }
                }
                catch
                { }

  • 相关阅读:
    Python自动化开发学习的第十周----Redis
    Python自动化开发学习的第九周----线程、进程、协程
    Python自动化开发学习的第八周----socket网络编程
    Python自动化开发学习的第七周---面向对象编程进阶
    Python自动化开发学习的第六周------面向对象学习
    Python自动化开发学习的第五周------模块介绍
    java保留两位有效数字
    java中对象的初始化顺序
    &和&&的区别
    关于Java中遍历map的四种方式
  • 原文地址:https://www.cnblogs.com/94cool/p/2151557.html
Copyright © 2011-2022 走看看