search
HomeBackend DevelopmentXML/RSS TutorialSample code sharing for XmlDocument XML encoding conversion

Recently I made an RSS online aggregator. Most RSS 2.0 encoded XML encoded .NET compilers can read it correctly, but some, such as GBK encoding, our. NET cannot read it. If you manually change the XML encoding to "gb2312" or other encoding, it cannot be read. However, whether the encoding changes or not, IE can view it correctly. What to do next is really hard for me. How about changing the encoding? The RSS 2.0 files that my RSS online aggregator wants to read are not downloaded to local files, but read online. Well, after getting the connection, you can use the stream to get the correctly encoded XML stream. See the code below:

1 private void Page_Load(object sender, System.EventArgs e)
2 {
3 rssRepeater.DataSource = ReturnReadResult( Request[ "url" ] );
4 rssRepeater.DataBind( );
5 }
6
7 private DataTable ReturnReadResult( string rssUrl )
8    {
9      //构在DataTable表格
10      DataTable dt = CreateDataTable();
11       DataRow dr;
12
13      try 
14      {
15        XmlDocument xml = new XmlDocument();
16
17        //正常加载完全合格的RSS 2.0文件
18         try
19        {
20           xml.LoadXml( rssUrl );
21        }
22         catch
23        {
24          //下面的措施 针对一些特别的RSS 2.0文件,比如下面的一个站点:
25           //site :http://www.csdn.net/rss/rssfeed.aspx? rssid=1&bigclassid=14
26          //按照常规是无法正 常加载的。需要进一步处理。比如一些.NET暂时不支持的编码,目前可以读取所 知的RSS 2.0
27           rssUrl = "http://soft.yesky.com/index.xml";
28           System.Net.WebRequest wr = System.Net.WebRequest.Create( rssUrl );
29          System.Net.WebResponse srp = wr.GetResponse ();
30          //加入了把原先编码都转化成了2312gb形式。 
31          StreamReader sr = new StreamReader( srp.GetResponseStream() ,System.Text.Encoding.GetEncoding( "gb2312" ));
32
33          xml.LoadXml( sr.ReadToEnd( ).Trim( ) );
34          sr.Close();
35          srp.Close();
36        }
37
38        //读取总标题信息,可以判断是否有图片展示
39        try
40        {
41           titleLabel.Text = xml.SelectSingleNode ("/rss/channel/title").InnerText
42             + "<br><a href = "
43             + xml.SelectSingleNode("//image/link").InnerText
44             + ">"
45             + "<img src="
46            + xml.SelectSingleNode("//image/url").InnerText
47             + " border = no></a><br>"
48            + xml.SelectSingleNode ("/rss/channel/description").InnerText
49             + "<br>"
50            +  xml.SelectSingleNode("/rss/channel/link").InnerText;
51         }
52        catch
53         {
54          try
55          {
56             titleLabel.Text = xml.SelectSingleNode ("/rss/channel/title").InnerText
57               + "<br>"
58              + xml.SelectSingleNode("/rss/channel/description").InnerText
59              + "<br>"
60               + xml.SelectSingleNode ("/rss/channel/link").InnerText;
61           }
62          catch
63          {
64            //假如没有频道进行说明的情况下
65             titleLabel.Text = xml.SelectSingleNode ("/rss/channel/title").InnerText
66               + "<br>"
67              + xml.SelectSingleNode("/rss/channel/link").InnerText;
68           }
69        }
70
71         XmlNodeList nodes = xml.SelectNodes("//item");
72
73        foreach( XmlNode item in nodes )
74         {
75          dr = dt.NewRow();
76           foreach( XmlNode child in item.ChildNodes )
77           {
78
79            switch( child.Name )
80            {
81               case "title":
82                 dr[ "title" ] = child.InnerText;
83                 break;
84              case "link":
85                dr[ "link" ] = child.InnerText;
86                 break;
87              case "author":
88                dr[ "author" ] = child.InnerText;
89                 break;
90              case "guid":
91                dr[ "guid" ] = child.InnerText;
92                 break;
93              case "category":
94                dr[ "category" ] = child.InnerText;
95                 break;
96              case "pubDate":
97                dr[ "pubDate" ] = child.InnerText;
98                 break;
99              case "description":
100                dr[ "description" ] = child.InnerText;
101                 break;
102              case "comments":
103                dr[ "comments" ] = child.InnerText;
104                 break;
105            }
106           }
107          dt.Rows.Add( dr );
108         }
109        return dt;
110      } 
111      catch ( Exception ex )
112      {
113        Response.Write( ex.ToString( ) );
114         return null;
115      }
116    }
117
118//手动创立一个DataTable
119    private DataTable CreateDataTable()
120    {
121      DataTable dt = new DataTable();
122      DataColumn dc;
123
124       System.Type type;
125      type = System.Type.GetType("System.String");
126
127       dc = new DataColumn( "title",type );
128       dt.Columns.Add( dc );
129
130      dc = new DataColumn( "link", type );
131       dt.Columns.Add( dc );
132
133      dc = new DataColumn( "author", type );
134      dt.Columns.Add( dc );
135
136      dc = new DataColumn( "guid", type );
137      dc.DefaultValue = "";
138       dt.Columns.Add( dc );
139
140      dc = new DataColumn( "category", type );
141       dc.AllowDBNull = true;
142      dt.Columns.Add( dc );
143
144      dc = new DataColumn( "pubDate", type );
145      dt.Columns.Add( dc );
146
147       dc = new DataColumn( "description", type );
148       dc.AllowDBNull = true;
149      dt.Columns.Add( dc );
150
151      dc = new DataColumn( "comments", type );
152      dc.AllowDBNull = true;
153      dt.Columns.Add( dc );
154
155       return dt;
156    }

After processing in this way, most RSS 2.0 connections can be read.

As for processing local files, use StreamReader stream conversion encoding, the same process.

The core is to use stream conversion encoding.

The above is the detailed content of Sample code sharing for XmlDocument XML encoding conversion. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
RSS & XML: Understanding the Dynamic Duo of Web ContentRSS & XML: Understanding the Dynamic Duo of Web ContentApr 19, 2025 am 12:03 AM

RSS and XML are tools for web content management. RSS is used to publish and subscribe to content, and XML is used to store and transfer data. They work with content publishing, subscriptions, and update push. Examples of usage include RSS publishing blog posts and XML storing book information.

RSS Documents: The Foundation of Web SyndicationRSS Documents: The Foundation of Web SyndicationApr 18, 2025 am 12:04 AM

RSS documents are XML-based structured files used to publish and subscribe to frequently updated content. Its main functions include: 1) automated content updates, 2) content aggregation, and 3) improving browsing efficiency. Through RSSfeed, users can subscribe and get the latest information from different sources in a timely manner.

Decoding RSS: The XML Structure of Content FeedsDecoding RSS: The XML Structure of Content FeedsApr 17, 2025 am 12:09 AM

The XML structure of RSS includes: 1. XML declaration and RSS version, 2. Channel (Channel), 3. Item. These parts form the basis of RSS files, allowing users to obtain and process content information by parsing XML data.

How to Parse and Utilize XML-Based RSS FeedsHow to Parse and Utilize XML-Based RSS FeedsApr 16, 2025 am 12:05 AM

RSSfeedsuseXMLtosyndicatecontent;parsingtheminvolvesloadingXML,navigatingitsstructure,andextractingdata.Applicationsincludebuildingnewsaggregatorsandtrackingpodcastepisodes.

RSS Documents: How They Deliver Your Favorite ContentRSS Documents: How They Deliver Your Favorite ContentApr 15, 2025 am 12:01 AM

RSS documents work by publishing content updates through XML files, and users subscribe and receive notifications through RSS readers. 1. Content publisher creates and updates RSS documents. 2. The RSS reader regularly accesses and parses XML files. 3. Users browse and read updated content. Example of usage: Subscribe to TechCrunch's RSS feed, just copy the link to the RSS reader.

Building Feeds with XML: A Hands-On Guide to RSSBuilding Feeds with XML: A Hands-On Guide to RSSApr 14, 2025 am 12:17 AM

The steps to build an RSSfeed using XML are as follows: 1. Create the root element and set the version; 2. Add the channel element and its basic information; 3. Add the entry element, including the title, link and description; 4. Convert the XML structure to a string and output it. With these steps, you can create a valid RSSfeed from scratch and enhance its functionality by adding additional elements such as release date and author information.

Creating RSS Documents: A Step-by-Step TutorialCreating RSS Documents: A Step-by-Step TutorialApr 13, 2025 am 12:10 AM

The steps to create an RSS document are as follows: 1. Write in XML format, with the root element, including the elements. 2. Add, etc. elements to describe channel information. 3. Add elements, each representing a content entry, including,,,,,,,,,,,. 4. Optionally add and elements to enrich the content. 5. Ensure the XML format is correct, use online tools to verify, optimize performance and keep content updated.

XML's Role in RSS: The Foundation of Syndicated ContentXML's Role in RSS: The Foundation of Syndicated ContentApr 12, 2025 am 12:17 AM

The core role of XML in RSS is to provide a standardized and flexible data format. 1. The structure and markup language characteristics of XML make it suitable for data exchange and storage. 2. RSS uses XML to create a standardized format to facilitate content sharing. 3. The application of XML in RSS includes elements that define feed content, such as title and release date. 4. Advantages include standardization and scalability, and challenges include document verbose and strict syntax requirements. 5. Best practices include validating XML validity, keeping it simple, using CDATA, and regularly updating.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.