Share via


GetNamespaceUri (Compact 2013)

3/26/2014

This interface returns the namespace URI of the node upon which the reader is currently positioned. If no namespace URI is available, this interface returns an empty string.

Syntax

HRESULT GetNamespaceUri (
  const WCHAR** ppwszNamespaceUri, 
  UINT* pcwchNamespaceUri
);

Arguments

  • ppwszNamespaceUri
    [out] The namespace URI of the node that the reader is currently positioned on. The returned string is always NULL-terminated.

    If no namespace URI is available, the returned string is empty.

    The passed in value cannot be NULL.

  • pcwchNamespaceUri
    [out] The size of the string, in characters, is returned.

    This value is optional. You can pass NULL for it, and the method will not return a value.

Return Value

Returns S_OK if no error is generated.

Remarks

For example, returns "u://1" for the element <xyz:abc xmlns:xyz="u://1" />.

Note

The pointer that is returned by GetNamespaceUri is only valid until you move the reader to another node. When you move the reader to another node, XmlLite can reuse the memory referenced by the pointer. Therefore, you should not use the pointer after you call one of the following methods: Read, MoveToNextAttribute, MoveToFirstAttribute, MoveToAttributeByName, or MoveToElement. Although they do not move the reader, the following two methods will also make the pointer invalid: SetInput and IUnknown::Release. If you want to preserve the value that was returned in ppwszNamespaceUri, you should make a deep copy.

The following code returns the namespace URI of the current node:

while (true)
{
    hr = pReader->Read(&nodetype);
    if (S_FALSE == hr)
        break;
    if (S_OK != hr)
    {
        wprintf(L"\nXmlLite Error: %08.8lx\n", hr);
        return -1;
    }
    switch (nodetype)
    {
    case XmlNodeType_Element:
        if (FAILED(hr = pReader->GetPrefix(&pwszPrefix, &cwchPrefix)))
        {
            wprintf(L"Error, Method: GetPrefix, error is %08.8lx", hr);
            return -1;
        }
        if (FAILED(hr = pReader->GetLocalName(&pwszLocalName, NULL)))
        {
            wprintf(L"Error, Method: GetLocalName, error is %08.8lx", hr);
            return -1;
        }
        if (cwchPrefix > 0)
            wprintf(L"%s:%s ", pwszPrefix, pwszLocalName);
        else
            wprintf(L"%s ", pwszLocalName);
        if (FAILED(hr = pReader->GetNamespaceUri(&pwszNamespaceUri, NULL)))
        {
            wprintf(L"Error, Method: GetNamespaceUri, error is %08.8lx", hr);
            return -1;
        }
        wprintf(L"Namespace=%s\n", pwszNamespaceUri);

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

When on the xml declaration, version, or encoding, the attribute count will be 2.

When on book, name, publisher, or address the attribute count will be 3.

This method lets the application writer to inspect the number of attributes on the node. This enables the allocation of an appropriate amount of memory for storing the attributes to provide performance benefits.

See Also

Reference

IXmlReader Methods
IXmlReader Properties
Error Codes