Display XML content in ASP. NET (take the common announcement board as an example)

Source: Internet
Author: User
Tags xslt

I have written a piece of similarArticleHe was instructed by R2. he found that there was a problem in it, so he reorganized it and debugged it. There were a lot of similar online content, but he just wanted to sort it out, you don't need to try again later.

1. announcement board (News list)

Newslist. xml

<? XML version = "1.0" encoding = "gb2312"?>

<Topiclist type = "aspcool News">

<Topic>

<Title> first news </title>

<Href> main. aspx? Name = news1
</Topic>

<Topic>

<Title> second news </title>

<Href> main. aspx? Name = new2
</Topic>

</Topiclist>

Shownewslist. aspx

<% @ import namespace =" system "%>
<% @ page Language =" C # "DEBUG =" true "codePage =" 936 "%>
<% @ import namespace =" system. io "%>
<% @ Assembly name =" system. XML "%>
<% @ import namespace =" system. XML "%>
<% @ import namespace =" system. XML. XSL "%>








<% = XSLT () %>

2. The display of each piece of news is the same.

3. How to Create a news item

News1.xml

<? XML version = "1.0" encoding = "gb2312"?>

<Document>

<Title> aspcool news! </Title>

<ABSTRACT> test news </abstract>

<Author> feiying </author>

<Content>

<Paragraph> the firet test </Paragraph>

</Content>

</Document>

News1.xml Method

Private void btngetxml_click (Object sender, system. eventargs E)

{

Stirng filename=this.txt filename. test;

String titlepolicthis.txt title. text;

String abstract=this.txt abstract. text;

String Authorizer this.txt author. text;

String content+this.txt content. text;

// Determine whether a file exists

If (file. exists (server. mappath (filename + ". xml ")))

{

Response. Write ("the file name already exists. Please reselect the file name. ");

Response. End ();

}

Else

{

// Create news1.xml

System. xml. xmltextwriter mywriter = new xmltextwriter (filename + ". xml", null );

Mywriter. writeprocessinginstruction ("XML", "version = '1. 0 '");

Mywriter. writestartelement ("content ");

Mywriter. writeattributestring ("type", "whitepaper ");

//
Mywriter. writestartelement ("document ");

Mywriter. writeelementstring ("title", title );

Mywriter. writeelementstring ("abstract", abstract );

Mywriter. writeelementstring ("author", author );

Mywriter. writeelementstring ("content", content );

//
Mywriter. writeendelement ();

Mywriter. Flush ();

Mywriter. Close ();

}

}

4. How to modify the existing newslist. xml

Each time a piece of news is added, add the previous one to newslist. xml.

Private void updatenewslist (String title, string href)

{

// Add new nodes to newslist. xml

Xmldocument mydocument = new xmldocument ();

Mydocument. Load ("newslist. xml ");

Xmlelement topiclist = (xmlelement) mydocument. getelementsbytagname ("topiclist ");

Xmlelement topic = mydocument. createelement ("topic ");

Xmlelement Title = topic. createelement ("title ");

Title. innertext = title;

Topic. appendchild (title );

Xmlelement href = topic. createelement ("href ");

Href. innertext = href;

Topic. appendchild (href );

Topiclist. prependchild (topic );

Mydocument. Save ("newslist. xml ");

}
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.