用文件流的形式读写xml文件到MFC的树控件中

这是在工作时用到的问题,,,,主要就是通过读取xml中的一部分到树控件中,但是保存的时候必须是完整的读取时的xml。

1,就是读取xml文件,成为文件流。

定义一个字符串,其大小就是你读取文件的大小。

CFile  fp;
	  if (!(fp.Open(fileName,CFile::modeRead)))
	  {
		  return;
	  }
	  fp.SeekToEnd();//移动到文件尾
	  fpLength=fp.GetLength();
	  m_buffer=new char[fpLength];
	  fp.SeekToBegin();//移动到文件头
	  if(fp.Read(m_buffer,fpLength)<1){
		  fp.Close();
		  return;
	  }

然后就做一些你想要的操作,,比如增删改插啦,,,等等等等

2,取到特定的xml字符串

我用的是这个方法,,,

用文件流的形式读写xml文件到MFC的树控件中_第1张图片

3,,把特定的字符串绑定到树控件中

TiXmlDocument *doc = new TiXmlDocument();
	   doc->Parse(m_SoftwaresString,0,TIXML_ENCODING_UTF8);
	   TiXmlElement *pElement=doc->RootElement();
      CrcXml(pElement,TVI_ROOT);
	  delete doc;

其中CrcXml是把节点循环放入树控件中的函数

而Parse这个函数是把文件流放入到TIXMLDOCUMENT中

4,保存文件流到XML文件中

outFile.Open(outFileName,CFile::modeWrite);
			   outFile.SeekToBegin();
			   outFile.Write(head,length_head);
			   outFile.Write((LPCTSTR)UTF8Str,UTF8Str.GetLength());
			   outFile.Write(foot,length_foot);
			   outFile.Close();

这个就是啦,,,哈哈,,其中UTF8Str就是树控件中的节点,,,

utf8str是这样得来的

TiXmlPrinter printer;
	doc->Accept(&printer);

	const CStringW UnicodeStr(printer.CStr());
	const CStringA UTF8Str=CW2A(UnicodeStr,CP_UTF8);


你可能感兴趣的:(xml)