0

I'm trying to update a XML document NODE attributes using the XMLLITE reader and writer , but i couldn't. When i try to add new attribute then the writer is adding.

My Question is Is it possible to update the Existing XML Node attribute values using XMLLITE?

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

I want to update the above XML Node Attribute of Name

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

XMLLIte c++ Code

//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;
                                        }

                                    }

I tried doing that as you have mentioned, I tried to set the value of the attribute , but it not setting the value for the existing attribute , but if i tried to add a new attribute then its adding the attribute.

When i tried to loop the through node list , the node type never comes to the case XmlNodeType_Attribute: I 'M NOT SURE WHY?

Please give me your suggestions,

Thanks karthik

Karthik SV
  • 99
  • 1
  • 9
  • Please show us what you tried – john Aug 27 '15 at 05:29
  • I want to update the child Name attribute of the above as given below, – Karthik SV Aug 27 '15 at 06:44
  • It's pretty simple, just write code that keeps track of where you are. So look for the child element and set inChild to true. Then when you are on an attribute and inChild is true then check if the attribute name is "Name". If it is then write the value you want instead of the real value with `pWriter->WriteAttributeString()`. – john Aug 27 '15 at 20:51
  • I tried doing that as you have mentioned, I tried to set the value of the attribute , but it not setting the value for the existing attribute , but if i tried to add a new attribute then its adding the attribute. – Karthik SV Aug 28 '15 at 07:05
  • You don't have to set the value of an attribute, you don't have to add a new attribute. All you have to do is **output** the attribute you want **instead of** the attribute you don't want. – john Aug 28 '15 at 07:07
  • Can you give some code for this? – Karthik SV Aug 28 '15 at 07:15

0 Answers0