VC,VB操作XML

TCHAR buffer[MAX_PATH] = {0};
 ::GetModuleFileName(NULL, buffer, MAX_PATH);
 CString strPath = buffer;
 int nIndex = strPath.ReverseFind(_T('\\'));
 strPath = strPath.Left(nIndex + 1);
 CString strBomFile = strPath + _T("Bom.xml");
 
 MSXML2::IXMLDOMDocumentPtr pDocXML = NULL;
 MSXML2::IXMLDOMElementPtr pRoot = NULL;
 MSXML2::IXMLDOMElementPtr pNode = NULL;
 HRESULT hr = pDocXML.CreateInstance(__uuidof(MSXML2::DOMDocument));
 ASSERT(SUCCEEDED(hr));
 pRoot = pDocXML->createElement((_bstr_t)(_T("BomData")));
 pDocXML->appendChild(pRoot);
 //3-1、遍历BomRcdArr
 for(long i = 0;i<27;i++)
 {
 
  CString strFilePath = strBomFile;
     
  //write to xml
  short iNum=0;
  iNum=10;
  BSTR strKey;
  BSTR strValue;
  short ii=0;
 
 
 
  for (ii=0;ii<iNum;ii++)
  {
   strKey = L"KEY";
   strValue =L"VALUE";
   //pITHDbBomRecorder->GetItem(&strKey,&strValue,ii);
 
   pNode=NULL;
   pNode = pDocXML->createElement((_bstr_t)(_T("Node")));
   pNode->put_text((_bstr_t)_T("BomData"));//设置标签的文本内容;
       
   pNode->setAttribute((_bstr_t)_T("BomDataBigNum"),(_variant_t)i);
   pNode->setAttribute((_bstr_t)_T("BomDataSmallNum"),(_variant_t)ii);
   pNode->setAttribute((_bstr_t)_T("key"),(_variant_t)strKey);
   pNode->setAttribute((_bstr_t)_T("value"),(_variant_t)strValue);
 
   pRoot->appendChild(pNode);
 
  }
 
 }
 pDocXML->save( _bstr_t(strBomFile));
 pNode->Release();
 pRoot->Release();
 pDocXML->Release(); 

 

Private Sub Command1_Click()
Dim xDoc As Object
Dim xmlFile As String
Dim strWidth As String
Dim strHeight As String
Set xDoc = CreateObject("MSXML2.DOMDocument")
xmlFile = "C:\aa.xml"
xDoc.Load xmlFile
strWidth = xDoc.selectSingleNode("overlays").selectSingleNode("Info").Attributes(2).Text
strHeight = xDoc.selectSingleNode("overlays").selectSingleNode("Info").Attributes(3).Text
MsgBox ("Width=" & strWidth & " Height=" & strHeight)
xDoc.selectSingleNode("overlays").selectSingleNode("Info").Attributes(2).Text = "123"
xDoc.selectSingleNode("overlays").selectSingleNode("Info").Attributes(3).Text = "456"
xDoc.save (xmlFile)
End Sub

注意 你上面的XML中Width="840"Height="630" 要分开,改成:
 Width="840" Height="630"

 

转载于:https://www.cnblogs.com/liaocheng/p/4243658.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值