开发者

Restructure an xml document

开发者 https://www.devze.com 2022-12-31 13:21 出处:网络
here is my structure of xml document, <worldpatentdata> <patent-family> <exchange-documents>......</exchange-documents>

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


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.


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.
0

精彩评论

暂无评论...
验证码 换一张
取 消