Home > Backend Development > XML/RSS Tutorial > Sample code sharing for XmlDocument XML encoding conversion

Sample code sharing for XmlDocument XML encoding conversion

黄舟
Release: 2017-03-24 17:12:42
Original
2036 people have browsed it

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

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    }

Copy after login

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 of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template