EDN Admin
Well-known member
Hi there,<br/> i have a question about linq to xml.<br/> <br/> i have a xml-document like this (called test.xml):<br/>
<pre lang=x-xml> <root>
<child id="1" attr="
<subchild id="1" subid="1" attr=""/>
<subchild id="1" subid="2" attr=""/>
<subchild id="1" subid="3" attr=""/>
<subchild id="1" subid="4" attr=""/>
</child>
<child id="2" attr="
<subchild id="2" subid="1" attr=""/>
</child>
<child id="3" attr="
<subchild id="3" subid="1" attr=""/>
<subchild id="3" subid="2" attr=""/>
</child>
</root>[/code]
but,<br/> in runtime the levels child and subchild are flatten in two Lists, like this:<br/> <br/>
<pre lang="x-c# List<XElement> childs = new List<XElement>()
{
{XElement.Parse("<child id=1 attr=/>")},
{XElement.Parse("<child id=2 attr=/>")},
{XElement.Parse("<child id=3 attr=/>")}
};
List<XElement> subchilds = new List<XElement>()
{
{XElement.Parse("<subchild id=1 subid=1 attr=/>")},
{XElement.Parse("<subchild id=1 subid=2 attr=/>")},
{XElement.Parse("<subchild id=1 subid=3 attr=/>")},
{XElement.Parse("<subchild id=1 subid=4 attr=/>")},
{XElement.Parse("<subchild id=2 subid=1 attr=/>")},
{XElement.Parse("<subchild id=3 subid=1 attr=/>")},
{XElement.Parse("<subchild id=3 subid=2 attr=/>")}
};[/code]
So know i want a linq-query (using the attribute id for joining) to convert the lists into an xml-file (like the test.xml).<br/> what i have is this:<br/> <br/>
<pre lang="x-c# var test_xml = from c in childs
from sc in subchilds
where c.Attribute("id").Value == sc.Attribute("id").Value
select new XDocument(new XElement(c.Name + c.Attribute("id").Value, new XElement(sc.Name + sc.Attribute("id").Value)));[/code]
<br/> What i got is this (the joining seems to be working):<br/> <br/>
<pre lang=x-xml><child1>
<subchild1 />
</child1>
<child1>
<subchild1 />
</child1>
<child1>
<subchild1 />
</child1>
<child1>
<subchild1 />
</child1>
<child2>
<subchild2 />
</child2>
<child3>
<subchild3 />
</child3>
<child3>
<subchild3 />
</child3>[/code]
<br/> what i (may be) understand:<br/> the constructor of XDocument and XElement just allow to insert an element-name (type:string) and a sub-element (type:xelement).<br/> when i want to do it like this i need the constructors to allow interting an element (type:xelement) and a sub-element-list (type:[xelement]).<br/> <br/> <br/> now, the question:<br/> how can i convert my two lists in the xml-file by using a declarative way?????<br/> (i donts want walk throught the child-list using xpath and the add-method)<br/> <br/> <br/> <br/> thanx in advance,<br/> jobacca
View the full article
<pre lang=x-xml> <root>
<child id="1" attr="
<subchild id="1" subid="1" attr=""/>
<subchild id="1" subid="2" attr=""/>
<subchild id="1" subid="3" attr=""/>
<subchild id="1" subid="4" attr=""/>
</child>
<child id="2" attr="
<subchild id="2" subid="1" attr=""/>
</child>
<child id="3" attr="
<subchild id="3" subid="1" attr=""/>
<subchild id="3" subid="2" attr=""/>
</child>
</root>[/code]
but,<br/> in runtime the levels child and subchild are flatten in two Lists, like this:<br/> <br/>
<pre lang="x-c# List<XElement> childs = new List<XElement>()
{
{XElement.Parse("<child id=1 attr=/>")},
{XElement.Parse("<child id=2 attr=/>")},
{XElement.Parse("<child id=3 attr=/>")}
};
List<XElement> subchilds = new List<XElement>()
{
{XElement.Parse("<subchild id=1 subid=1 attr=/>")},
{XElement.Parse("<subchild id=1 subid=2 attr=/>")},
{XElement.Parse("<subchild id=1 subid=3 attr=/>")},
{XElement.Parse("<subchild id=1 subid=4 attr=/>")},
{XElement.Parse("<subchild id=2 subid=1 attr=/>")},
{XElement.Parse("<subchild id=3 subid=1 attr=/>")},
{XElement.Parse("<subchild id=3 subid=2 attr=/>")}
};[/code]
So know i want a linq-query (using the attribute id for joining) to convert the lists into an xml-file (like the test.xml).<br/> what i have is this:<br/> <br/>
<pre lang="x-c# var test_xml = from c in childs
from sc in subchilds
where c.Attribute("id").Value == sc.Attribute("id").Value
select new XDocument(new XElement(c.Name + c.Attribute("id").Value, new XElement(sc.Name + sc.Attribute("id").Value)));[/code]
<br/> What i got is this (the joining seems to be working):<br/> <br/>
<pre lang=x-xml><child1>
<subchild1 />
</child1>
<child1>
<subchild1 />
</child1>
<child1>
<subchild1 />
</child1>
<child1>
<subchild1 />
</child1>
<child2>
<subchild2 />
</child2>
<child3>
<subchild3 />
</child3>
<child3>
<subchild3 />
</child3>[/code]
<br/> what i (may be) understand:<br/> the constructor of XDocument and XElement just allow to insert an element-name (type:string) and a sub-element (type:xelement).<br/> when i want to do it like this i need the constructors to allow interting an element (type:xelement) and a sub-element-list (type:[xelement]).<br/> <br/> <br/> now, the question:<br/> how can i convert my two lists in the xml-file by using a declarative way?????<br/> (i donts want walk throught the child-list using xpath and the add-method)<br/> <br/> <br/> <br/> thanx in advance,<br/> jobacca
View the full article