簡單的VC 操作XML 檔案的的方法

來源:互聯網
上載者:User

//<![CDATA[
Sys.WebForms.PageRequestManager._initialize('AjaxHolder$scriptmanager1', document.getElementById('Form1'));
Sys.WebForms.PageRequestManager.getInstance()._updateControls(['tAjaxHolder$UpdatePanel1'], [], [], 90);
//]]>

簡單的VC 操作XML 檔案的的方法
首先建立一個XML檔案,我的機器軟體configuration如下:Windows xp professional sp2IE 6.0( 無關緊要)VS 2003------------------------------------------------------------------------------------------------------#import <msxml3.dll> //I have no idea why msxl4 cannot be used                          //in my PC, may be something on personal                         //discriminationusing namespace MSXML2;///the same to above///-----------------------------------------------------------------------------------建立檔案 int CreateXMLFile(char * xml_file_name){   // create an xml file named xml_file_name comtemporily  // if the return val is "0",it means that creation // if failed  ::CoInitialize(NULL);// this item must be included                            //it is fatal ,else it will failed                            //then at last “::CoUninitialize();”//matched  MSXML2::IXMLDOMDocumentPtr pDoc;  HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument30));//         //<msxml3.dll> version is 3.0  if(!(SUCCEEDED(hr)))//faild {                     return 0; }  MSXML2::IXMLDOMElementPtr pPolicy=pDoc->createElement("Local");  pDoc->appendChild(pPolicy);  { // the brackets including 1,2,3 can show the     //tree structure of the xml file     // nodes       //1         MSXML2::IXMLDOMElementPtr pPolicyType;           pPolicyType=pDoc->createElement("North");         pPolicyType->Puttext("Piking");          pPolicy->appendChild(pPolicyType);          pPolicyType=pDoc->createElement("South");                      pPolicy->appendChild(pPolicyType);         {//2.1                MSXML2::IXMLDOMElementPtr pControlType;                pControlType=pDoc->createElement("Shanghai");                      pControlType->Puttext("Xujiahui");                pPolicyType->appendChild(pControlType);                 pControlType.Release();         }//end 2.1         pPolicyType=pDoc->createElement("East");          pPolicy->appendChild(pPolicyType);             {//2.2                MSXML2::IXMLDOMElementPtr pProcessType;                pProcessType=pDoc->createElement("Heilongjiang");                 pProcessType->Puttext("harbin,Qiqihar");                pPolicyType->appendChild(pProcessType);                 pProcessType=pDoc->createElement("Liaoning");                      pProcessType->Puttext("ShenYang,Dalian");                pPolicyType->appendChild(pProcessType);                 pProcessType.Release();         }//end 2.2         _variant_t varXml(xml_file_name);         pDoc->save(varXml);    //save the file                  pPolicy.Release();         pPolicyType.Release(); }//end 1 pDoc.Release(); ::CoUninitialize();   //necessary it same as new and delete return 1; } 產生的檔案如下。。<Location><North> Pingking</North><South>   <Shanghai>Xujiahui</Shanghai></South><East>     <Heilongjiang>harbin,qiqihar</Heilongjiang>     <Liaoning>Shenyang,Dalian</Liaoning></East>-----------------------------------------------------------------------------------------尋找值訪問結點時,如果要讀<North>的值,就直接把”North”寫進去,返回的就是Pingking了,要訪問<Shanghai>要代入”South\\Shanghai”, 這個和註冊表分健值很像char* GetValueFromXml( char* nodeName){// get the value of certain node named nodeName // from the xml file // the return value 0 represent failure              char* nodeValue=NULL;              ::CoInitialize(NULL);              MSXML2::IXMLDOMDocumentPtr pDoc;              HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument30));               if(!(SUCCEEDED(hr)))              {                          return nodeValue;                                   }                  hr=pDoc->load("book.xml");              if(!SUCCEEDED(hr))              {                                 MSXML2::IXMLDOMElementPtr pPolicyNode=pDoc->GetdocumentElement();                                 MSXML2::IXMLDOMElementPtr pSelectNode=pPolicyNode->selectSingleNode(nodeName);                             nodeValue =    _com_util::ConvertBSTRToString(pSelectNode->text);                                   pPolicyNode.Release();                     pSelectNode.Release();              }              pDoc.Release();              ::CoUninitialize();              return nodeValue; }--------------------------------------------------------------------------------修改XML 檔案中的某一個結點的值存取方法同上int modify(const char* nodeName,char* nodeValue)//前者為結點名,後者為結點的字串值。{// modify the value of node named as nodeName // the new value is nodeValue // if the xml file is not existed then return  // 0       ::CoInitialize(NULL);       MSXML2::IXMLDOMDocumentPtr pDoc;       HRESULT hr = pDoc.CreateInstance(__uuidof(DOMDocument30));    int rtval=0;       if(!(SUCCEEDED(hr)))       {                   rtval=0;                 return rtval;       }           hr=pDoc->load("book.xml");// 檔案名稱。              if(!SUCCEEDED(hr))       {                            MSXML2::IXMLDOMElementPtr pPolicyNode=pDoc->GetdocumentElement();                          MSXML2::IXMLDOMElementPtr 二pSelectNode=pPolicyNode->selectSingleNode(nodeName);              pSelectNode->Puttext(nodeValue);               pDoc->save("book.xml");              pPolicyNode.Release();              pSelectNode.Release();              rtval=1;                               }       pDoc.Release();       ::CoUninitialize();       return rtval;       } -------------------------------------------------------------------------------------------------刪去結點

這個就是一個什麼remove 的成員函數就搞定了,和尋找的差不多,先找到要刪的結點,就行了。。當然刪後要儲存了,這一點不同,想必都知道。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.