我有一种解析XML
的方法:
public static List<Profile> Parse XML(string Document)
{
List<Profile> Result = new List<Profile>();
doc = XDocument.Load(Document);
Resoults = (from n in doc.Descendants("level")
select new Profile()
{
CurrentID = int.Parse(n.Attribute("CurrentID").Value),
Location = (from l in n.Element("ID").Elements("ID")
select new Location()
{
id = (int)(l.Attribute("id")),
x = (Single)l.Attribute("x"),
y = (Single)l.Attribute("y"),
z = (Single)l.Attribute("z")
}).ToList(),
Bank = (from l in doc.Descendants("Banker")
select new Banker()
{
BankID = (int)(l.Attribute("id")),
BankX = (Single)(l.Attribute("x")),
BankY = (Single)(l.Attribute("y")),
BankZ = (Single)(l.Attribute("z"))
}).ToList(),
Vendor = (from l in doc.Descendants("Vendor")
select new Vendor()
{
VendorID = (int)(l.Attribute("id")),
VendorX = (Single)(l.Attribute("x")),
VendorY = (Single)(l.Attribute("y")),
VendorZ = (Single)(l.Attribute("z"))
}).ToList()
}).ToList();
var ProperID = Resoults.Where(s => s.CurrentID <= 10).Aggregate((c, d) => c.CurrentID > d.CurrentID ? c : d);
return ProperID; //error: Here i want to return list ProperID
}
我想解析XML文件,然后从特定CurrentID
的解析列表中获取节点。
我想返回ProperID
列表但编译器errores
以及:
无法隐式将
'Classes.XMLprofile.Profile'
类型转换为'System.Collections.Generic.List<Classes.XMLprofile.Profile>'
答案 0 :(得分:5)
您想要在CurrentId中返回具有正确ID的结果, 在代码中你得到了编译错误,因为返回值是一个Profile对象,而方法签名是一个Profile对象的List,所以:
return Resoults.Where(p=>p.CurrentID ==ProperID.CurrentID).ToList();