Results 1 to 5 of 5

Thread: xml encoding

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    176

    xml encoding

    I have written a program that creates an xml using the following code
    Code:
    Dim writer As New XmlTextWriter(SaveFile, System.Text.Encoding.UTF8)
            writer.WriteStartDocument(True)
            writer.Formatting = Formatting.Indented
            writer.Indentation = 5
            writer.WriteStartElement("Table")
    I need iso-8859-1 encoding however it is apparently not an option, with this method.

    How can I create the encoding that I need?

    I am using VB 2019

    Thank You

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,712

    Re: xml encoding

    I haven't tested it, but you should be able to specify any encoding in the second argument of the constructor:
    Code:
    Using writer = New XmlTextWriter(SaveFile, Encoding.GetEncoding(28591))
        With writer
            .WriteStartDocument(True)
            .Formatting = Formatting.Indented
            .Indentation = 5
            .WriteStartElement("Table")
        End With
    End Using
    Edit - As I thought, it is possible. Take a look at this fiddle: https://dotnetfiddle.net/e0DXIA
    Last edited by dday9; Dec 8th, 2020 at 02:25 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: xml encoding

    As a test I used a literal. Make certain that the encoding is what you want!

    Code:
            Dim libraryRequest As XDocument
            libraryRequest = <?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
                             <!-- Tests that the application works. -->
                             <books>
                                 <book/>
                                 <foo>
                                     <!-- the characters in bar will not be stored with ISO-8859-1 -->
                                     <bar>■▲</bar>
                                 </foo>
                             </books>
    
            libraryRequest.Save("path here")
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Aug 2006
    Posts
    176

    Re: xml encoding

    I modified the first line as shown below and it works now.

    Code:
    Dim writer As New XmlTextWriter(SaveFile, System.Text.Encoding.GetEncoding(28591))
    When I tried using dday9's code I received and error that the writer had not been declared, but it put me on the right track.

    Thank You

  5. #5
    Lively Member
    Join Date
    Jan 2020
    Posts
    120

    Re: xml encoding

    Dim Stream As New IO.StreamReader("File.xml", System.Text.Encoding.UTF8)
    Dim Reader As New Xml.XmlTextReader(Stream)

    Reference : https://stackoverflow.com/a/17528811/11954917

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width