XmlDocument XML Encoding Conversion

Source: Internet
Author: User
Tags foreach comments

Recently made an RSS online aggregator, most of the RSS 2.0 encoded XML encoding. NET compiler can read correctly, but some such as GBK code, our. NET can not read, if the encoding of that XML manually changed to "gb2312" or other encoding, is not read. However, the code changes do not change, ie can be viewed correctly. What to do next, it really baffled me. How about changing the code? The RSS 2.0 file that my RSS online aggregator reads is not downloaded to a local file, but is read online. Well, after getting the connection, using the stream can be a good one 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//Structure in DataTable table
a DataTable dt = createdatatable ();
One DataRow Dr;
12
A try
14 {
XmlDocument xml = new XmlDocument ();
16
17//Normal loading of fully qualified RSS 2.0 files
A try
19 {
XML. Loadxml (Rssurl);
21}
Catch
23 {
24//The following measures for a number of special RSS 2.0 files, such as the following site:
//site:http://www.csdn.net/rss/rssfeed.aspx? Rssid=1&bigclassid=14
26//is not normally loaded in accordance with the normal. Further processing is required. Like some. NET temporarily unsupported encoding, currently can read the known RSS 2.0
Rssurl = "Http://soft.yesky.com/index.xml";
System.Net.WebRequest WR = System.Net.WebRequest.Create (Rssurl);
System.Net.WebResponse SRP = wr. GetResponse ();
30//Added the original code into the 2312GB form.
StreamReader sr = new StreamReader (SRP. GetResponseStream (), System.Text.Encoding.GetEncoding ("gb2312"));
32
XML. Loadxml (Sr. ReadToEnd (). Trim ());
Sr. Close ();
SRP. Close ();
36}
37
38//Read the general title information, you can determine whether there are pictures show
The Try
40 {
Titlelabel.text = XML. selectSingleNode ("/rss/channel/title"). InnerText
+ "<br><a href ="
+ XML. selectSingleNode ("//image/link"). InnerText
+ ">"
+ "+ XML. selectSingleNode ("//image/url"). InnerText
+ "border = no></a><br>"
+ XML. selectSingleNode ("/rss/channel/description"). InnerText
+ "<br>"
+ XML. selectSingleNode ("/rss/channel/link"). InnerText;
51}
Catch
53 {
The Try
55 {
Titlelabel.text = XML. selectSingleNode ("/rss/channel/title"). InnerText
+ "<br>"
+ XML. selectSingleNode ("/rss/channel/description"). InnerText
"<br>"
+ XML. selectSingleNode ("/rss/channel/link"). InnerText;
61}
The catch
63 {
64//If there is no channel to explain the situation
Titlelabel.text = XML. selectSingleNode ("/rss/channel/title"). InnerText
"<br>"
+ XML. selectSingleNode ("/rss/channel/link"). InnerText;
68}
69}
70
XmlNodeList nodes = XML. SelectNodes ("//item");
72
Nodes foreach (XmlNode item in)
74 {
Dr = dt. NewRow ();
XmlNode foreach (Child in item.) ChildNodes)
77 {
78
The switch (child. Name)
80 {
Bayi Case "title":
The dr["title"] = child. InnerText;
a break;
Case "link":
dr["link" = child. InnerText;
break;
The case "Author":
The dr["Author"] = child. InnerText;
The break;
Case "GUID":
dr["GUID" = child. InnerText;
The break;
Case "category":
dr["category"] = child. InnerText;
The break;
Case "pubdate":
The dr["pubdate"] = child. InnerText;
The break;
Case "description":
dr["description"] = child. InnerText;
a break;
102 Case "Comments":
The dr["comments"] = child. InnerText;
The break;
105}
106}
DT. Rows.Add (DR);
108}
109 return DT;
110}
The catch (Exception ex)
112 {
113 Response.Write (ex. ToString ());
114 return NULL;
115}
116}
117
118//Manually Create a DataTable
119 Private DataTable createdatatable ()
120 {
121 DataTable dt = new DataTable ();
122 DataColumn DC;
123
124 System.Type Type;
The 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
The DataColumn dc = new ("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);
A DC. AllowDBNull = true;
The 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
The return DT;
156}

After this process, you can read most of the RSS 2.0 connections.

As for handling local files using StreamReader stream conversion encoding, the same processing.

The core is the use of stream conversion coding.

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.