Share via


Read (Compact 2013)

3/26/2014

This interface reads the next node from the stream and returns the type of the node.

Syntax

HRESULT Read (
  XmlNodeType* pXmlNodeType
);

Arguments

  • pXmlNodeType
    [out] Returns the type of node that is read. This value can be NULL. In this case, the node type is not returned.

Return Value

This interface returns S_OK if no error is generated. At the end of the input stream, returns S_FALSE.

Remarks

In XmlLite, the reader is positioned on a node. When you obtain the value and attributes of the node, they are 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; successive calls to the methods that retrieve 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.

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

while (S_OK == (hr = pReader->Read(&nodeType)))
{
    switch (nodeType)
    {
    case XmlNodeType_XmlDeclaration:
        wprintf(L"XmlDeclaration\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);

        if (FAILED(hr = WriteAttributes(pReader)))
        {
            wprintf(L"Error writing attributes, error is %08.8lx", hr);
            return -1;
        }

        if (pReader->IsEmptyElement() )
            wprintf(L" (empty)");
        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:
    case XmlNodeType_Whitespace:
        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_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;
    }
}

See Also

Reference

IXmlReader Methods
MoveToFirstAttribute
MoveToNextAttribute
MoveToAttributeByName
ReadValueChunk
IXmlReader Properties
Error Codes