Victor Charles
asked on
Help with appending data elememt from xmlfile1 to xmlfil2 based on identical IDs
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
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
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.
ASKER
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>
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>
OK, having the new file1.xml and the old file2.xml what should the new file2.xml look like.?
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
ASKER
Hi,
Once I combine the data element from file2 with file1 to create a new file, I don't need file2.
Thanks,
Victor
Once I combine the data element from file2 with file1 to create a new file, I don't need file2.
Thanks,
Victor
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.
ASKER
Hi,
I will try it and get back to you.
Thank You.
Victor
I will try it and get back to you.
Thank You.
Victor
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?
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?
ASKER
Hi,
What I initially posted was just an example, I forgot to include the <Root> </Root> nodes. Yes, please inlcude them.
Thanks,
Victor
What I initially posted was just an example, I forgot to include the <Root> </Root> nodes. Yes, please inlcude them.
Thanks,
Victor
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank You. I will try it and get back to you.
ASKER
Hi,
How do I declare XDocument? tried to declare it XMLDocument but that didn't work.
V.
How do I declare XDocument? tried to declare it XMLDocument but that didn't work.
V.
Imports System.Xml.Linq
and is found in the dll System.Xml.Linq.dll
and is found in the dll System.Xml.Linq.dll
ASKER
Hi,
It worked perfectly.
Thanks,
Victor
It worked perfectly.
Thanks,
Victor
Not a problem, glad to help.
ASKER
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
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
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.
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.
ASKER
OK. Will do.
Thanks,
Victor
Thanks,
Victor