vc中操作Xml--使用CMarkup类



vc中操作Xml--使用CMarkup类

分类: C#专题之XML应用技术 843人阅读 评论(1) 收藏 举报
encoding xml user null 网络

目录(?)[+]

  1. 生成UserInfoxml
  2. 浏览特定元素
  3. 修改
  4. 添加
  5. 删除
  6. 查找
  7. 产生
  8. 查找
  9. 修改

 

DotNet中使用xml相对比较轻松,在vc中操作xml,有些繁琐。最近在网络上得知CMarkup比较好用,于是试了下,还好,就把部分例子写出来。以后有时间的话,再写清楚点。

 

 

[xhtml] view plain copy print ?
  1. //----------UserInfo.xml--------------     
  2.     
  3. <?xml version="1.0" encoding="UTF-8" ?>      
  4.     
  5.  <UserInfo>     
  6.     
  7.   <UserID>luo</UserID>      
  8.     
  9.   <UserID>lin</UserID>      
  10.     
  11.   </UserInfo>    
//----------UserInfo.xml-------------- <?xml version="1.0" encoding="UTF-8" ?> <UserInfo> <UserID>luo</UserID> <UserID>lin</UserID> </UserInfo>

 

1. 生成UserInfo.xml

[c-sharp] view plain copy print ?
  1. CMarkup xml;      
  2. xml.SetDoc("<?xml version=/"1.0/" encoding=/"UTF-8/"?>/r/n");      
  3. xml.AddElem("UserInfo");      
  4. xml.IntoElem();      
  5. xml.AddElem("UserID","luo");      
  6. xml.AddElem("UserID","lin");      
  7. xml.OutOfElem();      
  8. xml.Save("UserInfo.xml");     
CMarkup xml; xml.SetDoc("<?xml version=/"1.0/" encoding=/"UTF-8/"?>/r/n"); xml.AddElem("UserInfo"); xml.IntoElem(); xml.AddElem("UserID","luo"); xml.AddElem("UserID","lin"); xml.OutOfElem(); xml.Save("UserInfo.xml");

 

2. 浏览特定元素

[c-sharp] view plain copy print ?
  1. //----查看所有UserID----      
  2. CMarkup xml;      
  3. xml.Load("UserInfo.xml");      
  4. BOOL bFind = true;      
  5. xml.ResetMainPos();      
  6. while (xml.FindChildElem("UserID");)      
  7. {      
  8.       //此时接点还是父接点      
  9.         CString strTagName = _T("");      
  10.         CString strData = _T("");      
  11.         strTagName = xml.GetChildTagName();      
  12.         strData = xml.GetChildData();      
  13.         TRACE("/n---tagName:%s,Data:%s--/n",strTagName,strData);      
  14. }      
  15.       /********************这样也可以******************    
  16.       CMarkup xml;    
  17.       xml.Load("UserInfo.xml");    
  18.       BOOL bFind = true;    
  19.       xml.ResetMainPos();    
  20.          
  21.       while (xml.FindChildElem("UserID");)    
  22.       {    
  23.                     xml.IntoElem();    
  24.                     CString strTagName = _T("");    
  25.                     CString strData = _T("");    
  26.                     strTagName = xml.GetTagName();    
  27.                     strData = xml.GetData();    
  28.                     TRACE("/n---tagName:%s,Data:%s--/n",strTagName,strData);    
  29.                     xml.OutOfElem();    
  30.       }*/      
  31.       /**************这样也可以**********************    
  32.       CMarkup xml;    
  33.       xml.Load("UserInfo.xml");    
  34.       BOOL bFind = true;    
  35.       xml.ResetMainPos();    
  36.       xml.FindElem();  //UserInfo    
  37.       xml.IntoElem();    
  38.       while (xml.FindElem("UserID");)    
  39.       {    
  40.                     CString strTagName = _T("");    
  41.                     CString strData = _T("");    
  42.                     strTagName = xml.GetTagName();    
  43.                     strData = xml.GetData();    
  44.                     TRACE("/n---tagName:%s,Data:%s--/n",strTagName,strData);    
  45.       }    
  46.       */      
  47. //-------------结果----------      
  48. ---tagName:UserID,Data:luo--      
  49. ---tagName:UserID,Data:lin—     
//----查看所有UserID---- CMarkup xml; xml.Load("UserInfo.xml"); BOOL bFind = true; xml.ResetMainPos(); while (xml.FindChildElem("UserID");) { //此时接点还是父接点 CString strTagName = _T(""); CString strData = _T(""); strTagName = xml.GetChildTagName(); strData = xml.GetChildData(); TRACE("/n---tagName:%s,Data:%s--/n",strTagName,strData); } /********************这样也可以****************** CMarkup xml; xml.Load("UserInfo.xml"); BOOL bFind = true; xml.ResetMainPos(); while (xml.FindChildElem("UserID");) { xml.IntoElem(); CString strTagName = _T(""); CString strData = _T(""); strTagName = xml.GetTagName(); strData = xml.GetData(); TRACE("/n---tagName:%s,Data:%s--/n",strTagName,strData); xml.OutOfElem(); }*/ /**************这样也可以********************** CMarkup xml; xml.Load("UserInfo.xml"); BOOL bFind = true; xml.ResetMainPos(); xml.FindElem(); //UserInfo xml.IntoElem(); while (xml.FindElem("UserID");) { CString strTagName = _T(""); CString strData = _T(""); strTagName = xml.GetTagName(); strData = xml.GetData(); TRACE("/n---tagName:%s,Data:%s--/n",strTagName,strData); } */ //-------------结果---------- ---tagName:UserID,Data:luo-- ---tagName:UserID,Data:lin—

3. 修改

[c-sharp] view plain copy print ?
  1. //--------把UserID为”luo”改为”flypigluo”-----------      
  2. BOOL bLoadXml = false;      
  3. CMarkup xml;      
  4. bLoadXml = xml.Load("UserInfo.xml");      
  5.       if (bLoadXml)      
  6.       {      
  7.               CString strUserID = _T("");      
  8.               xml.ResetMainPos();      
  9.               xml.FindElem();      
  10.               xml.IntoElem();      
  11.               while (xml.FindElem("UserID"))      
  12.               {      
  13.                     strUserID = xml.GetData();      
  14.                     if (strUserID=="luo")      
  15.                     {      
  16.                             xml.SetData(CString("flypig")+strUserID);      
  17.                             xml.Save("UserInfo.xml");      
  18.                             break;      
  19.                     }      
  20.               }      
  21.       }     
//--------把UserID为”luo”改为”flypigluo”----------- BOOL bLoadXml = false; CMarkup xml; bLoadXml = xml.Load("UserInfo.xml"); if (bLoadXml) { CString strUserID = _T(""); xml.ResetMainPos(); xml.FindElem(); xml.IntoElem(); while (xml.FindElem("UserID")) { strUserID = xml.GetData(); if (strUserID=="luo") { xml.SetData(CString("flypig")+strUserID); xml.Save("UserInfo.xml"); break; } } }

 

4. 添加

4.1  添在最后面(使用的是AddElem)

[c-sharp] view plain copy print ?
  1. //加在最后面      
  2.       BOOL bLoadXml = false;      
  3.       CMarkup xml;      
  4.       bLoadXml = xml.Load("UserInfo.xml");      
  5.       if (bLoadXml)      
  6.       {      
  7.               xml.ResetMainPos();      
  8.               xml.FindElem();      
  9.               xml.IntoElem();      
  10.               xml.AddElem("UserID","luoluo");      
  11.               xml.OutOfElem();      
  12.               xml.Save("UserInfo.xml");      
  13.       }      
  14. //--------原来为------      
  15. <?xml version="1.0" encoding="UTF-8" ?>      
  16. <UserInfo>      
  17.   <UserID>luo</UserID>      
  18.   <UserID>lin</UserID>      
  19.   </UserInfo>      
  20. //---------添加后为-------------      
  21. <?xml version="1.0" encoding="UTF-8" ?>      
  22. <UserInfo>      
  23.   <UserID>luo</UserID>      
  24.   <UserID>lin</UserID>      
  25.   <UserID>luoluo</UserID>      
  26.   </UserInfo>     
//加在最后面 BOOL bLoadXml = false; CMarkup xml; bLoadXml = xml.Load("UserInfo.xml"); if (bLoadXml) { xml.ResetMainPos(); xml.FindElem(); xml.IntoElem(); xml.AddElem("UserID","luoluo"); xml.OutOfElem(); xml.Save("UserInfo.xml"); } //--------原来为------ <?xml version="1.0" encoding="UTF-8" ?> <UserInfo> <UserID>luo</UserID> <UserID>lin</UserID> </UserInfo> //---------添加后为------------- <?xml version="1.0" encoding="UTF-8" ?> <UserInfo> <UserID>luo</UserID> <UserID>lin</UserID> <UserID>luoluo</UserID> </UserInfo>

 

4.2  加在最前面(使用InsertElem)

[cpp] view plain copy print ?
  1. BOOL bLoadXml = false;      
  2. CMarkup xml;      
  3. bLoadXml = xml.Load("UserInfo.xml");      
  4. if (bLoadXml)      
  5. {      
  6.       xml.ResetMainPos();      
  7.       xml.FindElem();      
  8.       xml.IntoElem();      
  9.       xml.InsertElem("UserID","AddUserIDHead");      
  10.       xml.OutOfElem();      
  11.       xml.Save("UserInfo.xml");      
  12. }      
  13. //----原来为—      
  14. <?xml version="1.0" encoding="UTF-8" ?>      
  15. <UserInfo>      
  16.   <UserID>luo</UserID>      
  17.   <UserID>lin</UserID>      
  18.   </UserInfo>      
  19. //------修改后-----      
  20. <?xml version="1.0" encoding="UTF-8" ?>      
  21. <UserInfo>      
  22.   <UserID>AddUserIDHead</UserID>      
  23.   <UserID>luo</UserID>      
  24.   <UserID>lin</UserID>      
  25.   </UserInfo>     
BOOL bLoadXml = false; CMarkup xml; bLoadXml = xml.Load("UserInfo.xml"); if (bLoadXml) { xml.ResetMainPos(); xml.FindElem(); xml.IntoElem(); xml.InsertElem("UserID","AddUserIDHead"); xml.OutOfElem(); xml.Save("UserInfo.xml"); } //----原来为— <?xml version="1.0" encoding="UTF-8" ?> <UserInfo> <UserID>luo</UserID> <UserID>lin</UserID> </UserInfo> //------修改后----- <?xml version="1.0" encoding="UTF-8" ?> <UserInfo> <UserID>AddUserIDHead</UserID> <UserID>luo</UserID> <UserID>lin</UserID> </UserInfo>

 

5. 删除

[c-sharp] view plain copy print ?
  1. CMarkup xml;      
  2. xml.Load("UserInfo.xml");      
  3. BOOL bFind = true;      
  4. xml.ResetMainPos();           
  5. while (bFind)      
  6. {      
  7.       bFind = xml.FindChildElem("UserID");      
  8.       if (bFind)      
  9.       {      
  10.               //此时接点还是父接点      
  11.               CString strData = _T("");      
  12.               strData = xml.GetChildData();      
  13.               if (strData=="luo")      
  14.               {      
  15.                     xml.RemoveChildElem();      
  16.                     xml.Save("UserInfo.xml");      
  17.                     break;      
  18.               }      
  19.       }      
  20. }     
CMarkup xml; xml.Load("UserInfo.xml"); BOOL bFind = true; xml.ResetMainPos(); while (bFind) { bFind = xml.FindChildElem("UserID"); if (bFind) { //此时接点还是父接点 CString strData = _T(""); strData = xml.GetChildData(); if (strData=="luo") { xml.RemoveChildElem(); xml.Save("UserInfo.xml"); break; } } }

 

6. 查找

见第二点浏览。类似上面的例子里面没有属性这些东西,下面的一个例子是有属性的

[c-sharp] view plain copy print ?
  1. //UserInfo1.xml      
  2. <?xml version="1.0" encoding="UTF-8" ?>      
  3. <UserInfo>      
  4.   <User UserID="UserID00" UserPwd="UserPwd00" UserLevel="158" />      
  5.   <User UserID="UserID01" UserPwd="UserPwd01" UserLevel="162" />      
  6.   <User UserID="UserID02" UserPwd="UserPwd02" UserLevel="165" />      
  7.   <User UserID="UserID03" UserPwd="UserPwd03" UserLevel="168" />      
  8.   <User UserID="UserID04" UserPwd="UserPwd04" UserLevel="171" />      
  9.   <User UserID="UserID05" UserPwd="UserPwd05" UserLevel="175" />      
  10.   <User UserID="UserID06" UserPwd="UserPwd06" UserLevel="178" />      
  11.   <User UserID="UserID07" UserPwd="UserPwd07" UserLevel="181" />      
  12.   <User UserID="UserID08" UserPwd="UserPwd08" UserLevel="184" />      
  13.   <User UserID="UserID09" UserPwd="UserPwd09" UserLevel="188" />      
  14.   </UserInfo>     
//UserInfo1.xml <?xml version="1.0" encoding="UTF-8" ?> <UserInfo> <User UserID="UserID00" UserPwd="UserPwd00" UserLevel="158" /> <User UserID="UserID01" UserPwd="UserPwd01" UserLevel="162" /> <User UserID="UserID02" UserPwd="UserPwd02" UserLevel="165" /> <User UserID="UserID03" UserPwd="UserPwd03" UserLevel="168" /> <User UserID="UserID04" UserPwd="UserPwd04" UserLevel="171" /> <User UserID="UserID05" UserPwd="UserPwd05" UserLevel="175" /> <User UserID="UserID06" UserPwd="UserPwd06" UserLevel="178" /> <User UserID="UserID07" UserPwd="UserPwd07" UserLevel="181" /> <User UserID="UserID08" UserPwd="UserPwd08" UserLevel="184" /> <User UserID="UserID09" UserPwd="UserPwd09" UserLevel="188" /> </UserInfo>

 

7. 产生

[cpp] view plain copy print ?
  1. CString strID = _T("");      
  2.       CString strPwd = _T("");      
  3.       CMarkup xml;      
  4.       xml.SetDoc("<?xml version=/"1.0/" encoding=/"UTF-8/"?>");      
  5.       xml.AddElem("UserInfo");      
  6.       xml.IntoElem();      
  7.       for (int i=0; i<10; i++)      
  8.       {      
  9.               //产生一个随机数,作为权限      
  10.               srand(UINT(time(NULL)+i));      
  11.               int nLevel = rand()%256;      
  12.               strID.Format("UserID%02d",i);      
  13.               strPwd.Format("UserPwd%02d",i);      
  14.               xml.AddElem("User");      
  15.               xml.AddAttrib("UserID",strID);      
  16.               xml.AddAttrib("UserPwd",strPwd);      
  17.               xml.AddAttrib("UserLevel",nLevel);      
  18.       }      
  19.       xml.OutOfElem();      
  20.       xml.Save("UserInfo1.xml");     
CString strID = _T(""); CString strPwd = _T(""); CMarkup xml; xml.SetDoc("<?xml version=/"1.0/" encoding=/"UTF-8/"?>"); xml.AddElem("UserInfo"); xml.IntoElem(); for (int i=0; i<10; i++) { //产生一个随机数,作为权限 srand(UINT(time(NULL)+i)); int nLevel = rand()%256; strID.Format("UserID%02d",i); strPwd.Format("UserPwd%02d",i); xml.AddElem("User"); xml.AddAttrib("UserID",strID); xml.AddAttrib("UserPwd",strPwd); xml.AddAttrib("UserLevel",nLevel); } xml.OutOfElem(); xml.Save("UserInfo1.xml");

 

8. 查找

[c-sharp] view plain copy print ?
  1. CMarkup xml;      
  2. BOOL bLoad = false;      
  3. BOOL bFind = false;      
  4. bLoad = xml.Load("UserInfo1.xml");      
  5.       if (bLoad)      
  6.       {      
  7.               CString strID;      
  8.               CString strPwd;      
  9.               xml.ResetMainPos();      
  10.               xml.FindElem();    //UserInfo      
  11.               while (xml.FindChildElem("User"))      
  12.               {      
  13.                     strID = xml.GetChildAttrib("UserID");      
  14.                     strPwd = xml.GetChildAttrib("UserPwd");      
  15.                     TRACE("/n----id:%s,pwd:%s-------/n",strID,strPwd);      
  16.                     if (strID=="UserID00"&&strPwd=="UserPwd00")      
  17.                     {      
  18.                             bFind = true;      
  19.                             break;      
  20.                     }      
  21.               }      
  22.       }      
  23.       if (bFind)      
  24.       {      
  25.               TRACE("/n---- find------/n");      
  26.       }      
  27.       else      
  28.       {      
  29.               TRACE("/n----no find------/n");      
  30.       }     
CMarkup xml; BOOL bLoad = false; BOOL bFind = false; bLoad = xml.Load("UserInfo1.xml"); if (bLoad) { CString strID; CString strPwd; xml.ResetMainPos(); xml.FindElem(); //UserInfo while (xml.FindChildElem("User")) { strID = xml.GetChildAttrib("UserID"); strPwd = xml.GetChildAttrib("UserPwd"); TRACE("/n----id:%s,pwd:%s-------/n",strID,strPwd); if (strID=="UserID00"&&strPwd=="UserPwd00") { bFind = true; break; } } } if (bFind) { TRACE("/n---- find------/n"); } else { TRACE("/n----no find------/n"); }

 

9. 修改

[c-sharp] view plain copy print ?
  1. CMarkup xml;      
  2.       BOOL bLoad = false;      
  3.       BOOL bFind = false;      
  4.       bLoad = xml.Load("UserInfo1.xml");      
  5.       if (bLoad)      
  6.       {      
  7.               CString strID;      
  8.               CString strPwd;      
  9.               xml.ResetMainPos();      
  10.               xml.FindElem();    //UserInfo      
  11.               while (xml.FindChildElem("User"))      
  12.               {      
  13.                     strID = xml.GetChildAttrib("UserID");      
  14.                     strPwd = xml.GetChildAttrib("UserPwd");      
  15.                     TRACE("/n----id:%s,pwd:%s-------/n",strID,strPwd);      
  16.                     if (strID=="UserID00"&&strPwd=="UserPwd00")      
  17.                     {      
  18.                             bFind = true;      
  19.                             xml.SetChildAttrib("UserID",strID+CString("Modify"));      
  20.                             xml.Save("UserInfo1.xml");      
  21.                             break;      
  22.                     }      
  23.               }      
  24.       }      
  25.       if (bFind)      
  26.       {      
  27.               TRACE("/n---- find------/n");      
  28.       }      
  29.       else      
  30.       {      
  31.               TRACE("/n----no find------/n");      
  32.       }     
CMarkup xml; BOOL bLoad = false; BOOL bFind = false; bLoad = xml.Load("UserInfo1.xml"); if (bLoad) { CString strID; CString strPwd; xml.ResetMainPos(); xml.FindElem(); //UserInfo while (xml.FindChildElem("User")) { strID = xml.GetChildAttrib("UserID"); strPwd = xml.GetChildAttrib("UserPwd"); TRACE("/n----id:%s,pwd:%s-------/n",strID,strPwd); if (strID=="UserID00"&&strPwd=="UserPwd00") { bFind = true; xml.SetChildAttrib("UserID",strID+CString("Modify")); xml.Save("UserInfo1.xml"); break; } } } if (bFind) { TRACE("/n---- find------/n"); } else { TRACE("/n----no find------/n"); }

 

相关文件的下载请到置顶的资源首页。

 

你可能感兴趣的:(C#专题之XML应用技术)