Asp.Net教程,WinForm教程,Asp.Net MVC,vs2008教程,vs2010教程,Silverlight技术,源码下载,Asp.Net视频教程
全站热门标签
vs2010 Silverlight 存储过程 水晶报表 LINQ ADO.NET JavaScript DataGridView GridView AjaxPro DevExpress 面向对象 Extjs XML HTML教程 Oracle jQuery WPF MVC 分页 Office2010 GDI+ Visual C++2010 Dojo MySQL VB.NET WCF4.0 ASPxGridView WinForm textbox Sql2005 cookie Discuz!NT checkbox WCF SQL经典语句 T-SQL F# asp.net VS2008 SQL Ajax DropDownList VS2008新特性 TreeView Access Flex 页面执行时间 字符串 DataSet VB2005 回调 动画 C#时间 游戏 ASP.NET MVC
尚未分类 LINQ教程 Enterprise技术 性能优化/调试 水晶报表与打印 安全与加密 图形图像 文件处理 基础教程 Web Services 内置对象 控件示例 正则表达式\采集 ADO.NET 缓存\泛型\线程XML技术 Url重写\静态页 vs2008综合教程
当前位置: 主页 > ASP.NET教程 > XML技术 >

通过树形结构在控制台显示XML文档的全部内容

时间:2011-12-30 20:29来源:未知 作者:admin 点击:

xml 文件为:

<?xml version="1.0"?>
<configuration>
 <configSections>
  <section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
 </configSections>
 <connectionStrings>
  <add name="cnblogsDb" connectionString="Data Source=192.168.0.88;Initial Catalog=cnblogs;Integrated Security=True" providerName="System.Data.SqlClient"/>
 </connectionStrings>
 <appSettings/>
 <system.web>
  <compilation debug="false">
   <assemblies>
    <add assembly="System.Transactions, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System.Management, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
    <add assembly="System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
    <add assembly="System.Configuration.Install, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
   </assemblies>
  </compilation>
  <authentication mode="Windows"/>
 </system.web>
</configuration>

 

.cs文件代码为:

/*
 * 通过树形结构在控制台显示XML文档的全部内容
 */

using System;
using System.Collections.Generic;
using System.Text;

using System.Xml;

namespace displayxmlDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            System.Xml.XmlDocument doc = new XmlDocument();

           
            //  解析xml 文件,可以通过文件名,字节流,或者字符流获取
            doc.Load("..\\..\\xmlfile1.xml");
            //doc.Load(new System.IO.FileStream("http://www.cnblogs.com/xmlfile1.xml",
            //    System.IO.FileMode.Open));
            //doc.Load(new System.IO.StreamReader("http://www.cnblogs.com/xmlfile1.xml"));

            ShowNode(doc, 0);
        }

        private static void ShowNode(XmlNode node, int spacecount)
        {
            Space(spacecount);

            //输出节点本身
            Console.WriteLine("Name:{0},Type :{1},Value:{2}", node.Name,
                node.NodeType,
                node.Value);

            //判断是否元素,处理元素的属性
            if (node.NodeType == XmlNodeType.Element)
            {
                XmlElement element = node as XmlElement;
                foreach (XmlAttribute att in element.Attributes)
                {
                    ShowNode(att, spacecount + 2);
                }
            }

            Console.WriteLine("-----------------------------------------");

            //处理孩子节点
            foreach (XmlNode n in node.ChildNodes)
            {
                ShowNode(n, spacecount + 5);
            }
        }

        private static void Space(int count)
        {
            for (int i = 0; i < count; i++)
            {
                Console.Write(i);
            }
        }
    }
}

Tags:XML
责任编辑:admin
返回顶部
------分隔线----------------------------
推荐内容
骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价 骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价