【发布时间】:2022-01-16 07:46:55
【问题描述】:
我正在做一项关于处理结构化/半结构化/非结构化数据的作业,我正在通过导入每个戏剧的 txt 文件和一个 xml 来统计莎士比亚戏剧的字数(以了解语言如何随时间变化)索引文件,其中存储有关每个剧本的关键信息,例如编写年份、角色列表等。然后我将从 txt 中删除角色名称、设置、标点符号和常用词(and, but, or, if etc...)文件准备好进行字数统计 - 全部在 C# 中运行的控制台脚本中。我正在编写一个将存储每个播放数据的类,它目前看起来像这样:
class PlayImport
{
public string Title;
public DateTime Year;
public string location;
public string[] Cast;
public Counter[] WordCount;
public PlayImport(string location, int Num)
{
XmlDocument Reader = new XmlDocument();
Reader.Load(location);
this.Title = Convert.ToString(Reader.DocumentElement.ChildNodes[Num].Attributes["Title"].Value);
this.Year = Convert.ToDateTime(Reader.DocumentElement.ChildNodes[Num].Attributes["Year"].Value);
this.location = Convert.ToString(Reader.DocumentElement.ChildNodes[Num].Attributes["Location"].Value);
foreach (XmlNode xmlNode in Reader.DocumentElement.ChildNodes[Num].ChildNodes[0].ChildNodes)
this.Cast += Convert.ToString(xmlNode.Attributes["Name"].Value);
}
}
但是,最后一行 (Cast +=) 发出错误,无法将字符串转换为字符串 []。如何解决这个问题,以便将字符列表捆绑到 Cast 字符串数组中?
【问题讨论】:
-
我认为在这种情况下,
List<string>是比数组更好的选择
标签: c# arrays xml string type-conversion