Solved

Help with appending data elememt from xmlfile1 to xmlfil2 based on identical IDs

Posted on 2014-02-25
18
179 Views
Last Modified: 2014-08-01
Hi,


If file1.xml contains

<Table1>
<SN>10411</SN>
<NSC>ITEMA</NSC>
</Table1>
<Table1>
<SN>10412</SN>
<NSC>ITEMB</NSC>
</Table1>
<Table1>
<SN>10421</SN>
<NSC>ITEMC</NSC>
</Table1>
<Table1>
<SN>10521</SN>
<NSC>ITEMD</NSC>
</Table1>
<Table1>
<SN>10611</SN>
<NSC>ITEME</NSC>
</Table1>


and file2.xml contains

<Table1>
<SN>10411</SN>
<NSD>ITEMY</NSD>
</Table1>
<Table1>
<SN>10412</SN>
<NSD>ITEMV</NSD>
</Table1>
<Table1>
<SN>10421</SN>
<NSD>ITEMW</NSD>
</Table1>

How do I append NSD IN file1.xml where N have identical values using VB.NET?

Victor
0
Comment
Question by:vcharles
  • 9
  • 7
  • 2
18 Comments
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39887891
The file1.xml does NOT have a node with NSD in it. Also please show what you want the resulting file2.xml to look like after the operation is complete.
0
 

Author Comment

by:vcharles
ID: 39887944
Hi,

Just read your post. Based on the example I gave, File3.xml should look as follows:

<Table1>
<SN>10411</SN>
<NSC>ITEMA</NSC>
<NSD>ITEMY</NSD>
</Table1>
<Table1>
<SN>10412</SN>
<NSC>ITEMB</NSC>
<NSD>ITEMV</NSD>
</Table1>
<Table1>
<SN>10421</SN>
<NSC>ITEMC</NSC>
<NSD>ITEMV</NSD>
</Table1>
<Table1>
<SN>10521</SN>
<NSC>ITEMD</NSC>
</Table1>
<Table1>
<SN>10611</SN>
<NSC>ITEME</NSC>
</Table1>
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39888513
OK, having the new file1.xml and the old file2.xml what should the new file2.xml look like.?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39888697
You need something along the lines of the following:
        '// assuming "doc1" has file1 loaded and "doc2" has file2 loaded

        Dim nodes As XmlNodeList = doc2.SelectNodes("//Table1")
        Dim copyNode As XmlNode
        Dim targetNode As XmlNode

        For Each node As XmlNode In nodes

            targetNode = doc1.SelectSingleNode("//Table1[SN='" & node("SN").InnerText & "']")
            If Not targetNode Is Nothing Then
                copyNode = targetNode.OwnerDocument.ImportNode(node("NSD"), True)
                targetNode.AppendChild(copyNode)
            End If

        Next

Open in new window

0
 

Author Comment

by:vcharles
ID: 39888952
Hi,

Once I combine the data element from file2 with file1 to create a new file, I don't need file2.

Thanks,

Victor
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39889043
OK, so ditch it then. The code i posted appends the nodes to file1, you just need to call Save() on the XmlDocument object to commit it to disk. You can do what you like with file2 after that point.
0
 

Author Comment

by:vcharles
ID: 39889053
Hi,

I will try it and get back to you.

Thank You.

Victor
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39889695
Hi Victor;

It looks like the two XML files are NOT well formed, they are both missing a root node and therefore cannot be loaded into an XML Document. Does the actual documents have a root node and if not is there an issue of adding one?
0
 

Author Comment

by:vcharles
ID: 39889724
Hi,

What I initially posted was just an example, I forgot to include the <Root> </Root> nodes. Yes, please inlcude them.

Thanks,

Victor
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 62

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 39889865
Hi Victor;

This should give you what you are looking for.

' Load the file1.xml and file2.xml into memory
Dim f1 = XDocument.Load("C:\Working Directory\file1.xml")
Dim f2 = XDocument.Load("C:\Working Directory\file2.xml")

' Query both documents and Join on the SN node in each 
Dim results = From n1 In f1.Descendants("Table1") _
              Join n2 In f2.Descendants("Table1") On n1.Element("SN").Value Equals n2.Element("SN").Value _
              Select n1, n2

' Add the NSD node in file2.xml to the corresponding node in file1.xml
For Each pair in results
    pair.n1.Add(pair.n2.Element("NSD"))
Next

' Save file1.xml as file3.xml
f1.Save("C:\Working Directory\file3.xml")

Open in new window

0
 

Author Comment

by:vcharles
ID: 39890171
Thank You. I will try it and get back to you.
0
 

Author Comment

by:vcharles
ID: 39890389
Hi,

How do I declare XDocument? tried to declare it XMLDocument but that didn't work.

V.
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39890400
Imports System.Xml.Linq

and is found in the dll System.Xml.Linq.dll
0
 

Author Comment

by:vcharles
ID: 39893783
Hi,

It worked perfectly.

Thanks,

Victor
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 39894756
Not a problem, glad to help.
0
 

Author Comment

by:vcharles
ID: 40233841
Hi Fernando,

I am receiving error message  "Object reference not set to an instance of an object." on line:
 Join n2 In f2.Descendants("Table1") On n1.Element("SN").Value Equals n2.Element("SN").Value
and n2.Element("SN").Value  is highlighted as specific cause of error.

which I think is because some records don't have any matching values, for example file1 may have an SN where there is no matching SN in file 2.

How do I avoid this error?

Thanks,

Victor
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 40234188
Hi Victor;

Please open a new question for this. You can post here the link to the new post and I will have a look. In your new post please post sample xml files that cause the specific error.

Thanks.
0
 

Author Comment

by:vcharles
ID: 40234346
OK. Will do.

Thanks,

Victor
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now