본문 바로가기
MFC

MSXML에서 createElement를 사용할 때 자동으로 xmlns="" 속성이 붙는 이유와 막는 방

by leo21c 2025. 12. 19.

MSXML에서 createElement사용할 자동으로 xmlns="" 속성이 붙는 이유는 네임스페이스를 지정하지 않고 요소를 생성했기 때문입니다. 이를 막으려면 createElement 대신 createNode를 사용하여 네임스페이스를 명시적으로 지정하거나, createElement 호출 시 올바른 네임스페이스를 함께 전달해야 합니다.

 

https://learn.microsoft.com/en-us/answers/questions/857792/how-to-remove-the-xmlns-attribute-that-gets-added

 

how to remove the xmlns attribute that gets added by the xmldocument - Microsoft Q&A

Hi experts , i have a scenario, where i have to read the xml document and create/add an xmlnode to the document. newElem = xmlDocument.CreateNode("element", "newnode", ""); //creates the node newElem.InnerText…

learn.microsoft.com

 

1. createNode 사용하기

MFC에서 COM 인터페이스를 직접 호출하면 됩니다.

#import <msxml6.dll> named_guids

MSXML2::IXMLDOMDocumentPtr pDoc;
pDoc.CreateInstance(__uuidof(MSXML2::DOMDocument60));

// 루트 생성
MSXML2::IXMLDOMElementPtr pRoot = pDoc->createElement(L"root");
pDoc->appendChild(pRoot);

// 네임스페이스 없는 노드 생성
MSXML2::IXMLDOMNodePtr pNode = pDoc->createNode(
    MSXML2::NODE_ELEMENT,   // 노드 타입
    L"child",               // 이름
    L""                     // 네임스페이스 URI (빈 문자열)
);
pNode->text = L"Hello";
pRoot->appendChild(pNode);

 

2. createElement 대신 네임스페이스 지정

MSXML2::IXMLDOMElementPtr pElem = pDoc->createElement(L"ns:child");
pElem->setAttribute(L"xmlns:ns", L"http://example.com/ns");
pRoot->appendChild(pElem);

 

3. 이미 붙은 xmlns="" 제거

pElem->removeAttribute(L"xmlns");

 

정리

  • MFC에서 MSXML 사용 시 createElement만 쓰면 xmlns=""가 자동 추가됨.
  • 해결책은
    1. createNode(NODE_ELEMENT, name, "") 사용
    2. createElement 호출 시 네임스페이스 지정
    3. 필요 시 removeAttribute("xmlns")로 제거
LIST