Home >Backend Development >XML/RSS Tutorial >Sample 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!