tags:

views:

57

answers:

2

here is my structure of xml document,

<worldpatentdata>
    <patent-family>
        <exchange-documents>......</exchange-documents>
        <exchange-documents>......</exchange-documents>
        <exchange-documents>......</exchange-documents>
        <familymember>.....</familymember>
        <exchange-documents>......</exchange-documents>
        <exchange-documents>......</exchange-documents>
        <familymember>.....</familymember>
        <exchange-documents>......</exchange-documents>
        <exchange-documents>......</exchange-documents>
        <familymember>.....</familymember>
    </patent-family>
</worldpatentdata>

In the above xml structure,all the <exchange-documents> element is found to be child node of <patent-family> element. I want these <exchange-documents> elements as childnode of <family-member> element,so that the xml have the following structure,

<worldpatentdata>
    <patent-family>
        <familymember>.....
            <exchange-documents>......</exchange-documents>
            <exchange-documents>......</exchange-documents>
            <exchange-documents>......</exchange-documents>
        </familymember>
        <familymember>.....
            <exchange-documents>......</exchange-documents>
            <exchange-documents>......</exchange-documents>
        </familymember>
        <familymember>.....
            <exchange-documents>......</exchange-documents>
            <exchange-documents>......</exchange-documents>
        </familymember>
    </patent-family>
</worldpatentdata>

can anybody help me on this? thanks

A: 

Iterate over the children of <patent-family> via the DOM, remembering every <exchange-documents> you encounter along the way. Whenever you see a <familymember>, move the remembered elements under it.

Marcelo Cantos
A: 

Above the top of my head, one way could be to:

  1. Use XDocument to load the xml, and create a tempXDocument.
  2. foreach (XElement... through the elements and add to XmlNodeList items which are <exchange-documents>
  3. Add <familymember> in tempXDocument when <familymember> is found; and add the XmlNodeList to tempXDocument.
  4. Save the tempXDocument.
KMan