我收到编译器错误:“System.Collections.Generic.List”到“System.Xml.Linq.XName”。
我最初得到的“XAttribute”不包含“Trim”的定义,也没有可访问的扩展方法“Trim”……等等。但我想我发现我的引语放错了地方。
我究竟做错了什么?
public static List<Phrase> LoadPhrasesFromXMLFile(string file)
{
try
{
XDocument xdocument = XDocument.Load(file);
char[] trim = new char[3] { '\'', '"', ' ' };
return xdocument.Descendants("Phrase").Select((Func<XElement, Phrase>)(x => new Phrase()
{
eventname = (string)x.Attribute("Event".Trim(trim)),
priority = int.Parse((string)x.Attribute("Priority".Trim(trim))),
words = x.Descendants("Word").Select((Func<XElement, Word>)(y =>
{
Word word1 = new Word
{
preferred_text = (string)y.Attribute("Primaries".Trim(trim).ToLower())
};
List<string> stringList = (string)y.Attribute("Secondaries") == null || string.IsNullOrWhiteSpace((string)y.Attribute("Secondaries"))
? new List<string>()
在这一行失败:
: (List<string>)(IEnumerable<string>)(string)y.Attribute("Secondaries".Trim(trim).Replace(" ", "").ToLower().Split(',').ToList());
续代码:
Word word2 = word1;
word2.Ssecondaries = stringList;
return word1;
})).ToList<Word>()
})).ToList<Phrase>();
}
错误捕捉:
catch (Exception ex)
{
Sup.Logger("Encountered an exception reading '" + file + "'. It was: " + ex.ToString(), false, true);
}
return (List<Phrase>)null;
}