C # dynamically generate xml and read it using JavaScript on the foreground

Source: Internet
Author: User

Considering that using dynamic pages on the homepage affects server performance, JavaScript is used to dynamically update the homepage.

Principle: Take press publication as an example. When a piece of news is sent in the background, an XML file is dynamically generated by accessing the database to save news information. When the front-end page is opened, use JavaScript to read and display XML files.

BackgroundCode:

Sseentities _ DB = new sseentities ();
VaR newstoview = (from N in _ dB. News select N). toarray (). Reverse (). tolist ();

Xmldocument XD = new xmldocument ();
Xmldeclaration xde;
Xde = XD. createxmldeclaration ("1.0", "GBK", null );
XD. appendchild (xde );

Xmlelement newslist = XD. createelement ("newslist ");
XD. appendchild (newslist );
Xmlnode root = XD. selectsinglenode ("newslist ");
Xmlelement [] news = new xmlelement [4];
Xmlelement [] Text = new xmlelement [4];
Xmlelement [] IMG = new xmlelement [4];
For (INT I = 0; I <4; I ++)
{
News [I] = XD. createelement ("news ");
Text [I] = XD. createelement ("text ");
IMG [I] = XD. createelement ("IMG ");
News [I]. setattribute ("ID", I. tostring ());
If (I <newstoview. Count)
{
Text [I]. innertext = newstoview [I]. title;
IMG [I]. innertext = "content/newsimages/slider_item1.png ";
}
Else
{
Text [I]. innertext = "";
IMG [I]. innertext = "";
}
News [I]. appendchild (Text [I]);
News [I]. appendchild (IMG [I]);
Root. appendchild (news [I]);
}
XD. Save (server. mappath ("../sources/news. xml "));

Front-end read code:

<SCRIPT type = "text/JavaScript">
Function loadxml (xmlfile ){
VaR xmldoc; Container Freight
If (! Window. activexobject ){
VaR parser = new domparser ();
Xmldoc = parser. parsefromstring (xmlfile, "text/XML ");
} Else {
Xmldoc = new activexobject ("Microsoft. xmldom ");
Xmldoc. async = "false ";
Xmldoc. Load (xmlfile );
}
Return xmldoc; iphone5
}
VaR xmldoc = loadxml ("sources/news. xml ");
// Parse the XML file and determine whether an error occurs.
If (xmldoc. parseerror. errorcode! = 0 ){
Alert (xmldoc. parseerror. Reason );
}
// Obtain the root node
VaR nodes = xmldoc.doc umentelement. childnodes;
</SCRIPT>

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.