在代碼中,我需要解析一個XML並獲得一種ContactData。 我的目標是解析代碼中顯示的簡單聯繫人列表,但不指定結構數據,如註釋代碼。Xml,Linq到類
,如果我嘗試如果我使用 只有下面的代碼使用註釋代碼我得到一個例外是不會發生:
XDocument xmlDocument = XDocument.Parse(data);
var result = from entry in xmlDocument.Descendants("contact")
select new ContactData
{
//Data = (Dictionary<string,object>)(from element in entry.Elements() select new Dictionary<string, object>().ToDictionary(o => o.Key, o => o.Value)),
Data = new Dictionary<string, object>
{
{"uid", entry.Element("uid").Value},
{"name", entry.Element("name").Value},
{"email", entry.Element("email").Value},
{"message", entry.Element("message").Value},
{"state", entry.Element("state").Value}
},
State = (States)Enum.Parse(typeof(States), entry.Element("state").Value)
};
return result.ToArray<ContactData>();
如何糾正呢?
Data = (Dictionary<string,object>)(from element in entry.Elements() select new Dictionary<string, object>().ToDictionary(o => o.Key, o => o.Value))
請發表您的例外。 – Robaticus 2011-03-18 17:14:23