0

I'm using Microsoft's XmlLite DLL to parse a simple XML file, using the code in the example XmlLiteReader. The essential part of the code (C++) is

    while(S_OK == (hr = pReader->Read(&nodeType)))
    {
        switch(nodeType)
        {
        case XmlNodeType_Element:
            // Get name...
            WriteAttributes(pReader, es, attributes);
            break;
        case XmlNodeType_EndElement:
            // Process end-of-element...
            break;
    }

and

HRESULT WriteAttributes(IXmlReader* pReader, CString& es, StringStringMap& attributes)
{
    while(TRUE)
    {
        // Get and store an attribute...
        HRESULT hrMove = pReader->MoveToNextAttribute();
    }
    // ...
}

So, here's my question. With XML input such as

<?xml version="1.0" encoding="utf-8"?>
<settings version="1.2">
 <runID name="test" mode="N" take_data="Y">
  <cell id="01">
   <channel id="A" sample="something"/>
   <channel id="B" sample="something else"/>
  </cell>
  <cell id="03">
   <channel id="A" sample="other something"/>
   <channel id="B" sample="other something else"/>
  </cell>
 </runID>
</settings>

Everything works as expected, except that the /> at the end of each channel line, which indicates the end of the element channel, isn't recognized as the end of an element. The successive node types following channel are whitespace (\n), then element (the second channel).

How can I determine from XmlLite that element `channel' has ended? Or am I misunderstanding the XML syntax?

Woody20
  • 791
  • 11
  • 30

1 Answers1

1

You can test if an element ends with /> by using the function IsElementEmpty.

Name
  • 3,430
  • 4
  • 30
  • 34
  • Take care to observe the remarks in the MSDN documentation for IsElementEmpty. It needs to be invoked while the reader's current node is the element node, not any attribute nodes. From MSDN: ```You should save the value of IsEmptyElement before processing attributes, or call MoveToElement to make IsEmptyElement valid after processing attributes.``` – Nathan Schubkegel Sep 15 '18 at 09:32