Sample code for XmlDocumentXML encoding and conversion

Source: Internet
Author: User
Recently I made an RSS online aggregator, most of which are RSS2.0 encoded XML.. NET compilers can read the data correctly, but some of them are encoded in GBK format. NET cannot be read. if the XML encoding is manually changed to "gb2312" or other encoding, it cannot be read. However, if the encoding does not change, IE can be correctly viewed. What should I do next? it's really hard for me. How about changing the encoding? The RSS2.0 file to be read by my RSS online aggregator is not downloaded to a local file, but read online. Well, after getting the connection, you can use the stream to get the XML stream correctly encoded. See the code below: Recently I made an RSS online aggregator. Most RSS 2.0 encoded XML codes.. NET compilers can read the data correctly, but some of them are encoded in GBK format. NET cannot be read. if you manually change the XML encoding to "gb2312" or other encoding, it cannot be read. However, if the encoding remains unchanged, IE can be correctly viewed. What should I do next? it's really hard for me. How does one change the encoding? The RSS 2.0 file to be read by my RSS online aggregator is not downloaded to a local file, but read online. Well, after getting the connection, you can use the stream to get the XML stream correctly encoded. 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} 67 private DataTable ReturnReadResult (string rssUrl) 8 {9 // constructed in DataTable table 10 DataTable dt = CreateDataTable (); 11 DataRow dr; 1213 try 14 {15 XmlDocument xml = new XmlDocument (); 1617 // properly load the fully qualified RSS 2.0 File 18 try19 {20 xml. loadXml (rssUrl); 21} 22 catch23 {24 // the following measures are targeted at some special RSS 2.0 files, such as the following site: 25 // site: http://www.csdn.net/rss/rssfeed.aspx ? Rssid = 1 & bigclassid = 1426 // it cannot be loaded normally. Further processing is required. For example, some. NET codes that are not currently supported can be read from the RSS 2.027 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 // added the original encoding into the 2312gb format. 31 StreamReader sr = new StreamReader (srp. getResponseStream (), System. text. encoding. getEncoding ("gb2312"); 3233 xml. loadXml (sr. readToEnd (). trim (); 34 sr. close (); 35 srp. close (); 36} 3738 // read the total title information to determine whether an image is displayed 39 try40 {41 titleLabel. text = xml. selectSingleNode ("/rss/channel/title "). innerText42 +"
"45 +"
"48 + xml. SelectSingleNode ("/rss/channel/description "). InnerText49 +"
"50 + xml. selectSingleNode ("/rss/channel/link "). innerText; 51} 52 catch53 {54 try55 {56 titleLabel. text = xml. selectSingleNode ("/rss/channel/title "). innerText57 +"
"58 + xml. SelectSingleNode ("/rss/channel/description "). InnerText59 +"
"60 + xml. selectSingleNode ("/rss/channel/link "). innerText; 61} 62 catch63 {64 // if there is no channel for instructions, 65 titleLabel. text = xml. selectSingleNode ("/rss/channel/title "). innerText66 +"
"67 + xml. selectSingleNode ("/rss/channel/link "). innerText; 68} 69} 7071 XmlNodeList nodes = xml. selectNodes ("// item"); 7273 foreach (XmlNode item in nodes) 74 {75 dr = dt. newRow (); 76 foreach (XmlNode child in item. childNodes) 77 {7879 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} 117118 // manually create a ableable119 private DataTable CreateDataTable () 120 {121 DataTable dt = new DataTable (); 122 DataColumn dc; 123124 System. type type; 125 type = System. type. getType ("System. string "); 126127 dc = new DataColumn (" title ", type); 128 dt. columns. add (dc); 129130 dc = new DataColumn ("link", type); 131 dt. columns. add (dc); 132133 dc = new DataColumn ("author", type); 134 dt. columns. add (dc); 135136 dc = new DataColumn ("guid", type); 137 dc. default value = ""; 138 dt. columns. add (dc); 139140 dc = new DataColumn ("category", type); 141 dc. allowDBNull = true; 142 dt. columns. add (dc); 143144 dc = new DataColumn ("pubDate", type); 145 dt. columns. add (dc); 146147 dc = new DataColumn ("description", type); 148 dc. allowDBNull = true; 149 dt. columns. add (dc); 150151 dc = new DataColumn ("comments", type); 152 dc. allowDBNull = true; 153 dt. columns. add (dc); 154155 return dt; 156}

After this processing, you can read a large part of RSS 2.0 connections.

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

Its core is to use stream conversion encoding.

The above is the details shared by the sample code for XmlDocument XML encoding conversion. For more information, see other related articles in the first PHP community!

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.