C # retrieving XML file nodes net XML node Traversal

Source: Internet
Author: User
Tags net xml
# Region traversal XML file call method xmlfileallnodes (server. mappath ("treeout. xml "),"")
/// <Summary>
/// Add the temporary level of the topic
/// </Summary>
Int xmlfileallnodescloumnlvtemp = 0;

/// <Summary>
/// Temporary ID of the added topic
/// </Summary>
Int xmlfileallnodescolumnsidtemp = 0;

/// <Summary>
/// Add a column function
/// </Summary>
/// <Param name = "xmlpath"> XML topic file </param>
/// <Param name = "xmldata"> null "" </param>
/// <! [CDATA [Call method xmlfileallnodes (server. mappath ("treeout. xml"), "")]>
/// <Returns> </returns>
Public String xmlfileallnodes (string xmlpath, string xmldata)
{
System. xml. xmldatadocument myxml = new system. xml. xmldatadocument ();
If (xmldata = "")
{
Myxml. Load (xmlpath); // get XML data
}
Else
{
Myxml. loadxml (xmldata );
}

For (INT I = 0; I <myxml. documentelement. childnodes. Count; I ++)
{

Try
{// Obtain the Tree Structure
If (xmlfileallnodescloumnlvtemp = 0) // Level 1 Node
{
System. web. httpcontext. current. response. write (textdata. estr (xmlfileallnodescloumnlvtemp, "& nbsp;") + xmlfileallnodescolumnsidtemp + myxml. documentelement. childnodes [I]. attributes. getnameditem ("Cont "). value + xmlfileallnodescloumnlvtemp + "<br/> ");
}
Else
{

System. web. httpcontext. current. response. write (textdata. estr (xmlfileallnodescloumnlvtemp, "& nbsp;") + xmlfileallnodescolumnsidtemp + myxml. documentelement. childnodes [I]. attributes. getnameditem ("Cont "). value + xmlfileallnodescloumnlvtemp + "<br/> ");
// If it is a subnode

}
Xmlfileallnodescolumnsidtemp + = 1;
Xmlfileallnodescloumnlvtemp ++;
Xmlfileallnodes (xmlpath, myxml. documentelement. childnodes [I]. outerxml); // obtain all the subnodes in the XML file.
Xmlfileallnodescloumnlvtemp --;
}
Catch (system. xml. xmlexception) // if this is the lowest layer, you will not make the next call.
{
}
}
Return "";
}
# Endregion

 

XML file

<? XML version = "1.0" encoding = "UTF-8"?>
<Addcolumns>
<Column name = "add topic name 1" Lx = "topic category" dir = "" PIC = "topic image" itempadderss = "topic template address" itempparam = "" tabname =" "Cont =" 'djflksjdlfjl & amp; 1 = & quot; ">
</Column>

</Addcolumns>

 

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.