问题
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:
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 "/>".
来源:https://stackoverflow.com/questions/23200312/parse-an-xml-file-without-changing-the-encoding-and-preserving-the-file-format