C++コード管理方式でc#のhttpAgilityPackライブラリを呼び出す

1993 ワード

c#のhttpAgilityPackは優れたHTML解析ライブラリです.最も重要なことはXPathで解析できることである.残念ながら、C++は管理コードでしか呼び出せません.
管理コードの設定:
ALT+F 7プロパティページを開く-プロパティ構成-一般-共通言語実行時サポート-共通言語実行時サポート(clr)
#include<afxwin.h>
#include<string> 
using namespace std;
using namespace System;
#using <System.Xml.dll>
#using "..\Debug\HtmlAgilityPack.dll"
using namespace HtmlAgilityPack;
 

#include<list>
list<string> _links;

using namespace System::Runtime::InteropServices;  //Marshal
//System::String std::string
void MarshalString ( String^ s, string& os ) 
{   
   const char* chars = (const char*)(Marshal::StringToHGlobalAnsi(s)).ToPointer();
   os = chars;
   Marshal::FreeHGlobal(IntPtr((void*)chars));
}


inline void ParseLink(HtmlNode^ node, String^ name)
{
	HtmlAttribute ^att = node->Attributes[name];
	
	if(att == nullptr)
		return;
	String^ str = node->Name;
	if ((name == "href") && (node->Name != "link"))
		return;
									
	string strValue ;
	MarshalString(att->Value,strValue);			
	_links.push_back(strValue);
}


void CTESTDlg::OnBnClickedOk()
{
    string url =  "http://www.sina.com";;     
    String^ str2 = gcnew String(url.c_str());  

	HtmlWeb ^hw = gcnew HtmlWeb();		
 	HtmlDocument ^doc = hw->Load(str2);
	doc->Save("mshome.htm");
	
	HtmlNodeCollection ^atts = doc->DocumentNode->SelectNodes("//*[@background or @lowsrc or @src or @href]");
		
	for(int i = 0;i < atts->Count;i ++)
	{
		ParseLink(atts[i], "background");
		ParseLink(atts[i], "href");
		ParseLink(atts[i], "src");
		ParseLink(atts[i], "lowsrc");
	}

	string strurl;
	list<string>::iterator v;
	for(v = _links.begin();v != _links.end();++v)
	{
		strurl += (*v);
		strurl += "  ";
	}
	

	m_edit.SetWindowTextA(strurl.c_str());
	CString str = ":";

	int a =1;
}

ソースアドレスのテスト
http://download.csdn.net/detail/witch_soya/4978532