xml をオブジェクトの階層に解析しようとしていますが、階層を適切に再帰する方法がわかりません。XElement を解析してコレクションを返す GetChild の呼び出しである大まかな解決策 (良い解決策ではありません) があります。誰かが純粋なlinq式でこれを達成する方法を知っていることを願っています。GetChild() などの関数をインラインで呼び出すことなく、親子項目の関係をリストに入力する
ありがとう
var element = XElement.Parse(@"<Root RegisterVersion='1.0' xmlns='http://www.test.com.au/docs/schemas' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://www.test.com/docs/schemas/spin/surcharge http://www.test.com/docs/schemas/test.xsd'>
<Parent id='1' name='parent1'>
<Child id='1' name='child1'>
<Item id='1' name='someitem'></Item>
</Child>
</Parent>
<Parent id='2' name='parent2'>
<Child id='2' name='child2'>
<Item id='2' name='someotheritem'></Item>
</Child>
</Parent>
</Root>
");
XNamespace ns = element.Name.Namespace;
var list =
from compileItem in element.Elements (ns + "Parent")
select new Parent
{
Id = compileItem.Attribute("id").Value.ToString(),
Name = compileItem.Attribute("name").Value.ToString(),
children = GetChild(compileItem)
// this call here I'd like to replace with another linq select
};
public List<Child> GetChild(XElement frag)
{
//etc
}
public List<Item> GetItem(XElement frag)
{
//etc
}