1. #include <iostream> 
  2. #include "tinyxml.h" 
  3. #include "tinystr.h" 
  4. #include <string> 
  5. #include <windows.h> 
  6. #include <atlstr.h> 
  7. using namespace std; 
  8.  
  9. CString GetAppPath() 
  10. {//获取应用程序根目录 
  11.     TCHAR modulePath[MAX_PATH]; 
  12.     GetModuleFileName(NULL, modulePath, MAX_PATH); 
  13.     CString strModulePath(modulePath); 
  14.     strModulePath = strModulePath.Left(strModulePath.ReverseFind(_T('\\'))); 
  15.     return strModulePath; 
  16.  
  17. bool CreateXmlFile(string& szFileName) 
  18. {//创建xml文件,szFilePath为文件保存的路径,若创建成功返回true,否则false 
  19.     try 
  20.     { 
  21.         //创建一个XML的文档对象。 
  22.         TiXmlDocument *myDocument = new TiXmlDocument(); 
  23.         //创建一个根元素并连接。 
  24.         TiXmlElement *RootElement = new TiXmlElement("Persons"); 
  25.         myDocument->LinkEndChild(RootElement); 
  26.         //创建一个Person元素并连接。 
  27.         TiXmlElement *PersonElement = new TiXmlElement("Person"); 
  28.         RootElement->LinkEndChild(PersonElement); 
  29.         //设置Person元素的属性。 
  30.         PersonElement->SetAttribute("ID""1"); 
  31.         //创建name元素、age元素并连接。 
  32.         TiXmlElement *NameElement = new TiXmlElement("name"); 
  33.         TiXmlElement *AgeElement = new TiXmlElement("age"); 
  34.         PersonElement->LinkEndChild(NameElement); 
  35.         PersonElement->LinkEndChild(AgeElement); 
  36.         //设置name元素和age元素的内容并连接。 
  37.         TiXmlText *NameContent = new TiXmlText("周星星"); 
  38.         TiXmlText *AgeContent = new TiXmlText("22"); 
  39.         NameElement->LinkEndChild(NameContent); 
  40.         AgeElement->LinkEndChild(AgeContent); 
  41.         CString appPath = GetAppPath(); 
  42.         string seperator = "\\"
  43.         string fullPath = appPath.GetBuffer(0) +seperator+szFileName; 
  44.         myDocument->SaveFile(fullPath.c_str());//保存到文件 
  45.     } 
  46.     catch (string& e) 
  47.     { 
  48.         return false
  49.     } 
  50.     return true
  51.  
  52. bool ReadXmlFile(string& szFileName) 
  53. {//读取Xml文件,并遍历 
  54.     try 
  55.     { 
  56.         CString appPath = GetAppPath(); 
  57.         string seperator = "\\"
  58.         string fullPath = appPath.GetBuffer(0) +seperator+szFileName; 
  59.         //创建一个XML的文档对象。 
  60.         TiXmlDocument *myDocument = new TiXmlDocument(fullPath.c_str()); 
  61.         myDocument->LoadFile(); 
  62.         //获得根元素,即Persons。 
  63.         TiXmlElement *RootElement = myDocument->RootElement(); 
  64.         //输出根元素名称,即输出Persons。 
  65.         cout << RootElement->Value() << endl; 
  66.         //获得第一个Person节点。 
  67.         TiXmlElement *FirstPerson = RootElement->FirstChildElement(); 
  68.         //获得第一个Person的name节点和age节点和ID属性。 
  69.         TiXmlElement *NameElement = FirstPerson->FirstChildElement(); 
  70.         TiXmlElement *AgeElement = NameElement->NextSiblingElement(); 
  71.         TiXmlAttribute *IDAttribute = FirstPerson->FirstAttribute(); 
  72.         //输出第一个Person的name内容,即周星星;age内容,即;ID属性,即。 
  73.         cout << NameElement->FirstChild()->Value() << endl; 
  74.         cout << AgeElement->FirstChild()->Value() << endl; 
  75.         cout << IDAttribute->Value()<< endl; 
  76.     } 
  77.     catch (string& e) 
  78.     { 
  79.         return false
  80.     } 
  81.     return true
  82. int main() 
  83.     string fileName = "info.xml"
  84.     CreateXmlFile(fileName); 
  85.     ReadXmlFile(fileName);