I’ve been working on a text classification system and am trying to read words in text documents, parse it, save it to dictionary, then save it to an XML.
fileNames = openFileDialog1->FileNames;
StreamReader^ objReader = gcnew StreamReader(fileNames[0]);
String^ strReader = objReader->ReadToEnd(); //read it to a string
objReader->Close();
//cut the file up
String^ delimStr = L"\r,\t,\n,' ','.',','"; //pull out the return,tabs,newline
array<Char>^ delimiter = delimStr->ToCharArray();//transform into an array
array<String^ > ^ strSplit = strReader->Split(delimiter);//split up the file
System::Collections::IEnumerator^ myenum = strSplit->GetEnumerator();//get enumerators
Dictionary<String^, int>^ saveWords = gcnew Dictionary<String^, int>(StringComparer::InvariantCultureIgnoreCase);
int position=-1;
//output the file
while (myenum->MoveNext())//enumerate through it
{
position++;
try{
saveWords->Add((Convert::ToString(myenum->Current)),0);
}
catch (Exception ^){
//some code
}
}
So, how should I do the XMLSerializer?
P.S. Sorry if the code is a mess and un-optimized. I am new to all this (text parsing, dictionary, XML), and so far I’m just trying to make it work.
Yes, you should serialize Dictionary to XML file, there is a nice implemenatation here :
http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx
Update for FW 4+ (comment from Hearty):
In order to work for the latest framework it is necessary to have a root element.
;