다음을 통해 공유


GetNamespaceUri

 

Gets the namespace URI of the node that the reader is currently positioned on. If no namespace URI is available, returns an empty string.

Syntax

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

Arguments

ppwszNamespaceUri
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
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 returned by GetNamespaceUri is only valid until you move the reader to another node. When you move the reader to another node, XmlLite may reuse the memory referenced by the pointer. Therefore, you should not use the pointer after calling 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 gets 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;  
    }  
}  

Requirements

Header: XmlLite.h

Library: XmlLite.lib

See Also

IXmlReader Methods