Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: SetAttributes in TXML



Permlink Replies: 1 - Last Post: Mar 26, 2018 9:55 AM Last Post By: Remy Lebeau (Te...
vijayalakshmi b

Posts: 20
Registered: 2/9/18
SetAttributes in TXML
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 26, 2018 3:29 AM
Hi,
I am using SetAttrribute as follows
void TXML_Node::AddAttribute(const std::wstring &i_wstAttributeName, const std::wstring &i_wstAttributeValue)
{
m_pNode->SetAttribute(i_wstAttributeName.c_str(), i_wstAttributeValue.c_str());
}
here I'm passing Attribute value as unicodestring .
But it writes the attribute as "true"
Is there a way I can pass it as string if so How??
Thanks in advance

Edited by: vijayalakshmi b on Mar 26, 2018 3:30 AM

Edited by: vijayalakshmi b on Mar 26, 2018 3:30 AM

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: SetAttributes in TXML [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Mar 26, 2018 9:55 AM   in response to: vijayalakshmi b in response to: vijayalakshmi b
vijayalakshmi b wrote:

Is there a way I can pass it as string if so How??

Assuming m_pNode is an IXMLNode, then the 2nd parameter of
IXMLNode::SetAttribute() takes a System::OleVariant as input.

You are passing in a 'const wchar_t*' (a non-const pointer to a const
wchar_t), but OleVariant does not have a constructor that accepts a
`const wchar_t*` as input. However, all pointers are implicitly
convertible to 'bool', and OleVariant does have a non-explicit
constructor that accepts a 'bool' as input. That is the constructor
that your code is calling, which is why you see "true" since your
'wchar_t*' pointer is never NULL.

OleVariant does have a constructor that accepts a 'wchar_t* const' (a
const pointer to a non-const wchar_t), and casting away your 'wchar_t*'
pointer's constness, such as with const_cast, does call that
constructor instead. However, that constructor only works correctly
with OLE 'BSTR' strings, which yours is not.

The safest way to make your code work is to explicitly convert your
'std::wstring' to a 'System::WideString' (CB2007 and earlier) or
'System::UnicodeString' (CB2009 and later), eg:

void TXML_Node::AddAttribute(const std::wstring &i_wstAttributeName,
const std::wstring &i_wstAttributeValue)
{
    m_pNode->SetAttribute(i_wstAttributeName.c_str(),
WideString(i_wstAttributeValue.c_str(), i_wstAttributeValue.length()));
}


void TXML_Node::AddAttribute(const std::wstring &i_wstAttributeName,
const std::wstring &i_wstAttributeValue)
{
    m_pNode->SetAttribute(i_wstAttributeName.c_str(),
UnicodeString(i_wstAttributeValue.c_str(),
i_wstAttributeValue.length()));
}


--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02