zoukankan      html  css  js  c++  java
  • 在应用程序中使用Xml文件

    用于操作Xml的文档主要有XmlNode、XmlDocument、XmlComment、XmlElement、XmlAttribute、XmlText、XmlNodeList

    下面用一段代码来具体说明怎么操作Xml文件的:

      private void button1_Click(object sender, EventArgs e)
            {
                XmlDocument document = new XmlDocument();
                document.Load("XmlReader");
                textBox1.Text = FormatText(document.DocumentElement as XmlNode, "", "");
            }
    
            private string FormatText(XmlNode node, string text, string indent)
            {
                if (node is XmlText)
                {
                    text += node.Value;
                    return text;
                }
                if (string.IsNullOrEmpty(indent))
                {
                    indent = "";
                }
                else
                {
                    text += "
    " + indent;
                }
                if (node is XmlComment)
                {
                    text += node.OuterXml;
                    return text;
                }
                text += "<" + node.Name;
                if (node.Attributes.Count > 0)
                {
                    AddAttribute(node, ref text);
                }
                if (node.HasChildNodes)
                {
                    text += ">";
                    foreach (XmlNode child in node.ChildNodes)
                    {
                        FormatText(child, text, indent + " ");
                    }
                    if (node.ChildNodes.Count == 1 && (node.FirstChild is XmlText || node.FirstChild is XmlComment))
                    {
                        text += "
    " + indent + "</" + node.Name + ">";
                    }
                    
                }
                else
                {
                    text += "/>";
                }
                return text;
            }
    
            private void AddAttribute(XmlNode node, ref string text)
            {
                foreach (XmlAttribute attribute in node.Attributes)
                {
                    text += " " + attribute.Name + "='" + attribute.Value + "'";
                }
            }
    

      

  • 相关阅读:
    jQuery检测滚动条(scroll)是否到达底部
    sql group by
    hbm.xml 详解总结
    net.sf.json 时间格式的转化
    经典SQL语句大全
    HashTable
    in与exist , not in与not exist 的区别
    网页布局常用的一些命名规则和书写
    什么是SOA?
    sql之left join、right join、inner join的区别
  • 原文地址:https://www.cnblogs.com/simen-tan/p/5389567.html
Copyright © 2011-2022 走看看