学习: .NET XML实例演练,遍历XML文档和构造XML文档

学习: .NET XML实例演练,遍历XML文档和构造XML文档

撰写日期:9:29 2016/4/26
更新日期:9:29 2016/4/26
发布地址:http://www.cnblogs.com/gibbonnet/p/5434794.html

任务:

  • 从XMLDocument遍历为Collection
  • 从Collection构造XMLDocument

源代码

using System;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using System.IO;

namespace DemoXML
{
    /// <summary>
    /// 演练XML,将XML构造为集合,从集合构造XML
    /// </summary>
    class Program
    {
        public static void Main()
        {
            // 创建XMl文档并载入内容
            XmlDocument xd = new XmlDocument();
            xd.LoadXml(File.ReadAllText(@"schema.xml"));

            // 从XML文档提取Node集合
            List<XmlNode> nodeCollection = new List<XmlNode>();
            Traverse(xd.DocumentElement, nodeCollection);

            // 从Node集合构造XML文档,并输出
            XmlDocument newXml = BuildXml(nodeCollection);
            File.WriteAllText(@"out.xml", newXml.OuterXml);
        }
        
        /// <summary>
        /// 深度优先遍历XML文档,提取节点集合
        /// </summary>
        /// <param name="node">根节点</param>
        /// <param name="list">节点集合</param>
        public static void Traverse(XmlNode node, List<XmlNode> list)
        {
            // 忽略空白节点
            if (node.NodeType == XmlNodeType.SignificantWhitespace) return;
            list.Add(node);
            // 遍历子节点
            foreach (XmlNode child in node.ChildNodes)
            {
                Traverse(child, list);
            }
        }
        
        /// <summary>
        /// 从元素集合创建新的XMLDocument
        /// </summary>
        /// <param name="list">深度优先遍历提取的节点集合</param>
        /// <returns></returns>
        public static XmlDocument BuildXml(List<XmlNode> list)
        {
            XmlDocument newDoc = new XmlDocument();
            // 堆栈,用于存储已经创建的节点,作为后面节点的父节点
            Stack<XmlNode> createdNodes = new Stack<XmlNode>();
            foreach(XmlNode node in list)
            {
                // 元素节点
                if (node.NodeType == XmlNodeType.Element)
                {
                    XmlElement newNode = newDoc.CreateElement(node.Name);
                    // 设置属性
                    foreach(XmlAttribute att in node.Attributes)
                    {
                        newNode.SetAttribute(att.Name, att.Value);
                    }
                    newNode.InnerText = "";
                    if (createdNodes.Count == 0)
                    {
                        newDoc.AppendChild(newNode);
                    }
                    else
                    {
                        // 非父节点出栈
                        while (createdNodes.Peek().Name != node.ParentNode.Name)
                        {
                            createdNodes.Pop();
                        }
                        createdNodes.Peek().AppendChild(newNode);
                    }
                    // 新节点入栈
                    createdNodes.Push(newNode);
                }

                // 文本节点
                if (node.NodeType == XmlNodeType.Text)
                {
                   if(node.ParentNode.Name == createdNodes.Peek().Name)
                    {
                        createdNodes.Peek().InnerText = node.Value;
                    }
                }
            }
            return newDoc;
        }

    }
}

输入

<?xml version="1.0" encoding="utf-8" ?>
<do_main0217List xml:space="preserve">
    <do_main0217 xml:space="preserve">
        <NM description="内码" type="string"></NM>
        <Creator description="创建人" type="string"></Creator>
        <CreatedDate description="创建时间" type="dateTime">0001-01-01T00:00:00+08:00</CreatedDate>
        <LastModifier description="最后修改人" type="string"></LastModifier>
        <LastModifiedDate description="最后修改时间" type="dateTime">0001-01-01T00:00:00+08:00</LastModifiedDate>
        <Code description="编号" type="string"></Code>
        <Name description="名称" type="string"></Name>
        <Djzt description="单据状态" type="integer">0</Djzt>
        <Lcsl description="流程实例" type="string"></Lcsl>
        <do_congbiao0217List xml:space="preserve">
            <do_congbiao0217 xml:space="preserve">
                <NM description="内码" type="string"></NM>
                <Creator description="创建人" type="string"></Creator>
                <CreatedDate description="创建时间" type="dateTime">0001-01-01T00:00:00+08:00</CreatedDate>
                <LastModifier description="最后修改人" type="string"></LastModifier>
                <LastModifiedDate description="最后修改时间" type="dateTime">0001-01-01T00:00:00+08:00</LastModifiedDate>
                <Code description="编号" type="string"></Code>
                <Name description="名称" type="string"></Name>
                <PNM description="父内码" type="string"></PNM>
            </do_congbiao0217>
        </do_congbiao0217List>
        <do_congbiao0325List xml:space="preserve">
            <do_congbiao0325 xml:space="preserve">
                <NM description="内码" type="string"></NM>
                <Creator description="创建人" type="string"></Creator>
                <CreatedDate description="创建时间" type="dateTime">0001-01-01T00:00:00+08:00</CreatedDate>
                <LastModifier description="最后修改人" type="string"></LastModifier>
                <LastModifiedDate description="最后修改时间" type="dateTime">0001-01-01T00:00:00+08:00</LastModifiedDate>
                <Code description="编号" type="string"></Code>
                <Name description="名称" type="string"></Name>
                <PNM description="父内码" type="string"></PNM>
            </do_congbiao0325>
        </do_congbiao0325List>
    </do_main0217>
</do_main0217List>

输出

<do_main0217List xml:space="preserve"><do_main0217 xml:space="preserve"><NM description="内码" type="string"></NM><Creator description="创建人" type="string"></Creator><CreatedDate description="创建时间" type="dateTime">0001-01-01T00:00:00+08:00</CreatedDate><LastModifier description="最后修改人" type="string"></LastModifier><LastModifiedDate description="最后修改时间" type="dateTime">0001-01-01T00:00:00+08:00</LastModifiedDate><Code description="编号" type="string"></Code><Name description="名称" type="string"></Name><Djzt description="单据状态" type="integer">0</Djzt><Lcsl description="流程实例" type="string"></Lcsl><do_congbiao0217List xml:space="preserve"><do_congbiao0217 xml:space="preserve"><NM description="内码" type="string"></NM><Creator description="创建人" type="string"></Creator><CreatedDate description="创建时间" type="dateTime">0001-01-01T00:00:00+08:00</CreatedDate><LastModifier description="最后修改人" type="string"></LastModifier><LastModifiedDate description="最后修改时间" type="dateTime">0001-01-01T00:00:00+08:00</LastModifiedDate><Code description="编号" type="string"></Code><Name description="名称" type="string"></Name><PNM description="父内码" type="string"></PNM></do_congbiao0217></do_congbiao0217List><do_congbiao0325List xml:space="preserve"><do_congbiao0325 xml:space="preserve"><NM description="内码" type="string"></NM><Creator description="创建人" type="string"></Creator><CreatedDate description="创建时间" type="dateTime">0001-01-01T00:00:00+08:00</CreatedDate><LastModifier description="最后修改人" type="string"></LastModifier><LastModifiedDate description="最后修改时间" type="dateTime">0001-01-01T00:00:00+08:00</LastModifiedDate><Code description="编号" type="string"></Code><Name description="名称" type="string"></Name><PNM description="父内码" type="string"></PNM></do_congbiao0325></do_congbiao0325List></do_main0217></do_main0217List>

外接

XmlNoteType枚举

        //
        // 摘要:
        //     如果未调用 Read 方法,则由 System.Xml.XmlReader 返回。
        None = 0,
        //
        // 摘要:
        //     元素(例如,<item>)。
        Element = 1,
        //
        // 摘要:
        //     特性(例如,id='123')。
        Attribute = 2,
        //
        // 摘要:
        //     节点的文本内容。
        Text = 3,
        //
        // 摘要:
        //     CDATA 节(例如,<![CDATA[my escaped text]]>)。
        CDATA = 4,
        //
        // 摘要:
        //     实体引用(例如,&num;)。
        EntityReference = 5,
        //
        // 摘要:
        //     实体声明(例如,<!ENTITY...>)。
        Entity = 6,
        //
        // 摘要:
        //     处理指令(例如,<?pi test?>)。
        ProcessingInstruction = 7,
        //
        // 摘要:
        //     注释(例如,<!-- my comment -->)。
        Comment = 8,
        //
        // 摘要:
        //     作为文档树的根的文档对象提供对整个 XML 文档的访问。
        Document = 9,
        //
        // 摘要:
        //     由以下标记指示的文档类型声明(例如,<!DOCTYPE...>)。
        DocumentType = 10,
        //
        // 摘要:
        //     文档片段。
        DocumentFragment = 11,
        //
        // 摘要:
        //     文档类型声明中的表示法(例如,<!NOTATION...>)。
        Notation = 12,
        //
        // 摘要:
        //     标记间的空白。
        Whitespace = 13,
        //
        // 摘要:
        //     混合内容模型中标记间的空白或 xml:space="preserve" 范围内的空白。
        SignificantWhitespace = 14,
        //
        // 摘要:
        //     末尾元素标记(例如,</item>)。
        EndElement = 15,
        //
        // 摘要:
        //     由于调用 System.Xml.XmlReader.ResolveEntity 而使 XmlReader 到达实体替换的末尾时返回。
        EndEntity = 16,
        //
        // 摘要:
        //     XML 声明(例如,<?xml version='1.0'?>)。
        XmlDeclaration = 17
原文地址:https://www.cnblogs.com/gibbonnet/p/5434794.html