0

I have this xml structure:

<section id="Test1">
  <pkg:transition id="Test">
    <h>CADTeam<translation lang="en">CADTeam</translation></h>
    <abstract>CADTeam<translation lang="en">CADTeam</translation></abstract>
    <import name="man/dummy.txt" />
  </pkg:transition>
  <pkg:transition id="CommTeam">
    <h>CommTeam<translation lang="en">CommTeam</translation></h>
    <abstract>CommTeam<translation lang="en">CommTeam</translation></abstract>
    <import name="man/dummy.txt" />
  </pkg:transition>
  <pkg:transition id="KeyUser">
    <h>KeyUser<translation lang="en">KeyUser</translation></h>
    <abstract>KeyUser<translation lang="en">KeyUser</translation></abstract>
    <import name="man/dummy.txt" />
  </pkg:transition>
  <pkg:stelle id="Error">
    <h>Error<translation lang="en">Error</translation></h>
    <abstract>Error<translation lang="en">Error</translation></abstract>
    <import name="man/dummy.txt" />
  </pkg:stelle>
</section>

I have several sections but I want to copy a specific section. We can use this for example. I want to copy this whole section with all the elements and attributes and paste it to another block in the xml file. My question is how can I copy this hole section into a variable?

I tried this method but it didn't work:

var section = from sections in xdocument.Descendants("section")
              where sections.Attribute("id").Value == selecteditem
              select sections.Descendants();

Thank You.

loris02
  • 39
  • 5
  • "It didn't work" doesn't make it easy to help. Let's look at what you're doing. `xdocument.Descendants("section")` represents a collection (well, an enumerable, actually) of all elements in the document with the name "section". So far, so good. `from sections in` means, you want to do something for each item of that collection. And "sections" is the name of the variable, each item will be put into one after the other. So maybe calling it just "section" would make more sense. `where [...]` filters out all those sections where the id doesn't match. – Corak May 12 '20 at 14:24
  • `select sections.Descendants()` would then try to select all subelements of the found section(s), but there could be more than one secion found, so you'd need another `from` at the beginning, but you don't even want that, right? You want the whole section, and not just its descendants (the `"pkg:[...]"` elements). So you probably just want to `select sections;` (again, "section" would be more clear). Then you'd have all matching secions _selected_. After that, you can decide what to do with them. – Corak May 12 '20 at 14:24
  • Yes, exactly. I want to select the whole the section not just its descendants. – loris02 May 13 '20 at 06:23
  • Btw. I'm not a big fan of the query syntax, I prefer the method syntax, so I'd probably do something like: `var selectedSections = xdocument.Descendants("section").Where(section => section.Attribute("id").Value == selecteditem);` – Corak May 13 '20 at 06:30
  • The result is now only "
    ". As you know I want to have it with the corresponding elements and attributes.
    – loris02 May 13 '20 at 06:49
  • 1
    The "result" (i.e. `selectedSecions` of my last comment) should be an `IEnumerable`, so you're obviously doing some more things you're not showing. What would be your result of `xdocument.Descendants("section").First(section => section.Attribute("id").Value == selecteditem).ToString();`? For me, using your example, that shows the entire "section" element with all sub-elements. -- And since you're dealing with XML namespaces (the `pkg:` thing), you might want to read https://stackoverflow.com/questions/16018434/query-xdocument-with-xmlns-attribute-namespace – Corak May 13 '20 at 07:21

0 Answers0