• 大小: 20.7 KB
    文件类型: .rar
    金币: 1
    下载: 1 次
    发布日期: 2024-11-20
  • 语言: C#
  • 标签: XmlDocument  

资源简介

遍历XmlDocument对象所有节点名称、类型、属性(Attribute)

资源截图

代码片段和文件信息

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.xml;
using static System.Net.Mime.MediaTypeNames;

namespace xmlDocoument
{
    class Program
    {
        static void Main(string[] args)
        {
            System.xml.xmlDocument doc = new System.xml.xmlDocument();
            doc.Load(“../../UpdateActivity2.xml“);//doc.Load(文件):从文件加载 doc。doc.Loadxml(字符串)从字符串加载
            UpdateActivity2(doc);
        }
        /// 
        /// 遍历所有节点的type、name、Attributes
        /// 

        /// 
        static void PrintChildNodes(xmlNodeList childnodelist)
        {
            try
            {
                foreach (xmlNode node in childnodelist)
                {
                    //(node.NodeType 是Text时,即是最内层 即innertext值,node.Attributes为null。
                    if (node.NodeType == xmlNodeType.Text)
                    {
                        Console.WriteLine(“NodeType:“ + node.NodeType + “\t“ + node.Name + “\t:“ + node.Value);
                        continue;
                    }
                    Console.WriteLine(“==========node.Name:“ + node.Name + “===========“);
                    foreach (xmlAttribute atr in node.Attributes)
                    {
                        Console.WriteLine(“NodeType:“+atr.NodeType+“\t“+ atr.Name + “\t:“ + atr.Value);
                    }
                    if (node.ChildNodes.Count > 0)
                    {
                        PrintChildNodes(node.ChildNodes);
                    }
                }
            }
            catch (Exception ex )
            {
                Console.WriteLine(ex.Message);
            }
        }
        /// 
        /// 测试方法
        /// 

        /// xmlDocument对象
        public static void UpdateActivity2(xmlDocument doc)
        {
            string xx = doc.Innerxml;
            //Activity2
            string activity2ID = ““;
            string docid = ““;
            string docTypeid = ““;
            #region MyRegion
            //获取delete的节点
            //doc.SelectNodes(“//Relationships/Item[@action=‘delete‘ and @type=‘Activity2 Deliverable‘]/related_id[@type=‘Deliverable‘]/Item[@type=‘Document‘ or @type=‘CAD‘]“)
            //获取add的节点
            //doc.SelectNodes(“//Relationships/Item[@action=‘add‘ and @type=‘Activity2 Deliverable‘]“) 
            #endregion

            xmlNodeList Activity2Nodes = doc.SelectNodes(“//Item[@type=‘Activity2‘]“);
            ////遍历Activity2Nodes中的所有节点
            //foreach (xmlNode node in Activity2Nodes)
            //{
            //    Console.WriteLine(“==========“+node.Name+“===========“);
            //    foreach (xmlAttribute atr in node.Attributes)
            //    {
            //        Console.WriteLine(atr.Name+ “\t:“ +atr.Value);
            //    }
  

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       3584  2017-09-30 13:32  xmlDocoument\.vs\xmlDocoument\v15\.suo

     文件        187  2017-09-27 18:21  xmlDocoument\App.config

     文件       7168  2017-09-30 13:28  xmlDocoument\bin\Debug\xmlDocoument.exe

     文件        187  2017-09-27 18:21  xmlDocoument\bin\Debug\xmlDocoument.exe.config

     文件      13824  2017-09-30 13:28  xmlDocoument\bin\Debug\xmlDocoument.pdb

     文件        711  2017-09-30 13:19  xmlDocoument\obj\Debug\DesignTimeResolveAssemblyReferences.cache

     文件       6994  2017-09-30 13:32  xmlDocoument\obj\Debug\DesignTimeResolveAssemblyReferencesInput.cache

     文件          0  2017-09-27 18:21  xmlDocoument\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs

     文件          0  2017-09-27 18:21  xmlDocoument\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs

     文件          0  2017-09-27 18:21  xmlDocoument\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

     文件         42  2017-09-30 13:32  xmlDocoument\obj\Debug\xmlDocoument.csproj.CoreCompileInputs.cache

     文件        991  2017-09-29 17:48  xmlDocoument\obj\Debug\xmlDocoument.csproj.FileListAbsolute.txt

     文件       1904  2017-09-28 09:11  xmlDocoument\obj\Debug\xmlDocoument.csprojResolveAssemblyReference.cache

     文件       7168  2017-09-30 13:28  xmlDocoument\obj\Debug\xmlDocoument.exe

     文件      13824  2017-09-30 13:28  xmlDocoument\obj\Debug\xmlDocoument.pdb

     文件       3917  2017-09-30 13:28  xmlDocoument\Program.cs

     文件       1322  2017-09-27 18:21  xmlDocoument\Properties\AssemblyInfo.cs

     文件      19034  2017-09-30 13:30  xmlDocoument\UpdateActivity2.xml

     文件       2488  2017-09-29 10:42  xmlDocoument\xmlDocoument.csproj

     文件        257  2017-09-27 18:21  xmlDocoument\xmlDocoument.csproj.vspscc

     目录          0  2017-09-30 13:32  xmlDocoument\.vs\xmlDocoument\v15

     目录          0  2017-09-27 18:21  xmlDocoument\obj\Debug\TempPE

     目录          0  2017-09-30 13:32  xmlDocoument\.vs\xmlDocoument

     目录          0  2017-09-30 13:31  xmlDocoument\bin\Debug

     目录          0  2017-09-30 13:31  xmlDocoument\obj\Debug

    ...D.H.         0  2017-09-30 13:32  xmlDocoument\.vs

     目录          0  2017-09-30 13:31  xmlDocoument\bin

     目录          0  2017-09-30 13:31  xmlDocoument\obj

     目录          0  2017-09-30 13:31  xmlDocoument\Properties

     目录          0  2017-09-30 13:32  xmlDocoument

............此处省略3个文件信息

评论

共有 条评论