【问题标题】:Merge XML nodes in document合并文档中的 XML 节点
【发布时间】:2015-05-22 12:28:12
【问题描述】:

我有一个由 MS Excel 创建的 XML 文档。我想要的是组合具有相同 id 的节点。我目前拥有的xml是这样的:

<ns1:publications>
<ns1:book id="3" subType="book">
    <ns1:peerReviewed>false</ns1:peerReviewed>
    <ns1:publicationCategory>scientific</ns1:publicationCategory>
    <ns1:publicationStatus>published</ns1:publicationStatus>
    <ns1:language>en_GB</ns1:language>
    <ns1:title>
        <ns2:text lang="en" country="EN">Kirja3</ns2:text>
    </ns1:title>
    <ns1:abstract>
        <ns2:text lang="en" country="EN">
    </ns1:abstract>
    <ns1:persons>
        <ns1:author>
            <ns1:role>author</ns1:role>
            <ns1:person external="false" id="3">
                <ns1:firstName>Rob</ns1:firstName>
                <ns1:lastName>Hubbard</ns1:lastName>
            </ns1:person>
        </ns1:author>
    </ns1:persons>
    <ns1:organisations>
        <ns1:organisation id="220400"/>
    </ns1:organisations>
    <ns1:owner id="220400"/>
    <ns1:publicationDate>
        <ns2:year>2014</ns2:year>
    </ns1:publicationDate>
    <ns1:visibility>Public</ns1:visibility>
    <ns1:numberOfPages>655</ns1:numberOfPages>
    <ns1:placeOfPublication>Newcastle</ns1:placeOfPublication>
</ns1:book>
<ns1:book id="3">
    <ns1:title/>
    <ns1:abstract/>
    <ns1:persons>
        <ns1:author>
            <ns1:person id="4">
                <ns1:firstName>Chris</ns1:firstName>
                <ns1:lastName>Steward</ns1:lastName>
            </ns1:person>
        </ns1:author>
    </ns1:persons>
    <ns1:organisations>
        <ns1:organisation id="220400"/>
    </ns1:organisations>
    <ns1:owner id="220400"/>
    <ns1:publicationDate/>
</ns1:book>

它应该是这样的:

<ns1:publications>
<ns1:book id="3" subType="book">
    <ns1:peerReviewed>false</ns1:peerReviewed>
    <ns1:publicationCategory>scientific</ns1:publicationCategory>
    <ns1:publicationStatus>published</ns1:publicationStatus>
    <ns1:language>en_GB</ns1:language>
    <ns1:title>
        <ns2:text lang="en" country="EN">Kirja3</ns2:text>
    </ns1:title>
    <ns1:abstract>
        <ns2:text lang="en" country="EN">
    </ns1:abstract>
    <ns1:persons>
        <ns1:author>
            <ns1:role>author</ns1:role>
            <ns1:person external="false" id="3">
                <ns1:firstName>Rob</ns1:firstName>
                <ns1:lastName>Hubbard</ns1:lastName>
            </ns1:person>
        </ns1:author>
        <ns1:author>
            <ns1:person external="false" id="4">
                <ns1:firstName>Chris</ns1:firstName>
                <ns1:lastName>Steward</ns1:lastName>
            </ns1:person>
        </ns1:author>
    </ns1:persons>
    <ns1:organisations>
        <ns1:organisation id="220400"/>
        <ns1:organisation id="220300"/>
    </ns1:organisations>
    <ns1:owner id="220400"/>
    <ns1:publicationDate>
        <ns2:year>2014</ns2:year>
    </ns1:publicationDate>
    <ns1:visibility>Public</ns1:visibility>
    <ns1:numberOfPages>655</ns1:numberOfPages>
    <ns1:placeOfPublication>Newcastle</ns1:placeOfPublication>
</ns1:book>

数据来自包含行信息的 Excel 电子表格。例如,书可以有多个作者,并且它们位于不同的行中,第一列具有相同的 id。

【问题讨论】:

    标签: xml excel xslt


    【解决方案1】:

    我建议你使用xml序列化从xml中导入你的数据,通过c#类操作你的数据并以xml格式输出。请参阅此post 了解更多信息。

    这就是你的阅读方式:

    MyClass myObject = new MyClass;
    XmlSerializer ser = new XmlSerializer(myObject.GetType());
    using (FileStream fs = new FileStream(FilePath, FileMode.Open))
    {
        XmlTextReader reader = new XmlTextReader(fs);
        myObject = (MyClass)ser.Deserialize(reader);
    }
    

    一旦你得到了类,就可以很容易地通过 linq 或循环来合并/删除节点。

    【讨论】:

    • 感谢您的回答,C# 在这个特定问题上的表现略胜一筹(与工作相关,无法安装 C#,而且技能组有些生疏)。
    猜你喜欢
    • 2015-08-28
    • 1970-01-01
    • 1970-01-01
    • 2017-07-08
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 2012-08-16
    • 1970-01-01
    相关资源
    最近更新 更多