1

I have a XML-document created by MS Excel. What I want is to combine nodes with same id. The xml I currently have is something like this:

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

And it should be something like this:

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

Data comes from Excel spreadsheet that has information on rows. For example book can have multiple authors and they are on separate rows with same id on first column.

1 Answers1

0

I suggest you use xml serialization to import your data from xml, manipulate your data through c# classes and output it in xml format. See this post for more informations.

That is how you read:

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);
}

Once ou got the class, it would be easy to merge/remove nodes through linq or loops.

Community
  • 1
  • 1
Fjodr
  • 919
  • 13
  • 32
  • Thanks for the answer, C# is little over the top for this particular problem (work related, no way to have C# installed, and somewhat rusty skillset). – Anthony Berkins May 23 '15 at 11:26