0

The original xml file is encoded with UTF-8 without BOM

<?xml version="1.0" encoding="UTF-8"?>
<some_text>
    <ada/>
    <file/>
    <title><![CDATA[]]></title>
    <code/>
    <parathrhseis/>
</some_text>

I try to set text to title in this function:

Dim myXmlDocument As XmlDocument = New XmlDocument()
Dim node As XmlNode
Dim s As String

s = "name.xml"
If System.IO.File.Exists(s) = False Then
    Return False
End If

myXmlDocument.Load(s)
node = myXmlDocument.DocumentElement

Try
    For Each node In node.ChildNodes
        If node.Name = "title" Then
            node.FirstChild.InnerText = "text"
            Exit For
        End If
    Next

    myXmlDocument.Save(s)
Catch e As Exception
        MsgBox("Error in XmlParsing: " + e.Message)
        Return False
End Try

Return True

The text is written correctly but the encoding changes to UTF-8 with BOM and also it
adds spaces:

<?xml version="1.0" encoding="UTF-8"?>
<some_text>
    <ada /> <- here
    <file /> <- here
    <title><![CDATA[text]]></title>
    <code /> <- here
    <parathrhseis /> <- here
</some_text>

How can i solve these problems

SOLUTION (with the help of Bradley Uffner)

Dim fileReader As String

Try
    fileReader = My.Computer.FileSystem.ReadAllText("original.xml")

    fileReader = fileReader.Replace("<ada />", "<ada/>")
    fileReader = fileReader.Replace("<file />", "<file/>")
    fileReader = fileReader.Replace("<code />", "<code/>")
    fileReader = fileReader.Replace("<parathrhseis />", "<parathrhseis/>")

    File.WriteAllText("copy.xml", fileReader) <- File.WriteAllText automatically stores it without the BOM
Catch ex As Exception
    MsgBox("Error: " + ex.Message)

    Return
End Try

1 Answer 1

3

This actually isn't a problem parsing the file, it's a problem saving it.

See this post for how to save xml without a BOM. XDocument: saving XML to file without BOM

The relevant code is:

Using writer = New XmlTextWriter(".\file.xml", New UTF8Encoding(False))
    doc.Save(writer)
End Using

Typically you can control the formatting of the document via the .Settings property of the XmlTextWriter, but I don't see a property to control the spacing of self closing elements. You might have better luck post-processing the output before saving to the filesystem by saving it to a stream and manually removing any spaces before "/>".

2
  • That is what i am trying to do now. Remove the spaces manually. Using the XmlTextWriter solves the problem with encoding but it also changes the text UTF-8 to utf-8!! I have to change this also. Apr 21, 2014 at 16:59
  • Just out of curiosity, why are the formatting requirements so strict? Apr 21, 2014 at 17:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.