Altova Mailing List Archives>Archive Index >microsoft.public.xml Archive Home >Recent entries >Thread Prev - XmlLite issue - IXmlReader::IsEmptyElement() >Thread Next - Re: XmlLite issue - IXmlReader::IsEmptyElement() Re: XmlLite issue - IXmlReader::IsEmptyElement()To: NULL Date: 1/31/2008 7:26:00 AM On Jan 31, 10:11 am, dj11235...@gmail.com wrote:
> Hello
>
> I'm working on a product that is using XmlLite. Following the example
> given by Microsoft on their website (http://msdn2.microsoft.com/en-us/
> library/ms753116(VS.85).aspx) I have made a while loop that calls
> IXmlReader::Read() and switches on the nodeType gotten. In the
> example, they use IXmlReader::IsEmptyElement() to determine if a node
> looks like this:
>
> <nodeName attributeName="attributeValue" />
>
> As I am walking through a hierarchical XML, I need to know when a node
> ends. So I have both that call to IXmlReader::IsEmptyElement() and
> code in the case for XmlNodeType_EndElement that walks me back up to
> the parent.
>
> Unfortunately, I am not seeing IXmlReader::IsEmptyElement() returning
> true ever. The node it is reading in my testing is this:
>
> <parameterType name="stringParameter" type="string"
> multiValued="false" />
>
> And the code reading that node is effectively this:
>
> while(S_OK == (hr = xmlReader->Read(&nodeType)))
> {
> switch(nodeType)
> {
> case XmlNodeType_Element:
> {
> if(FAILED(hr = xmlReader->GetLocalName(&nodeName, NULL)))
> // ... error handling
>
> // ... do stuff with the node
>
> while(true)
> {
> if(FAILED(hr = reader->MoveToNextAttribute()))
> // ... error handling
>
> if(S_FALSE == hr)
> break;
>
> if(FAILED(hr = reader->GetLocalName(&attributeName, NULL)))
> // ... error handling
>
> if(FAILED(hr = reader->GetValue(&attributeValue, NULL)))
> // ... error handling
>
> // ... do stuff with the attribute
> }
>
> // ... I never see this return true, even on the node example
> above
> if(! xmlReader->IsEmptyElement())
> // ... walk down the hierarchical tree of data
>
> // ... note that I expect to stay put (not walk down) if the
> element is empty
> }
> break;
> case XmlNodeType_EndElement:
> {
> // ... walk up the hierarchical tree of data
> }
> break;
>
> // ...
>
> If anyone has used IXmlReader::IsEmptyElement successfully, can you
> please tell me what you did to make it work? Or, if anyone else has
> encountered the same problem, I would be encouraged to hear that,
> because then I wouldn't feel so bad kludging the hierarchical logic.
Hi again
I have discovered the problem. The IXmlReader loses its state about
the element once you go read stuff about the attributes. The example
code provided by Microsoft (http://msdn2.microsoft.com/en-us/library/
ms753116(VS.85).aspx) is incorrect. You need to check
IsEmptyElement() before doing any work with the attributes. I am
posting this to hopefully help anyone who comes along with the same
problem later on. I now have this, and it works fine:
while(S_OK == (hr = xmlReader->Read(&nodeType)))
{
switch(nodeType)
{
case XmlNodeType_Element:
{
// ******************** new boolean to save the state
isEmptyElement = xmlReader->IsEmptyElement();
if(FAILED(hr = xmlReader->GetLocalName(&nodeName, NULL)))
// ... error handling
// ... do stuff with the node
while(true)
{
if(FAILED(hr = reader->MoveToNextAttribute()))
// ... error handling
if(S_FALSE == hr)
break;
if(FAILED(hr = reader->GetLocalName(&localAttributeName, NULL)))
// ... error handling
if(FAILED(hr = reader->GetValue(&value, NULL)))
// ... error handling
// ... do stuff with the attribute
}
// ******************** check my saved state rather than asking
the reader
if(! isEmptyElement)
// ... walk down the heirarchical tree of data
// ... note that I expect to stay put (not walk down) if the
element is empty
}
break;
case XmlNodeType_EndElement:
{
// ... walk up the hierarchical tree of data
}
break;
// ...
I find this behavior tremendously annoying, but at least I was able to
get things working.
| ||||||
| Company | Legal | Press | Partners | Careers | Sitemap | Contact Us | Altova Blog | Mobile | Full Site | |||
|
