重构xml文档

时间:2010-05-17 12:03:42

标签: c# xml

这是我的xml文档结构,

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

在上面的xml结构中,发现所有<exchange-documents>元素都是<patent-family>元素的子节点。 我希望这些<exchange-documents>元素作为<family-member>元素的子节点,以便xml具有以下结构,

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

任何人都可以帮我吗? 感谢

2 个答案:

答案 0 :(得分:0)

通过DOM迭代<patent-family>的孩子,记住你在途中遇到的每个<exchange-documents>。每当您看到<familymember>时,请移动其下的记忆元素。

答案 1 :(得分:0)

在我的头顶上,一种方法可能是:

  1. 使用XDocument加载xml,并创建一个tempXDocument。
  2. foreach (XElement...通过元素并添加到<exchange-documents>
  3. 的XmlNodeList项
  4. 找到<familymember>时,在tempXDocument中添加<familymember>;并将XmlNodeList添加到tempXDocument。
  5. 保存tempXDocument。