


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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



How to underline on the computer When entering text on the computer, we often need to use underlines to highlight certain content or mark it. However, for some people who are not very familiar with computer input methods, typing underline can be a bit confusing. This article will introduce you to how to underline on your computer. In different computer operating systems and software, the way to enter the underscore may be slightly different. The following will introduce the common methods on Windows operating system and Mac operating system respectively. First, let’s take a look at the operation in Windows

Can XML files be opened with PPT? XML, Extensible Markup Language (Extensible Markup Language), is a universal markup language that is widely used in data exchange and data storage. Compared with HTML, XML is more flexible and can define its own tags and data structures, making the storage and exchange of data more convenient and unified. PPT, or PowerPoint, is a software developed by Microsoft for creating presentations. It provides a comprehensive way of

Using Python to merge and deduplicate XML data XML (eXtensibleMarkupLanguage) is a markup language used to store and transmit data. When processing XML data, sometimes we need to merge multiple XML files into one, or remove duplicate data. This article will introduce how to use Python to implement XML data merging and deduplication, and give corresponding code examples. 1. XML data merging When we have multiple XML files, we need to merge them

Implementing filtering and sorting of XML data using Python Introduction: XML is a commonly used data exchange format that stores data in the form of tags and attributes. When processing XML data, we often need to filter and sort the data. Python provides many useful tools and libraries to process XML data. This article will introduce how to use Python to filter and sort XML data. Reading the XML file Before we begin, we need to read the XML file. Python has many XML processing libraries,

Convert XML data in Python to CSV format XML (ExtensibleMarkupLanguage) is an extensible markup language commonly used for data storage and transmission. CSV (CommaSeparatedValues) is a comma-delimited text file format commonly used for data import and export. When processing data, sometimes it is necessary to convert XML data to CSV format for easy analysis and processing. Python is a powerful

Importing XML data into the database using PHP Introduction: During development, we often need to import external data into the database for further processing and analysis. As a commonly used data exchange format, XML is often used to store and transmit structured data. This article will introduce how to use PHP to import XML data into a database. Step 1: Parse the XML file First, we need to parse the XML file and extract the required data. PHP provides several ways to parse XML, the most commonly used of which is using Simple

Python implements conversion between XML and JSON Introduction: In the daily development process, we often need to convert data between different formats. XML and JSON are common data exchange formats. In Python, we can use various libraries to convert between XML and JSON. This article will introduce several commonly used methods, with code examples. 1. To convert XML to JSON in Python, we can use the xml.etree.ElementTree module

Handling Errors and Exceptions in XML Using Python XML is a commonly used data format used to store and represent structured data. When we use Python to process XML, sometimes we may encounter some errors and exceptions. In this article, I will introduce how to use Python to handle errors and exceptions in XML, and provide some sample code for reference. Use try-except statement to catch XML parsing errors When we use Python to parse XML, sometimes we may encounter some
