Compartir a través de


Read

 

Reads the next node from the stream and returns the type of the node.

Syntax

  
HRESULT Read ([out] XmlNodeType * pXmlNodeType);  

Arguments

pXmlNodeType
Returns the type of node that is read. This value can be NULL, in which case the node type will not be returned.

Return Value

Returns S_OK if no error is generated. Returns E_PENDING if the data is unavailable, and the stream is paused. At the end of the input stream, returns S_FALSE.

Remarks

In XmlLite, the reader is positioned on a node. When you get the value and attributes of the node, you get them for the node on which the reader is positioned. The Read method moves the position of the reader to the next node in the stream; subsequent calls to the methods that get the values, name, and attributes return them for the new node.

This method skips attributes. To read attributes use the MoveToFirstAttribute, MoveToNextAttribute, and MoveToAttributeByName methods.

This method returns E_PENDING if the data is unavailable, and the stream is paused. The handling of the E_PENDING return value is up to the calling method.

The following code calls the Read method until there are no more nodes to read:

   // Read through each node until the end  
   while (!pReader->IsEOF())  
   {  
  hr = pReader->Read(&nodeType);  
  
  // Check if E_PENDING is returned and perform custom action  
        // This is a sample of how one might handle E_PENDING  
  if (hr == E_PENDING)  
  {  
  // Alert user to the pending notification  
  wprintf(L"Error pending, error is %08.8lx", hr);  
  
  // As long as E_PENDING is returned keep trying to read  
  while (hr == E_PENDING){  
                    ::Sleep(1000);  
  hr = pReader->Read(&nodeType);  
  }  
  
  continue;  
  }  
  
  if (hr != S_OK)  
  break;  
  
      switch (nodeType) {  
  
      case XmlNodeType_XmlDeclaration:  
         wprintf(L"XmlDeclaration\n\n");  
         if (FAILED(hr = WriteAttributes(pReader))) {  
            wprintf(L"Error writing attributes, error is %08.8lx", hr);  
            return -1;  
         }  
         break;  
  
      case XmlNodeType_Element:  
         if (FAILED(hr = pReader->GetPrefix(&pwszPrefix, &cwchPrefix))) {  
            wprintf(L"Error getting prefix, error is %08.8lx", hr);  
            return -1;  
         }  
         if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL))) {  
            wprintf(L"Error getting local name, error is %08.8lx", hr);  
            return -1;  
         }  
  
         if (cwchPrefix > 0)  
 wprintf(L"Element: %s:%s\n", pwszPrefix, pwszLocalName);  
         else  
            wprintf(L"Element: %s\n", pwszLocalName);  
  
 pReader->GetAttributeCount(&nAttrCount);  
 if (nAttrCount > 0){  
 if (FAILED(hr = WriteAttributes(pReader))) {  
wprintf(L"Error writing attributes, error is %08.8lx", hr);  
return -1;  
 }  
 if (FAILED(hr = pReader->MoveToElement())) {  
wprintf(L"Error moving to the element that owns the current attribute node, error is %08.8lx", hr);  
return -1;  
 }  
 }  
 if (pReader->IsEmptyElement() )  
wprintf(L" (empty element)\n");  
 break;  
  
      case XmlNodeType_EndElement:  
         if (FAILED(hr = pReader->GetPrefix(&pwszPrefix, &cwchPrefix))) {  
            wprintf(L"Error getting prefix, error is %08.8lx", hr);  
            return -1;  
         }  
         if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL))) {  
            wprintf(L"Error getting local name, error is %08.8lx", hr);  
            return -1;  
         }  
         if (cwchPrefix > 0)  
            wprintf(L"End Element: %s:%s\n", pwszPrefix, pwszLocalName);  
         else  
            wprintf(L"End Element: %s\n", pwszLocalName);  
         break;  
  
      case XmlNodeType_Text:  
  if (FAILED(hr = pReader->GetValue(&pwszValue, NULL))){  
  wprintf(L"Error getting value, error is %08.8lx", hr);  
  return -1;  
  }  
  wprintf(L"Text: %s\n", pwszValue);  
  break;  
  
      case XmlNodeType_Whitespace:  
         break;  
  
      case XmlNodeType_CDATA:  
         if (FAILED(hr = pReader->GetValue(&pwszValue, NULL))) {  
            wprintf(L"Error getting value, error is %08.8lx", hr);  
            return -1;  
         }  
 wprintf(L"CDATA: %s\n", pwszValue);  
         break;  
  
      case XmlNodeType_ProcessingInstruction:  
         if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL))) {  
            wprintf(L"Error getting name, error is %08.8lx", hr);  
            return -1;  
         }  
         if (FAILED(hr = pReader->GetValue(&pwszValue, NULL))) {  
            wprintf(L"Error getting value, error is %08.8lx", hr);  
            return -1;  
         }  
         wprintf(L"Processing Instruction name:%S value:%S\n", pwszLocalName, pwszValue);  
         break;  
  
      case XmlNodeType_Comment:  
         if (FAILED(hr = pReader->GetValue(&pwszValue, NULL))) {  
            wprintf(L"Error getting value, error is %08.8lx", hr);  
            return -1;  
         }  
         wprintf(L"Comment: %s\n", pwszValue);  
         break;  
  
      case XmlNodeType_DocumentType:  
         wprintf(L"DOCTYPE is not printed\n");  
         break;  
      }  
   }  
  

Requirements

Header: XmlLite.h

Library: XmlLite.lib

See Also

IXmlReader Methods
ReadValueChunk