XML parsing error with double quote character.

  • Thread starter Thread starter Terrence Chan
  • Start date Start date
T

Terrence Chan

I have try the below and the problem is my xmlstring is something like
"<employee name="Joh"n">

strXML = Replace(strXML, "&", "&amp;")
strXML = Replace(strXML, "<", "&lt;")
strXML = Replace(strXML, ">", "&gt;")
strXML = Replace(strXML, "'", "&apos;")
strXML = Replace(strXML, """", "&quot;")

after this it will be like this
<employee name=&quot;Joh&quot;n&quot;>

How to slove the double quote in xmlstring?

Thanks.
 
Terrence,
How to slove the double quote in xmlstring?
I would use a System.Xml.XmlTextWriter to create well formed XML!

You can use XmlTextWriter with a StringWriter to write the XML to a string.

Something like:

Dim buffer As New System.IO.StringWriter
Dim writer As New System.Xml.XmlTextWriter(buffer)

writer.WriteStartElement("employee")
writer.WriteAttributeString("name", "Joh""n")
writer.WriteEndElement()

writer.Close()
buffer.Close()

Dim xmlstring As String = buffer.ToString()
Debug.WriteLine(xmlstring, "xmlString")

Using XmlTextWriter will also ensure that any extended characters are
properly encoded, based on the System.Text.Encoding object passed to the
constructor. Note if you use a StringWriter, as I did above, the encoding is
going to be UTF-16, it can be changed with effort, post if want a sample.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top