我有一个Dictionary<string, Tuple<string, List<string>>> email,我想在其中写入XML (序列化),并从XML加载到字典。
我对XML的编写是这样的:
public void WritetoXML(string path) {
var xElem = new XElement(
"emailAlerts",
email.Select(x => new XElement("email", new XAttribute("h1", x.Key),
new XAttribute("body", x.Value.Item1),
new XAttribute("ids", string.Join(",", x.Value.Item2))))
);
xElem.Save(path);
}但是我使用的是LoadXML,它获取XML路径并将其加载到Email类中的字典中
这就是我到目前为止所知道的:
public void LoadXML(string path) {
var xElem2 = XElement.Parse(path);
var demail = xElem2.Descendants("email").ToDictionary(x => (string)x.Attribute("h1"),
(x => (string)x.Attribute("body"),
x => (string)x.Attribute("body")));
}背景信息我的XML应该是这样的
<emailAlerts>
<email h1='Test1' body='This is a test' ids='1,2,3,4,5,10,11,15'/>
</emailAlerts>发布于 2015-06-20 08:53:58
您可以尝试这样做:
public void LoadXML(string path)
{
var xElem2 = XElement.Load(path);
var demail = xElem2.Descendants("email")
.ToDictionary(
x => (string)x.Attribute("h1")
, x => Tuple.Create(
(string)x.Attribute("body")
, x.Attribute("ids").Value
.Split(',')
.ToList()
)
);
}path参数包含XML文件的路径,则应该使用XElement.Load(path)而不是XElement.Parse(path)。Tuple.Create()构造Tuple实例通常比new Tuple() style短
https://stackoverflow.com/questions/30946581
复制相似问题