當前位置:成語大全網 - 新華字典 - c#中 xml可以序列化到壹個List,能不能序列化到壹個Dictionary?

c#中 xml可以序列化到壹個List,能不能序列化到壹個Dictionary?

妳的xml文件中有中文,所以<att><![CDATA[妳好]]></att>

妳貼出的代碼對於將類序列化成xml文件,是非常好用的。但是妳的問題好像是讀取xml文件,並且轉換成Dictionary(不知道我的理解對不對)

如果我針對第2點的理解是對的,那麽建議妳采用linq to xml實現

using?System.Xml.Linq;

XElement?element?=?XElement.Load(@"d:\test.xml");

var?query?=?(from?c?in?element.Descendants("item")

select?new?{?Id?=?c.Element("id").Value,?Att?=?c.Element("att").Value?})

.ToDictionary(e?=>?e.Id);

//兩種寫法都可以

//var?query?=?element.Descendants("item").ToDictionary(c?=>?c.Element("id").Value);

foreach?(string?key?in?query.Keys)

{

Console.WriteLine("key:{0},Att:{1}",?key,?query[key].Att);

}

4.如果我的第二點理解錯誤,那麽就更簡單了。妳已經得到了List集合,轉換成Dictionary只需要list.ToDictionary(c=>c.Key)