Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6242461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:57:40+00:00 2026-05-24T11:57:40+00:00

I know there is an example in stackoverflow using JS, but I need to

  • 0

I know there is an example in stackoverflow using JS, but I need to accomplish this using C++ and by a strange reason I am not able to add the namespace to the root element using put_nodeValue() if the attribute name is prefixed with “xmlns:”.

So, I need something like this:

<entry xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
    <media:group />
    <media:group />
</entry>

I am trying with the following code:

HRESULT hr = S_OK;
IXMLDOMDocument *doc;
IXMLDOMNode *entryElement;
IXMLDOMNode *groupElement;
IXMLDOMNode *groupElement2;
IXMLDOMNode *titleElement;
IXMLDOMNode *mediaAttribute;

// Initialize ...
hr = CoInitialize(NULL);
assert(hr == S_OK);

// Create (root) document
hr = CoCreateInstance(
    CLSID_DOMDocument30,
    NULL,
    CLSCTX_INPROC_SERVER,
    IID_IXMLDOMDocument,
    (void**)&doc);
assert(hr == S_OK);

// Define namespaces
BSTR atomNamespace = L"http://www.w3.org/2005/Atom";
BSTR mediaNamespace = L"http://search.yahoo.com/mrss/";

// Define types
VARIANT elementType;
VariantInit(&elementType);
V_VT(&elementType) = VT_INT;
V_INT(&elementType) = NODE_ELEMENT;

VARIANT attributeType;
VariantInit(&attributeType);
V_VT(&attributeType) = VT_INT;
V_INT(&attributeType) = NODE_ATTRIBUTE;

// Add "entry" element
BSTR nodeName = L"entry";
hr = doc->createNode(elementType, nodeName, atomNamespace, &entryElement);
assert(hr == S_OK);

hr = doc->appendChild(entryElement, NULL);
assert(hr == S_OK);

// Add "media" namespace to "entry" element
nodeName = L"xmlns:media";
doc->createNode(attributeType, nodeName, atomNamespace, &mediaAttribute);

IXMLDOMNamedNodeMap *attributes;
entryElement->get_attributes(&attributes);

IXMLDOMNode *newAttribute;
hr = attributes->setNamedItem(mediaAttribute, &newAttribute);
assert(hr == S_OK);

VARIANT nodeValue;
VariantInit(&nodeValue);
V_VT(&nodeValue) = VT_BSTR;
V_BSTR(&nodeValue) = mediaNamespace;
hr = newAttribute->put_nodeValue(nodeValue);
assert(hr == S_OK);

// Add "group" element
nodeName = L"media:group";
hr = doc->createNode(elementType, nodeName, mediaNamespace, &groupElement);
assert(hr == S_OK);

hr = entryElement->appendChild(groupElement, NULL);
assert(hr == S_OK);

// Add second "group" element
nodeName = L"media:group";
hr = doc->createNode(elementType, nodeName, mediaNamespace, &groupElement2);
assert(hr == S_OK);

hr = entryElement->appendChild(groupElement2, NULL);
assert(hr == S_OK);

Any idea? I am using MSXML6.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:57:41+00:00Added an answer on May 24, 2026 at 11:57 am

    I got it! I received some help 🙂

    // Create (root) document
    hr = CoCreateInstance(
        CLSID_DOMDocument30,
        NULL,
        CLSCTX_INPROC_SERVER,
        IID_IXMLDOMDocument,
        (void**)&doc);
    assert(hr == S_OK);
    
    // Define namespaces
    BSTR atomNamespace = L"http://www.w3.org/2005/Atom";
    BSTR mediaNamespace = L"http://search.yahoo.com/mrss/";
    
    CComVariant elementType = NODE_ELEMENT;
    CComVariant attributeType = NODE_ATTRIBUTE;
    
    // Add "entry" element
    hr = doc->createNode(elementType, CComBSTR(L"entry"), atomNamespace, &entryNode);
    assert(hr == S_OK);
    
    hr = doc->appendChild(entryNode, NULL);
    assert(hr == S_OK);
    
    // Create "xmlns:media" attribute
    CComPtr<IXMLDOMElement> element;
    CComPtr<IXMLDOMAttribute> attr;
    
    doc->createNode(attributeType, CComBSTR(L"xmlns:media"), mediaNamespace, &attrNode);
    assert(hr == S_OK);
    
    hr = attrNode->put_nodeValue(CComVariant(mediaNamespace));
    assert(hr == S_OK);
    
    entryNode->QueryInterface(&element);
    assert(hr == S_OK);
    
    attrNode->QueryInterface(&attr);
    assert(hr == S_OK);
    
    element->setAttributeNode(attr, NULL);
    
    // Add first "group" element
    hr = doc->createNode(elementType, CComBSTR(L"media:group"), mediaNamespace, &groupNode1);
    assert(hr == S_OK);
    
    hr = entryNode->appendChild(groupNode1, NULL);
    assert(hr == S_OK);
    
    // Add second "group" element
    hr = doc->createNode(elementType, CComBSTR(L"media:group"), mediaNamespace, &groupNode2);
    assert(hr == S_OK);
    
    hr = entryNode->appendChild(groupNode2, NULL);
    assert(hr == S_OK);
    

    I just needed to add it as attribute and query the correct interfaces.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there are lots of examples out there on this, but they
I know there have been a few threads on this before, but I have
I know there are many database design tool, database modeling tool. Example, ER-win ,
I know there are thousands of examples on the internet, but I want for
I know there is a registry key indicating the install directory, but I don't
I know there are a lot of positive things mod-rewrite accomplishes. But are there
I know there is a way to add a IE control, how do you
There's a similar question on stackoverflow , but I wanted to ask it again
Guys, I opened this question (I hope could help some others) but reading stackoverflow
Sorry if this question sounds stupid, but I only know HTML/CSS and so I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.