我的代码是这样的:
HRESULT hr = m_plDomDocument.CreateInstance(MSXML2::CLSID_DOMDocument);
if (FAILED(hr))
{
_com_error er(hr);
AfxMessageBox(er.ErrorMessage());
}
CString strFileName ("PictureParameter.xml");
// convert xml file name string to something COM can handle (BSTR)
_bstr_t bstrFileName;
bstrFileName = strFileName.AllocSysString();
// call the IXMLDOMDocumentPtr's load function to load the XML document
variant_t vResult;
vResult = m_plDomDocument->load(bstrFileName);
if (((bool)vResult) == TRUE) // success!
{
m_pDocRoot = m_plDomDocument->documentElement;
}
else
{
AfxMessageBox("Document FAILED to load!");
} MSXML2::IXMLDOMNodePtr pCurrentNode = NULL;
MSXML2::IXMLDOMNodePtr pNode = NULL; //new node,
MSXML2::IXMLDOMNodePtr pNodeRoot = NULL; //new node,
pCurrentNode = m_pDocRoot; //set the top node as current
pNodeRoot=m_pDocRoot;
_variant_t varNodeType((short)MSXML2::NODE_ELEMENT); CEastDrawDoc* pDoc = GetDocument(); //获取文档指针
ASSERT_VALID(pDoc);
MSXML2::IXMLDOMElementPtr pElement=m_plDomDocument->selectSingleNode("//PictureParameter/DLine");
m_plDomDocument->removeChild (pElement);
/***********创建直线节点*********/
pNode= m_plDomDocument->createNode(varNodeType, _T("DLine"), _T(""));
ASSERT(NULL != pNode);
pCurrentNode->appendChild(pNode); //append it to the tree
/////////////////////////////////////////////////////////////////////
我想在每次load后只保留根节点,把上次生成的DLine节点扇到,请问怎么办?
我曾这样试过: MSXML2::IXMLDOMElementPtr pElement=m_plDomDocument->selectSingleNode("//PictureParameter/DLine");
m_plDomDocument->removeChild (pElement);
编译没问题,运行出错,请高手质点!!!
HRESULT hr = m_plDomDocument.CreateInstance(MSXML2::CLSID_DOMDocument);
if (FAILED(hr))
{
_com_error er(hr);
AfxMessageBox(er.ErrorMessage());
}
CString strFileName ("PictureParameter.xml");
// convert xml file name string to something COM can handle (BSTR)
_bstr_t bstrFileName;
bstrFileName = strFileName.AllocSysString();
// call the IXMLDOMDocumentPtr's load function to load the XML document
variant_t vResult;
vResult = m_plDomDocument->load(bstrFileName);
if (((bool)vResult) == TRUE) // success!
{
m_pDocRoot = m_plDomDocument->documentElement;
}
else
{
AfxMessageBox("Document FAILED to load!");
} MSXML2::IXMLDOMNodePtr pCurrentNode = NULL;
MSXML2::IXMLDOMNodePtr pNode = NULL; //new node,
MSXML2::IXMLDOMNodePtr pNodeRoot = NULL; //new node,
pCurrentNode = m_pDocRoot; //set the top node as current
pNodeRoot=m_pDocRoot;
_variant_t varNodeType((short)MSXML2::NODE_ELEMENT); CEastDrawDoc* pDoc = GetDocument(); //获取文档指针
ASSERT_VALID(pDoc);
MSXML2::IXMLDOMElementPtr pElement=m_plDomDocument->selectSingleNode("//PictureParameter/DLine");
m_plDomDocument->removeChild (pElement);
/***********创建直线节点*********/
pNode= m_plDomDocument->createNode(varNodeType, _T("DLine"), _T(""));
ASSERT(NULL != pNode);
pCurrentNode->appendChild(pNode); //append it to the tree
/////////////////////////////////////////////////////////////////////
我想在每次load后只保留根节点,把上次生成的DLine节点扇到,请问怎么办?
我曾这样试过: MSXML2::IXMLDOMElementPtr pElement=m_plDomDocument->selectSingleNode("//PictureParameter/DLine");
m_plDomDocument->removeChild (pElement);
编译没问题,运行出错,请高手质点!!!
用VB很容易做的的。
你的代码看上去没什么问题啊
我没写过,直觉是你的字符串都得使用Varient/String类型的
_bstr_t,
类似 L"//picture/dline"行不行?
出错。不知道为什么!错误信息:runtime error! abnormal program termination
MSXML2::IXMLDOMElementPtr pElement=pDocElement->selectSingleNode("DLine");
pDocElement->removeChild(pElement);
<DLne>
<DLine>23</DLine>
</DLne>
</root>
会如何? 只得到一个"首“节点