user1073472
user1073472

Reputation: 139

vb.net XML enconding "&" to "&" in UTF-8

I'm using MS XmlWriter for creating a XML file in UTF-8.

Dim settings As New XmlWriterSettings()

settings.Indent = True
settings.IndentChars = "    "
settings.Encoding = System.Text.Encoding.UTF8      
settings.ConformanceLevel = ConformanceLevel.Fragment

Using xWriter As XmlWriter = XmlWriter.Create(filePath, settings)

If I try to write using WriteElementString with the value of & I get & in the output XML. In UTF-8 shouldn't it stay as "&"?

Upvotes: 0

Views: 1146

Answers (2)

Michael Kay
Michael Kay

Reputation: 163282

Escaping of special characters has nothing to do with encoding. Whether your encoding is UTF-8 or anything else, & and < must always be escaped in XML.

Upvotes: 1

reduckted
reduckted

Reputation: 2518

& is an illegal character in XML and must be escaped. See this answer: https://stackoverflow.com/a/730150/4397397

Upvotes: 0

Related Questions