您的位置:首页 > 其它

VC 使用MSXML创建新结点时出现xmlns="" 属性解决方法

2013-04-16 11:00 537 查看
在VC中使用MSXML创建xml文件,在使用下面代码:

问题原因:当父节点具有XMLNS属性时,子节点必须指定XMLNS属性,但是当子节点的XMLNS属性与父节点命名空间相同时,子节点不显示XMLNS属性.

上面问题的根本所在就是我们理解上的错误,我们认为没有为子节点指定命名空间,就不应该出现此属性,恰恰相反,当我们为其指定与父节点相同的命名空间时,此属性才不会出现

那么如何去掉前缀xmlns=""呢?

代码如下::

pXMLSubElementRoot = pDoc->createNode("element","LoginInfo","http://DCMPlatform/v2.0");

pXMLRoot - > a(pXMLSubElementRoot);

而不用createElement_x_x_x_x_x

其中LoginInfo节点名称. http://DCMPlatform/v2.0 是前面定义的名字空间名!

或者是将父节点的信息设置如下,子节点的默认节点名字空间与父节点一样:
父节点修改代码如下:
    pXMLRoot
= pDoc->selectSingleNode("DCMCatalogueResult");
    pXMLRoot->setAttribute("xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
    pXMLRoot->setAttribute("xmlns:xsd","http://www.w3.org/2001/XMLSchema");
    pXMLRoot->setAttribute("xmlns","http://DCMPlatform/v2.0");


[cpp] view
plaincopy

<pre class="cpp" name="code">//创建一个层      

pLeyer = pDoc->createElement((_bstr_t)"g");  

pLeyer->setAttribute("id","Head_Layer");  

//创建一个节点      

pNode = pDoc->createElement((_bstr_t)"rect");  

pNode->setAttribute("x","0");  

pNode->setAttribute("y","0");  

pNode->setAttribute("width","1650");  

pNode->setAttribute("height","906");  

pNode->setAttribute("fill","rgb(0,0,0)");  

//添加节点到层  

pLeyer->appendChild(pNode);  

//将层添加到根  

xmlRoot->appendChild(pLeyer);  

</pre>  

<pre></pre>  

<p>建立一个新结点时,生成的结点中出现xmlns="" 属性,如下xml:</p>  

<pre class="html" name="code"><g xmlns="" id="Head_Layer">  

    <rect x="0" y="0" width="1650" height="906" fill="rgb(0,0,0)"/>  

</g></pre>  

<p>自动的添加了xmlns="" 属性,查了资料,解决办法如下代码:</p>  

<pre class="cpp" name="code">/********引用包含**********/  

VARIANT   vtTemp;   

vtTemp.vt   =   VT_I2;   

vtTemp.iVal   =   1;     

_bstr_t   namespaceURI="http://www.w3.org/2000/svg";   

  

//创建一个层   

pLeyer = pDoc->createNode(vtTemp,(_bstr_t)"g",namespaceURI);  

pLeyer->setAttribute("id","Head_Layer");  

//创建一个节点      

pNode = pDoc->createNode(vtTemp,(_bstr_t)"rect",namespaceURI);  

pNode->setAttribute("x","0");  

pNode->setAttribute("y","0");  

pNode->setAttribute("width","1650");  

pNode->setAttribute("height","906");  

pNode->setAttribute("fill","rgb(0,0,0)");  

//添加节点到层  

pLeyer->appendChild(pNode);  

//将层添加到根  

xmlRoot->appendChild(pLeyer);</pre>  

<p>使用createNode创建结点。<br>  

</p>  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MSXML xmlns vc
相关文章推荐