Use the tree structure to display all the content of the XML document on the console

Source: Internet
Author: User
Tags connectionstrings

The XML file is:

<? 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>

 

The. CS file code is:

/*
* Display all content of the XML document on the console using a Tree Structure
*/

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 ();


// Parse the XML file, which can be obtained through the file name, byte stream, or response stream
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 );

// Output Node itself
Console. writeline ("Name: {0}, type: {1}, value: {2}", node. Name,
Node. nodetype,
Node. value );

// Determine whether the element is used to process the attributes of the element
If (node. nodetype = xmlnodetype. element)
{
Xmlelement element = node as xmlelement;
Foreach (xmlattribute ATT in element. attributes)
{
Shownode (ATT, spacecount + 2 );
}
}

Console. writeline ("-----------------------------------------");

// Process Child Nodes
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 );
}
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.