1

I'm very new to XML parsing and don't really know what I'm doing. I'm trying to parse a file in XML, but have encountered this error:

System.Xml.XmlException: ''Element' is an invalid XmlNodeType.'

Here's the code below:

    Dim settings As XmlReaderSettings = New XmlReaderSettings
    settings.DtdProcessing = DtdProcessing.Parse
    Dim xmlR = XmlReader.Create(jmdictpath, settings)

    Do While xmlR.Read()
        If Not xmlR.IsEmptyElement And xmlR.NodeType = XmlNodeType.Element Then
            MsgBox(xmlR.ReadElementContentAsString)
        End If
    Loop

The error runs on the line MsgBox(xmlR.ReadElementContentAsString). I've tested that the element is not empty, so that shouldn't be a problem.

Then I checked the xmlReader object in QuickWatch, and it looks like what the object is currently trying to read is a whitespace or line return character:

enter image description here

Is there some standard method to handle all cases like this when parsing XML so that whitespace characters don't get picked up as XML tags and confuse the reader?

Lou
  • 2,200
  • 2
  • 33
  • 66
  • Have a look at your `Not` with your `And`. Are you sure it is evaluating like ((Not first) and second) vs (not(first and second))? Try adding some parens and let us know if it behaves differently. – tgolisch Feb 12 '20 at 21:11
  • 1
    I think you might be having issues similar to [this](https://stackoverflow.com/questions/9399850/c-sharp-whitespaces-issue-with-xmlreader) Try setting the IgnoreWhitespace property to true on XmlReaderSettings – jazakari Feb 12 '20 at 21:17
  • @jazakari That seemed to fix the problem, thanks :). I now have a different problem but it's for a different question and some more research on how XML parsing works I guess haha – Lou Feb 12 '20 at 21:39

0 Answers0