1

我一直在研究文本分类系统,并试图读取文本文档中的单词,对其进行解析,将其保存到字典中,然后将其保存到 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
        }

}

那么,我应该如何做 XMLSerializer 呢?

PS对不起,如果代码是一团糟且未优化。我对这一切(文本解析、字典、XML)都很陌生,到目前为止,我只是想让它工作。

4

1 回答 1

1

是的,您应该将 Dictionary 序列化为 XML 文件,这里有一个很好的实现:

http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx

FW 4+ 的更新(来自 Hearty 的评论):

为了适用于最新的框架,必须有一个根元素。

 In the WriteXML add a line:
 +49 writer.WriteStartElement("dictionary" ); 
 +70 writer.WriteEndElement( ); 

 In the ReadXML add lines 
 +27 reader.ReadStartElement( "dictionary" ); 
 +45 reader.ReadEndElement( )

;

于 2012-05-17T08:27:31.747 回答