Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
nsgmls parser giving error for UTF-8 encoded file "Character Data Not Allowed Here"
I am saving a file using the below function

C#
 Public Function SavetoFile(ByVal p_sFileName As String, ByVal p_sMessage As String, ByVal p_sEncoding As String) As Boolean
    Dim oEncoding As System.Text.Encoding = System.Text.Encoding.GetEncoding(p_sEncoding)
    Dim settings As System.Xml.XmlWriterSettings = New System.Xml.XmlWriterSettings()
    settings.Indent = True
    settings.OmitXmlDeclaration = True
    settings.Encoding = oEncoding
    Using writer As System.Xml.XmlWriter = System.Xml.XmlWriter.Create(p_sFileName, settings)
        writer.WriteStartDocument()
        writer.WriteRaw(p_sMessage)
        writer.Close()
    End Using
    Return True
End Function

The same file when saved under different encoding
case 1: p_sEncoding=SHIFT-JIS -> using nsgmls.exe validating the file saved above with the dtd .I am not getting any error
case 2: p_sEncoding=UTF-8 -> using nsgmls.exe validating the file saved above with the dtd .I am getting an error "Character Data Not Allowed Here"
Earlier the code used to save the file is as below

C#
Dim l_oReader As New MSXML2.SAXXMLReader60
Dim l_oWriter As New MSXML2.MXXMLWriter60
Dim l_oStream As New ADODB.Stream

l_oReader.contentHandler = l_oWriter
l_oWriter.byteOrderMark = False
l_oWriter.encoding = p_sEncoding
l_oWriter.omitXMLDeclaration = True
l_oWriter.indent = True
l_oStream.Open()
l_oStream.Charset = p_sEncoding
l_oWriter.output = l_oStream
l_oReader.parse(p_sMessage)
l_oWriter.flush()
l_oStream.SaveToFile(g_sTempProcessFolder & p_sFileName,   ADODB.SaveOptionsEnum.adSaveCreateOverWrite)

using the above way to save the file using UTF-8 then I didn't get any errors while parsing using nsgmls.exe

What I have tried:

all the documnets i could find on the google regarding nsgmls.exe
Posted
Updated 22-Feb-16 3:34am

1 solution

You may compare the generated files from all methods using a text editor to see what is the difference (the nsgmls message should give you the line number).

One point is that you are calling StartDocument() but not EndDocument().

A probable error source may be the passed p_sMessage string because you are using XmlWriter.WriteRaw Method (String) (System.Xml)[^]:
Quote:
The XmlWriter does not validate the data that is passed to the WriteRaw method. You should not pass arbitrary data to this method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900