Я преобразования этого 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 междунар, ProposalSystem.handlers. main.Pair»
когда я использую новую пару, я получаю следующее errors.Error \t \t 35 Не удается преобразовать лямбда-выражения к типу «System.Collections.Generic.IEqualityComparer», потому что это не тип делегата, а также Ошибка 'ProposalSystem.handlers.main.Pair.parentID' недоступен из-за своей защитной полосы –
@PrakashChennupati ваша 'Pair' должна иметь' public' memebers –
ah спасибо! понял! –