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.

Background code:


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"); the front-end reads the Code:


<Script type = "text/javascript">
Function loadXML (xmlFile ){
Var xmlDoc;
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;
}
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>


<Div id = "newsbody">
<Div id = "slider">
<Div class = "sliderItem" id = "slide1">
<Script type = "text/javascript">
Document. write ("Document. write (" ")
</Script>
</Div>
<! --/SliderItem -->
<Div class = "sliderItem" id = "slide2" style = "display: none">
<Script type = "text/javascript">
Document. write ("

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.