Asp. NET to create and use Web Components dynamically based on XML (i)

Source: Internet
Author: User
asp.net|web|xml| Create | dynamic ASP. NET to create the use of Web Components dynamically based on XML

A

Author: Shing

Some time ago the author in the development of the need to dynamically create Web Components, this thought is a trivial matter, who knows that easy to do when it is difficult to do. There's really a little problem in there. The following is a combination of my own program to introduce how to create and use the Web Components dynamically, I hope to do a similar job for friends to provide a little help.

First, the process of thinking

The procedure consists of three main parts:

1, the program to determine the number of Web components that need to be created according to the data information in the XML.

2, the dynamic creation of Web Components.

3. Use dynamically created Web Components.

2 and 3 of them are the part that I want to focus on.

The following author on the basis of these three parts of the combined program example (C # as an example) to introduce.



Second, read XML file

Read the XML file in a lot of information are detailed description, and believe that many friends have a good grasp of their technology. But in order to ensure the integrity of the article, I still have to repeat a few words here. A friend who has a deep taste can skip this paragraph without looking.

The XML file to be read in the author's program is as follows:

Config.xml

<?xml version= "1.0"?>

<Root>

<Nettype>net</Nettype>

<Totalnum>6</Totalnum>

<Cells>2</Cells>

<IPlink>

<Name> site 1</name>

<IP>192.8.198.1</IP>

<Sequence>1</Sequence>

</IPlink>

<IPlink>

<Name> site 2</name>

<IP>192.8.198.2</IP>

<Sequence>2</Sequence>

</IPlink>

... ...

</Root>










































The program reading the XML file is as follows:

protected void Readconfig ()

{

Try

{

System.Xml.XmlDocument mxmldoc=new System.Xml.XmlDocument ();

Mxmldoc.load (Server.MapPath (ConfigFilePath));

Nettype=mxmldoc.selectnodes ("//root/nettype") [0]. InnerText; Totalnum=int. Parse (Mxmldoc.selectnodes ("//root/totalnum") [0]. InnerText);

Read the column number

Cells=int. Parse (Mxmldoc.selectnodes ("//root/cells") [0]. InnerText);

XmlNodeList mxmlnodes=mxmldoc.selectnodes ("//root/iplink");

foreach (XmlNode iplinkchildlnode in Mxmlnodes)

{

Get serial number

int Icount=int. Parse (Iplinkchildlnode.childnodes[2]. InnerText);

According to the serial number, place the name of the measurement point in the appropriate location on the name array

Namestr[icount]=iplinkchildlnode.childnodes[0]. InnerText;

The IP of the measurement point is placed in the appropriate position on the IP array according to the serial number

IPSTR[ICOUNT]=IPLINKCHILDLNODE.CHILDNODES[1]. InnerText;

}

}

Catch

{

Errmessage. Innerhtml= "<table align=center><tr>

&LT;TD Align=left><font color=red> can not read the configuration file, possible error is <br> "+" 1, configuration file does not exist <br> "+

"2, the contents of the configuration file is corrupted" +

"</font></td></tr></table>";

}

}


































































Elements in the program that have no child nodes in the XML are as follows:

<Nettype>net</Nettype>










Read directly using the following statement.

Mxmldoc.selectnodes ("//root/nettype") [0]. InnerText;










For elements that have child nodes such as:

<IPlink>

<Name> site 1</name>

<IP>192.8.198.1</IP>

<Sequence>1</Sequence>

</IPlink>
















To use the statement as read down.

Iplinkchildlnode.childnodes[n]. InnerText










where [N] in childnodes[n] is the ordinal of a child node, a child node

<Name> site 1</name>










The serial number should be [0].



Related Article

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.