0

我正在尝试使用 XMLLITE 读取器和写入器更新 XML 文档节点属性,但我做不到。当我尝试添加新属性时,作者正在添加。

我的问题是是否可以使用 XMLLITE 更新现有的 XML 节点属性值?

<parent>
    <child Name="AAA">Yes
    </child>
</parent>

我想更新上面的 XML Node Attribute of Name

<parent>
    <child Name="BBB">Yes
    </child>
</parent>

XMLLIte c++ 代码

//if the element is price, then discount price by 25%   
                                    if (wcscmp(pQName, L"child") == 0)
                                    {
                                        inPrice = TRUE;
                                        /*if (FAILED(hr = pWriter->WriteAttributeString(NULL, L"test", NULL, L"TEST")))
                                        {
                                            wprintf(L"Error writing WriteAttributeString, error is %08.8lx", hr);
                                            return -1;
                                        }*/

                                        /*if (FAILED(hr = pWriter->WriteNodeShallow(pReader, FALSE)))
                                        {
                                            wprintf(L"Error writing WriteNodeShallow, error is %08.8lx", hr);
                                            return -1;
                                        }*/

                                        //if (FAILED(hr = pReader->MoveToAttributeByName(L"Name", NULL)))
                                        if (FAILED(hr = pReader->MoveToFirstAttribute()))
                                        {
                                            wprintf(L"Error Moving to Attribute, error is %08.8lx", hr);
                                        }

                                        LPCWSTR  AttributeValue = NULL ; 

                                        if (FAILED(hr = pReader->GetValue(&AttributeValue, NULL)))
                                        {
                                            wprintf(L"Error Moving to Attribute, error is %08.8lx", hr);
                                        }

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

                                        inPrice = TRUE;
                                        if (FAILED(hr = pWriter->WriteAttributeString(NULL, L"Name", NULL, L"BBB")))                                            
                                        {
                                        wprintf(L"Error writing WriteAttributeString, error is %08.8lx", hr);
                                        return -1;
                                        }


                                    }
                                    else
                                    {
                                        inPrice = FALSE;
                                        if (FAILED(hr = pWriter->WriteNodeShallow(pReader, FALSE)))
                                        {
                                            wprintf(L"Error writing WriteNodeShallow, error is %08.8lx", hr);
                                            return -1;
                                        }

                                    }

正如您所提到的,我尝试这样做,我尝试设置属性的值,但它没有设置现有属性的值,但是如果我尝试添加新属性,那么它会添加属性。

当我尝试循环遍历节点列表时,节点类型永远不会出现 XmlNodeType_Attribute: I 'M NOT SURE WHY?

请给我你的建议,

谢谢卡提克

4

0 回答 0