0

I'm trying to select some childelements from my object XElement and put them in a new XElement.

 var objNodes =
     doc.Descendants("object")
         .Where(node => node.Attribute("table").Value == idbsObject.Key)
         .Select(item => new XElement(item.Element("object"), new XElement(item.Element("nodes"))));

With this part

var objNodes =
         doc.Descendants("object")
             .Where(node => node.Attribute("table").Value == idbsObject.Key)

I'm able to get the object element looking like this

 <object occur="-1" prefix="" table="Articles" description="" singlename="" name="" statemode="disabled" link="" exportable="0">
  <views>
    <view id="1" type="" name="">
      <sql>SELECT ObjectID FROM WHERE</sql>
      <columns>
        <column description="" sortnode="" width="" />
      </columns>
      <values />
    </view>
  </views>
  <objectviews>
    <view id="view1" type="view" name="Bekijken" show="1" link="" />
    <view id="view2" type="edit" name="Bewerken" show="1" link="" />
    <view id="view3" type="delete" name="Verwijderen" show="1" link="" />
    <view id="view4" type="add" name="Toevoegen" show="1" link="" />
  </objectviews>
  <nodes>
    <node id="ArticleId" description="" required="" datatype="i" nodetype="" fieldlength="" reference="" exclude="" tab="" order="0" default="" decimals="" maxchars="" help="" unique="" show="" />
    <node id="Title" description="" required="" datatype="s" nodetype="" fieldlength="" reference="" exclude="" tab="" order="10" default="" decimals="" maxchars="" help="" unique="" show="" />
  </nodes>
  <tabs>
    <tab id="" order="0" type="normal" child="" foreignkey="" description="Algemeen" link="" />
  </tabs>
</object>

But how can I get the object and nodes element? I'm able to get the nodes but I can't manage to combine the object and nodes element into one. For the object element I don't want all the child elements.

Expected result

 <object occur="-1" prefix="" table="Articles" description="" singlename="" name="" statemode="disabled" link="" exportable="0">
  <nodes>
    <node id="ArticleId" description="" required="" datatype="i" nodetype="" fieldlength="" reference="" exclude="" tab="" order="0" default="" decimals="" maxchars="" help="" unique="" show="" />
    <node id="Title" description="" required="" datatype="s" nodetype="" fieldlength="" reference="" exclude="" tab="" order="10" default="" decimals="" maxchars="" help="" unique="" show="" />
  </nodes>
</object>

When I try to get the object element like this

var objNodes = doc.Descendants("object")
                   .Where(node => node.Attribute("table").Value == idbsObject.Key)
                   .Select(item => new XElement(item.Element("object")));

objNodes is null

user990423
  • 1,397
  • 2
  • 12
  • 32
Sybren
  • 1,071
  • 3
  • 17
  • 51
  • I don't have VS installed but answer would look like this : var objNodes = doc.Descendants("object") .Where(node => node.Attribute("table").Value == idbsObject.Key) .Select(item => new XElement("object", new object[] {item.Attributes(), item.Element("nodes")})); – jdweng Oct 12 '15 at 14:40
  • @jdweng your code seems to do the job it gives me the result I need. If u answer my question I can mark your answer as accepted :) – Sybren Oct 12 '15 at 14:46

1 Answers1

1

Comments can't be marked as accepted. I knew it would work because I answered similar question a few weeks ago. In the original answer made slight mistake used XElement[] and got an error because I was combining XAttributes and XElement[]. Notice in my solution is used "object" which is the tag name.

var objNodes = doc.Descendants("object") .Where(node => node.Attribute("table").Value == idbsObject.Key) .Select(item => new XElement("object", new object[] {item.Attributes(), item.Element("nodes")})); 
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • I know comments can't be marked as accepted, that's why I asked you to answer the question. Anyway thanks for your answer. – Sybren Oct 12 '15 at 15:23