0
我这个XML转换:使用转换XML字典
<root>
<item id="1" level="1" />
<item id="2" level="1">
<item id="3" level="2" />
<item id="4" level="2" >
<item id="5" level="3">
<item id="6" level="4" />
</item>
</item>
<item id="7" level=2" />
</item>
</root>
成字典这样的:
XElement root = XElement.Parse(strSerializedoutput);
Dictionary<int, Pair> list = root.Descendants("item").ToDictionary(x => (int)x.Attribute("id"), x =>
{
var pId = x.Parent.Attribute("id");
var depthLevel = x.Attribute("level");
if (pId == null)
{
return new { parentID = 0, level = (int)depthLevel };
}
else
{
return new { parentID = (int)pId, level = (int)depthLevel };
}
});
,其中对为:
public class Pair
{
int parentID;
int level;
}
输出我想:
ID | ParentID | level
------------------------
1 NULL 1
2 NULL 1
3 2 2
4 2 2
5 4 3
6 5 4
7 2 2
,但我得到一个错误说
错误35无法隐式转换 型 'System.Collections.Generic.Dictionary INT,AnonymousType#1' 到“System.Collections.Generic.Dictionary INT,ProposalSystem.handlers。 main.Pair”
当我使用新的对,我得到以下errors.Error 无法转换lambda表达式键入“System.Collections.Generic.IEqualityComparer”,因为它不是一个委托类型,也 错误'ProposalSystem.handlers.main.Pair.parentID'由于其保护级别而无法访问 –
@PrakashChennupati您的'Pair'应该有'public' memebers定义 –
啊谢谢!得到它了! –