使用Microsoft WebServices解析wchar_t xml

时间:2019-07-14 08:10:37

标签: web-services visual-c++ xml-parsing

我正在尝试使用WebServices解析宽字符串。

HRESULT hr = NOERROR;
WS_ERROR* error = NULL;
WS_XML_READER* xmlReader = NULL;

// Create an error object for storing rich error information
hr = WsCreateError(
    NULL,
    0,
    &error);
if (FAILED(hr))
{
    goto Exit;
}

// Create an XML reader
hr = WsCreateReader(
    NULL,
    0,
    &xmlReader,
    error);
if (FAILED(hr))
{
    goto Exit;
}


WCHAR* xml =
    L"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"
    "<Orders xmlns='http://example.com'>"
    "<!-- Order #1 -->"
    "<PurchaseOrder id='1'>"
    "<Quantity>42</Quantity>"
    "<ProductName>Toaster</ProductName>"
    "</PurchaseOrder>"
    "<!-- Order #2 -->"
    "<PurchaseOrder id='2'>"
    "<Quantity>5</Quantity>"
    "<ProductName><![CDATA[Block&Tackle]></ProductName>"
    "</PurchaseOrder>"
    "</Orders>";
BYTE* bytes = (BYTE*)xml;
ULONG byteCount = (ULONG)wcslen(xml) * sizeof(WCHAR);

// Setup the source input
WS_XML_READER_BUFFER_INPUT bufferInput;
ZeroMemory(&bufferInput, sizeof(bufferInput));
bufferInput.input.inputType = WS_XML_READER_INPUT_TYPE_BUFFER;
bufferInput.encodedData = bytes;
bufferInput.encodedDataSize = byteCount;

// Setup the source encoding
WS_XML_READER_TEXT_ENCODING textEncoding;
ZeroMemory(&textEncoding, sizeof(textEncoding));
textEncoding.encoding.encodingType = WS_XML_READER_ENCODING_TYPE_TEXT;
textEncoding.charSet = WS_CHARSET_AUTO;

// Setup the reader
hr = WsSetInput(xmlReader, &textEncoding.encoding, &bufferInput.input, NULL, 0, error);
if (FAILED(hr))
{
    goto Exit;
}

我还尝试将textEncoding.charSet = WS_CHARSET_AUTO;更改为textEncoding.charSet = WS_CHARSET_UTF16LE;

上面的代码在调用WsReadNode(在附加的链接中)时始终失败,并且错误消息是“数据输入不是预期的格式或没有预期的值”。

WsReadNode

我无法将 WCHAR 数组转换为 CHAR 数组,因为XML可能包含非ASCII字符。

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:1)

示例xml在声明中具有编码

 L"<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"

将其更改为

L"<?xml version='1.0' encoding='UTF-16LE' standalone='yes'?>"

应纠正该错误。