bool FindSetData( MCD_CSTR szName, MCD_CSTR szData, int nFlags = 0 );
FindSetData method combines FindElem and SetData into one convenient method plus additional capability for creating elements and setting an attribute value. Together with the FindGetData method you have single calls to get and set/create element and attribute values.
Update April 1, 2007: In release 9.0
FindSetData will create the element if it doesn't exist.
Update June 7, 2009: In release 11.1
FindSetData can set an attribute value.
FindSetData finds or creates the element, sets that element as the main position element, and sets either the element's data value or the attributes value with the
szData string data value. If setting the element's data value, this will remove any child elements of the element if any. It returns
false if it does not find or create the element.
With the path for the
szName argument as described in the FindElem method, this is a powerful way to set a data value or attribute value in a document without several navigation calls. See more information about this under the FindGetData method documentation. For example, calling
FindSetData("/Configuration/ShowUser","0") sets the ShowUser element data value to
Together with the FindGetData method, this is ideal for keeping configuration and messaging structures in XML documents. See Dynamic Structure Documents.
In order to create a non-existing element, a simple absolute path with no predicates must be specified. It will even create the parent elements necessary up to the leaf element. In the following example, a CMarkup object is instantiated and with one call to
FindSetData, multiple levels of elements are created.
CMarkup xml; xml.FindSetData("/Configuration/Dir", "C:\\Temp" );
<Configuration> <Dir>C:\Temp</Dir> </Configuration>
To set an attribute, follow the path of the element with
/@ plus the value. In the following example we set the nickname attribute of the Dir element:
xml.FindSetData("/Configuration/Dir/@nickname", "temp" );
<Configuration> <Dir nickname="temp">C:\Temp</Dir> </Configuration>
One more call adds more elements (indentation added for illustration):
FindSetData("/*/WindowPos/Right", "400" );
<Configuration> <Dir nickname="temp">C:\Temp</Dir> <WindowPos> <Right>400</Right> </WindowPos> </Configuration>
As with SetData, the
MNF_WITHREFS flags can be used to affect how the data content is stored when an element is specified, and as with SetAttrib, the
MNF_WITHREFS flag can be specified when setting an attribute value.