472,958 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

Encoding XML troubles

Hi,

I am trying to write a generic RSS/Atom/OPML feed client. The problem
is, that those xml feeds may have different encodings:

- <?xml version="1.0" encoding="ISO-8859-1" ?>...
- <?xml version="1.0" encoding="utf-8" ?>...
- ...

I am using the WebRequest functionality to get the feeds. So, my code
looks simplified like this:

WebRequest req = WebRequest.Create(url);
StreamReader reader = new StreamReader(..., Encoding.Default);
string result = readerEnc.ReadToEnd();

As you can see on the second line, I can (or must, because utf-8 is
default) already define the encoding type of the expected stream.
However, as I do not now the encoding type while fetching the xml
stream, I use Encoding.Default.

And now, I am in the middle of the problem: I like to read the result
xml string, get the encoding type and re-encode result string with the
correct encoding type. Otherwise, all special characters are not
readable or missing in the result string.

I have unlukely tried following work-arounds:
- convert directly the result xml string from Encoding.Default to XML
Encoding Type:
result = this.convertString(result, Encoding.Default,
Encoding.GetEncoding(myEncodingStringFromXMLFile)) ;

The convertString function uses similar code as the convert example on
msdn: http://msdn.microsoft.com/library/de...classtopic.asp
--> did not work - characters remained as they where before

- Creating a second StreamReader instance with the right encoding:
StreamReader reader2 = new StreamReader(...,
Encoding.GetEncoding(myEncodingStringFromXMLFile)) ;
string result = readerEnc.ReadToEnd();
--> did not work - it seems, that the ResponseStream from the
WebRequest class can only be read once! I am getting an error when
trying to modify the Position attribute on the stream (Another guy had
exactly the same problem:
http://groups.google.ch/groups?hl=de...f67a0c2&rnum=1)

Is there another solution, than fetching the URL twice? Do I miss some
basic functionalities? Thanks for your help...

Greets,

Phil
Nov 17 '05 #1
4 8761
fitsch wrote:
Hi,

I am trying to write a generic RSS/Atom/OPML feed client. The problem
is, that those xml feeds may have different encodings:

- <?xml version="1.0" encoding="ISO-8859-1" ?>...
- <?xml version="1.0" encoding="utf-8" ?>...
- ...

I am using the WebRequest functionality to get the feeds. So, my code
looks simplified like this:

WebRequest req = WebRequest.Create(url);
StreamReader reader = new StreamReader(..., Encoding.Default);
string result = readerEnc.ReadToEnd();

As you can see on the second line, I can (or must, because utf-8 is
default) already define the encoding type of the expected stream.
However, as I do not now the encoding type while fetching the xml
stream, I use Encoding.Default.
Note that Encoding.Default is your OS default character set, and no
magic catch all encoding. This step will already render a lot of XML
input useless.
And now, I am in the middle of the problem: I like to read the result
xml string, get the encoding type and re-encode result string with the
correct encoding type. Otherwise, all special characters are not
readable or missing in the result string.
Once it's a string, it's a string. You must re-*de*code bytes.
I have unlukely tried following work-arounds:
- convert directly the result xml string from Encoding.Default to XML
Encoding Type:
result = this.convertString(result, Encoding.Default,
Encoding.GetEncoding(myEncodingStringFromXMLFile)) ;

The convertString function uses similar code as the convert example on
msdn:
http://msdn.microsoft.com/library/de...ry/en-us/cpref
/html/frlrfsystemtextencodingclasstopic.asp --> did not work -
characters remained as they where before

- Creating a second StreamReader instance with the right encoding:
StreamReader reader2 = new StreamReader(...,
Encoding.GetEncoding(myEncodingStringFromXMLFile)) ;
string result = readerEnc.ReadToEnd();
--> did not work - it seems, that the ResponseStream from the
WebRequest class can only be read once! I am getting an error when
trying to modify the Position attribute on the stream (Another guy had
exactly the same problem:
http://groups.google.ch/groups?hl=de...f67a0c2&rnum=1)

Is there another solution, than fetching the URL twice? Do I miss some
basic functionalities? Thanks for your help...


The functionality to safely decode XML content is already available in
the BCL. Just use an XmlTextReader.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #2
fitsch wrote:
Hi,

I am trying to write a generic RSS/Atom/OPML feed client. The problem
is, that those xml feeds may have different encodings:

- <?xml version="1.0" encoding="ISO-8859-1" ?>...
- <?xml version="1.0" encoding="utf-8" ?>...
- ...

I am using the WebRequest functionality to get the feeds. So, my code
looks simplified like this:

WebRequest req = WebRequest.Create(url);
StreamReader reader = new StreamReader(..., Encoding.Default);
string result = readerEnc.ReadToEnd();

As you can see on the second line, I can (or must, because utf-8 is
default) already define the encoding type of the expected stream.
However, as I do not now the encoding type while fetching the xml
stream, I use Encoding.Default.
Note that Encoding.Default is your OS default character set, and no
magic catch all encoding. This step will already render a lot of XML
input useless.
And now, I am in the middle of the problem: I like to read the result
xml string, get the encoding type and re-encode result string with the
correct encoding type. Otherwise, all special characters are not
readable or missing in the result string.
Once it's a string, it's a string. You must re-*de*code bytes.
I have unlukely tried following work-arounds:
- convert directly the result xml string from Encoding.Default to XML
Encoding Type:
result = this.convertString(result, Encoding.Default,
Encoding.GetEncoding(myEncodingStringFromXMLFile)) ;

The convertString function uses similar code as the convert example on
msdn:
http://msdn.microsoft.com/library/de...ry/en-us/cpref
/html/frlrfsystemtextencodingclasstopic.asp --> did not work -
characters remained as they where before

- Creating a second StreamReader instance with the right encoding:
StreamReader reader2 = new StreamReader(...,
Encoding.GetEncoding(myEncodingStringFromXMLFile)) ;
string result = readerEnc.ReadToEnd();
--> did not work - it seems, that the ResponseStream from the
WebRequest class can only be read once! I am getting an error when
trying to modify the Position attribute on the stream (Another guy had
exactly the same problem:
http://groups.google.ch/groups?hl=de...f67a0c2&rnum=1)

Is there another solution, than fetching the URL twice? Do I miss some
basic functionalities? Thanks for your help...


The functionality to safely decode XML content is already available in
the BCL. Just use an XmlTextReader.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 17 '05 #3
fitsch <fi****@bluewin.ch> wrote:
I am trying to write a generic RSS/Atom/OPML feed client. The problem
is, that those xml feeds may have different encodings:

- <?xml version="1.0" encoding="ISO-8859-1" ?>...
- <?xml version="1.0" encoding="utf-8" ?>...
- ...

I am using the WebRequest functionality to get the feeds. So, my code
looks simplified like this:

WebRequest req = WebRequest.Create(url);
StreamReader reader = new StreamReader(..., Encoding.Default);
string result = readerEnc.ReadToEnd();


Why bother reading it as a string? The best solution is to get the
stream and pass it directly to XmlTextReader - then the XmlTextReader,
which knows how to deal with the encoding part of the XML declaration,
can do the right thing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
fitsch <fi****@bluewin.ch> wrote:
I am trying to write a generic RSS/Atom/OPML feed client. The problem
is, that those xml feeds may have different encodings:

- <?xml version="1.0" encoding="ISO-8859-1" ?>...
- <?xml version="1.0" encoding="utf-8" ?>...
- ...

I am using the WebRequest functionality to get the feeds. So, my code
looks simplified like this:

WebRequest req = WebRequest.Create(url);
StreamReader reader = new StreamReader(..., Encoding.Default);
string result = readerEnc.ReadToEnd();


Why bother reading it as a string? The best solution is to get the
stream and pass it directly to XmlTextReader - then the XmlTextReader,
which knows how to deal with the encoding part of the XML declaration,
can do the right thing.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Xaver Hinterhuber | last post by:
Hello pythonistas, I program a class which stores the source code for an output page in a string. At request time it compiles it, executes it and returns the result. I now have upgraded the...
7
by: Erik Bethke | last post by:
Hello All, I have found much help in the google archives but I am still stuck... here is my code snippet: path = os.getcwd() path = path.decode('UTF8') Now the trouble is I am getting that...
9
by: Ksenia Marasanova | last post by:
Hi, I have a little problem with encoding. Was hoping maybe anyone can help me to solve it. There is some amount of data in a database (PG) that must be inserted into Excel sheet and emailed....
0
by: lmckaha | last post by:
Hi, Mysql version: 3.23.49 Solaris version: 2.7 gcc compiler version: 2.95.2 Python : 2.2.2 I'm evaluating the C and C++ API to decide which one to bye but I have many troubles.
6
by: jmgonet | last post by:
Hello everybody, I'm having troubles loading a Xml string encoded in UTF-8. If I try this code: ------------------------------ XmlDocument doc=new XmlDocument(); String s="<?xml...
0
by: fitsch | last post by:
Hi, I am trying to write a generic RSS/Atom/OPML feed client. The problem is, that those xml feeds may have different encodings: - <?xml version="1.0" encoding="ISO-8859-1" ?>... - <?xml...
0
by: Michal | last post by:
I have troubles with instaling .Net Framework 2.0 (Beta 2 - 2.0.50215). The main instalation went just fine, troubles begin with my attemt to run aspnet_regiss.exe -i. Asp.Net is instaled into IIS...
1
by: Brad Wood | last post by:
I am testing a web service that returns an XML doc as a string. I went through troubles when developing it whereby the saved output of the service would not display in Internet Explorer due to...
12
by: Atlas | last post by:
I'm working on a multilanguage ASP/HTML site using a IIS6 web server. It perfectly works with two languages (english and italian) in this way: - basically the same ASP code for every language -...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.