From: Göran Andersson on
Keith G Hicks wrote:
> Yep. I found that out late last night. Thanks. It took quite a bit of
> hunting around to figure this out. It's not intuitive. "GetEncoding" sounds
> like a read only property. The word "get" is misleading. I finally landed
> upon something that I read that explained that it was more like "Set" than
> "Get". Now I'm sure they meant that GetEncoding("ISO-8859-1") means to "get"
> the encoding of "ISO-8859-1" but that's a bit ambiguous. With
> Encoding.GetEncoding as the 3rd param of StreamReader, it also could be
> interrpeted as "get the current encoding of the stream".

I believe that it's just your preconception about what the GetEncoding
method does that makes it seem misleading.

The GetEncoding method interprets the parameter that you send to it and
returns an Encoding object that handles that specific encoding.

If you want to set the default encoding for the system, that is not done
using the Encoding class itself. You create the appropriate encoding
object and apply it to a context, depending on how wide a scope you want
it to afffect.

--
G�ran Andersson
_____
http://www.guffa.com
From: Göran Andersson on
Keith G Hicks wrote:
> Never mind. I figured it out:
>
>
> Dim TheFileLines As New List(Of String)
>
> TheFileLines.AddRange(System.IO.File.ReadAllLines(xmlFilesLocation & "\" &
> sArticleToPost))
>
> TheFileLines.RemoveAt(0)
>
> TheFileLines.Insert(0, "<?xml version=""1.0"" encoding=""ISO-8859-1""?>")
>
> System.IO.File.WriteAllLines(xmlFilesLocation & "\" & sArticleToPost,
> TheFileLines.ToArray)
>

You are shuffling a lot of data around that you don't need to. You don't
have to turn the array to a list and back again. Just replace the first
item instead of removing it and inserting a new one.

Specify the encoding when you read the lines, that way you will read the
rest of the file correctly:

Dim fileName As String = Path.Combine(xmlFilesLocation, sArticleToPost)
Dim iso = Encoding.GetEncoding("ISO-8859-1")
Dim lines As String() = System.IO.File.ReadAllLines(fileName, iso)

lines(0) = "<?xml version=""1.0"" encoding=""ISO-8859-1""?>"
System.IO.File.WriteAllLines(fileName, lines, iso)


You could also save the file as UTF-8 if you like. Once you have the
text as strings, it can be encoded using any encoding that has a
character set that supports the characters that you have. As strings in
..NET are Uncide, you can always encode a string using any Unicode encoding.

lines(0) = "<?xml version=""1.0"" encoding=""UTF-8""?>"
System.IO.File.WriteAllLines(fileName, lines)

--
G�ran Andersson
_____
http://www.guffa.com
From: Keith G Hicks on
Yeah, I found that out too. I got that from a post somewhere and it seemed
a bit clunky. I ended up with this instead (also a bit clunky)

Dim sr As New StreamReader(xmlFilesLocation & "\" & sArticleToPost,
Encoding.GetEncoding("ISO-8859-1"))

Dim text As String = sr.ReadToEnd

sr.Close()

sr = Nothing

Dim text2() As String

ReDim text2(1)

text2(0) = text.Replace("<?xml version=1.0 encoding=UTF-8?>", "<?xml
version=""1.0"" encoding=""ISO-8859-1""?>").Replace("&", "&amp;")

System.IO.File.WriteAllLines(xmlFilesLocation & "\" & sArticleToPost,
text2, Encoding.GetEncoding("ISO-8859-1"))



But what you suggested below is cleaner and works just fine. Thanks for the
input. Really appreciated.

"G�ran Andersson" <guffa(a)guffa.com> wrote in message
news:OzpGp0vXKHA.3720(a)TK2MSFTNGP02.phx.gbl...
> Keith G Hicks wrote:
>> Never mind. I figured it out:
>>
>>
>> Dim TheFileLines As New List(Of String)
>>
>> TheFileLines.AddRange(System.IO.File.ReadAllLines(xmlFilesLocation & "\"
>> &
>> sArticleToPost))
>>
>> TheFileLines.RemoveAt(0)
>>
>> TheFileLines.Insert(0, "<?xml version=""1.0"" encoding=""ISO-8859-1""?>")
>>
>> System.IO.File.WriteAllLines(xmlFilesLocation & "\" & sArticleToPost,
>> TheFileLines.ToArray)
>>
>
> You are shuffling a lot of data around that you don't need to. You don't
> have to turn the array to a list and back again. Just replace the first
> item instead of removing it and inserting a new one.
>
> Specify the encoding when you read the lines, that way you will read the
> rest of the file correctly:
>
> Dim fileName As String = Path.Combine(xmlFilesLocation, sArticleToPost)
> Dim iso = Encoding.GetEncoding("ISO-8859-1")
> Dim lines As String() = System.IO.File.ReadAllLines(fileName, iso)
>
> lines(0) = "<?xml version=""1.0"" encoding=""ISO-8859-1""?>"
> System.IO.File.WriteAllLines(fileName, lines, iso)
>
>
> You could also save the file as UTF-8 if you like. Once you have the text
> as strings, it can be encoded using any encoding that has a character set
> that supports the characters that you have. As strings in .NET are Uncide,
> you can always encode a string using any Unicode encoding.
>
> lines(0) = "<?xml version=""1.0"" encoding=""UTF-8""?>"
> System.IO.File.WriteAllLines(fileName, lines)
>
> --
> G�ran Andersson
> _____
> http://www.guffa.com



From: Scott M. on

"Keith G Hicks" <krh(a)comcast.net> wrote in message
news:OTC0zFwXKHA.3720(a)TK2MSFTNGP02.phx.gbl...
> Yeah, I found that out too. I got that from a post somewhere and it
> seemed
> a bit clunky. I ended up with this instead (also a bit clunky)
>
> Dim sr As New StreamReader(xmlFilesLocation & "\" & sArticleToPost,
> Encoding.GetEncoding("ISO-8859-1"))
>
> Dim text As String = sr.ReadToEnd
>
> sr.Close()
>
> sr = Nothing
>
> Dim text2() As String
>
> ReDim text2(1)
>
> text2(0) = text.Replace("<?xml version=1.0 encoding=UTF-8?>", "<?xml
> version=""1.0"" encoding=""ISO-8859-1""?>").Replace("&", "&amp;")
>
> System.IO.File.WriteAllLines(xmlFilesLocation & "\" & sArticleToPost,
> text2, Encoding.GetEncoding("ISO-8859-1"))
>
>
>
> But what you suggested below is cleaner and works just fine. Thanks for
> the
> input. Really appreciated.
>
> "G�ran Andersson" <guffa(a)guffa.com> wrote in message
> news:OzpGp0vXKHA.3720(a)TK2MSFTNGP02.phx.gbl...
>> Keith G Hicks wrote:
>>> Never mind. I figured it out:
>>>
>>>
>>> Dim TheFileLines As New List(Of String)
>>>
>>> TheFileLines.AddRange(System.IO.File.ReadAllLines(xmlFilesLocation & "\"
>>> &
>>> sArticleToPost))
>>>
>>> TheFileLines.RemoveAt(0)
>>>
>>> TheFileLines.Insert(0, "<?xml version=""1.0""
>>> encoding=""ISO-8859-1""?>")
>>>
>>> System.IO.File.WriteAllLines(xmlFilesLocation & "\" & sArticleToPost,
>>> TheFileLines.ToArray)
>>>
>>
>> You are shuffling a lot of data around that you don't need to. You don't
>> have to turn the array to a list and back again. Just replace the first
>> item instead of removing it and inserting a new one.
>>
>> Specify the encoding when you read the lines, that way you will read the
>> rest of the file correctly:
>>
>> Dim fileName As String = Path.Combine(xmlFilesLocation, sArticleToPost)
>> Dim iso = Encoding.GetEncoding("ISO-8859-1")
>> Dim lines As String() = System.IO.File.ReadAllLines(fileName, iso)
>>
>> lines(0) = "<?xml version=""1.0"" encoding=""ISO-8859-1""?>"
>> System.IO.File.WriteAllLines(fileName, lines, iso)
>>
>>
>> You could also save the file as UTF-8 if you like. Once you have the text
>> as strings, it can be encoded using any encoding that has a character set
>> that supports the characters that you have. As strings in .NET are
>> Uncide,
>> you can always encode a string using any Unicode encoding.
>>
>> lines(0) = "<?xml version=""1.0"" encoding=""UTF-8""?>"
>> System.IO.File.WriteAllLines(fileName, lines)
>>
>> --
>> G�ran Andersson
>> _____
>> http://www.guffa.com
>


Replace your sr = Nothing line to sr.Dispose instead.

Setting a variable to Nothing in VB .NET really doesn't accomplish anything,
except for in the rarest cases. But, just closing a StreamReader, does not
fully clean it up, so Dispose is needed.

-Scott


From: Branco Medeiros on
Göran Andersson wrote:
<snip<
> Specify the encoding when you read the lines, that way you will read the
> rest of the file correctly:
>
> Dim fileName As String = Path.Combine(xmlFilesLocation, sArticleToPost)
> Dim iso = Encoding.GetEncoding("ISO-8859-1")
> Dim lines As String() = System.IO.File.ReadAllLines(fileName, iso)
>
> lines(0) = "<?xml version=""1.0"" encoding=""ISO-8859-1""?>"
> System.IO.File.WriteAllLines(fileName, lines, iso)
<snip>

I may be wrong, but it seems to me that your code tries to read the
file as if it was in ISO-8859-1 ecoding, while the OP's first post
states that the files are in UTF-8 and he wants then in ISO-8859-1. He
is probably reading garbage atm.

If he wants to pursue this approach, then the *reading* encoding
should be set to UTF8:

<aircode>
Dim FileName As String = _
Path.Combine(xmlFilesLocation, sArticleToPost)
Dim Lines As String() = _
System.IO.File.ReadAllLines(FileName, Encoding.UTF8)
Lines(0) = "<?xml version=""1.0"" encoding=""ISO-8859-1""?>"
System.IO.File.WriteAllLines( _
FileName, Lines, Encoding.GetEncoding("ISO-8859-1"))
</aircode>

HTH.

Regards,

Branco.