Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 5th, 2010, 03:41 AM
Registered User
 
Join Date: Jan 2010
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation how to add nodes to an existing xml file using vb.net

hi friends,
I have an xml file and i add a node to it using the following code.

LoadXmlDoc.Load("first.XML")
LoadXmlDoc_2.Load("second.XML")
Dim NodeToCreateOrReplace_2 As XmlNodeList = LoadXmlDoc_2.GetElementsByTagName("SB")
Dim NodeToCreateOrReplace As XmlNodeList = LoadXmlDoc.GetElementsByTagName("SB")
Dim newnode As XmlNode, currNode
'XmlNodeList NodeToCreateOrReplace = LoadXmlDoc.GetElementsByTagName(XmlTagNameTxtBx.Te xt);
Try
For Each selectednode As XmlNode In NodeToCreateOrReplace
Dim node As XmlNode = selectednode.SelectSingleNode("SBN")
For Each selectednode1 As XmlNode In LoadXmlDoc_2
Dim node_2 As XmlNode = selectednode.SelectSingleNode("SBN")
If node.InnerText = node_2.InnerText Then
' newnode = LoadXmlDoc.CreateElement("SB")
Dim docFrag As XmlDocumentFragment = doc.CreateDocumentFragment()
docFrag.InnerXml = selectednode1.OuterXml
currNode = LoadXmlDoc.DocumentElement("SBS").SelectSingleNode ("SB")
currNode.InsertAfter(LoadXmlDoc.ImportNode(docFrag , True), currNode.lastChild)

End If
Next
Next


im getting error "The element list has changed. The enumeration operation failed to continue."
please help.
 
Old January 5th, 2010, 04:58 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I tried to put your code into a VB project but I couldn't work out what you are trying to do. The doc variable isn't declared, so I'm not sure what that is meant to be and you have missed the end of the code as the Try is without a Catch or Finally.

Generally you get the error you mention if you add or delete an item from whatever you are iterating over - in this case one of the two For Each loops.

Perhaps if you tell us a bit about the XML and what you are actually trying to do we could help.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 5th, 2010, 05:50 AM
Registered User
 
Join Date: Jan 2010
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

sorry,
My code have some errors..
this is the updated one.
@samjudson
this is vb.net code not vb.

Here i have two xml files.
im comparing second.xml with first.xml and if there is any similar nodes in these files , the node in second.xml will replace the nodes in first.xml

LoadXmlDoc.Load("first.XML")
LoadXmlDoc_2.Load("second.XML")
Dim NodeToCreateOrReplace_2 As XmlNodeList = LoadXmlDoc_2.GetElementsByTagName("SB")
Dim NodeToCreateOrReplace As XmlNodeList = LoadXmlDoc.GetElementsByTagName("SB")
Dim newnode As XmlNode, currNode
Try
For Each selectednode As XmlNode In NodeToCreateOrReplace
Dim node As XmlNode = selectednode.SelectSingleNode("SBN")
For Each selectednode1 As XmlNode In LoadXmlDoc_2
Dim node_2 As XmlNode = selectednode.SelectSingleNode("SBN")
If node.InnerText = node_2.InnerText Then
Dim docFrag As XmlDocumentFragment = LoadXmlDoc.CreateDocumentFragment()
docFrag.InnerXml = selectednode1.OuterXml
currNode = LoadXmlDoc.DocumentElement("SBS").SelectSingleNode ("SB")
currNode.InsertAfter(LoadXmlDoc.ImportNode(docFrag , True), currNode.lastChild)

End If
Next
Next
Catch ex As Exception
End Try

Last edited by ammu86; January 5th, 2010 at 05:57 AM..
 
Old January 5th, 2010, 06:46 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Yes, I meant VB.Net.

That compiles, but without the XML I still don't know what you are trying to accomplish.

Dim node_2 As XmlNode = selectednode.SelectSingleNode("SBN")

This line refers to selectedNode - perhaps you mean selectedNode1?

Also NodeToCreateOrReplace_2 is never used, currNode is declared as an Object, not an XmlNode, and newnode is never used.

You could make you code much easier to read with better naming conventions. Also, there is no need to create the document fragment at all really. And you could select the list of nodes easier if you used xpath I suspect.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 6th, 2010, 12:25 AM
Registered User
 
Join Date: Jan 2010
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Smile RE:Insert a node to a particular position of xml.

hi.........


Quote:
Originally Posted by samjudson View Post
Yes, I meant VB.Net.

That compiles, but without the XML I still don't know what you are trying to accomplish.

Samjudson,
here i loaded two xml files..LoadXmlDoc & LoadXmlDoc_2 and if any similar nodes are there that particular node of loadxmldoc_2 will insert just after the existing node of loadxmldoc according to the criteria.


Dim node_2 As XmlNode = selectednode.SelectSingleNode("SBN")

This line refers to selectedNode - perhaps you mean selectedNode1?


this is wrong..i mean selectednode1. sorry for the mistake.

Also NodeToCreateOrReplace_2 is never used, currNode is declared as an Object, not an XmlNode, and newnode is never used.


I changed the currNode as XMLNode.Still the same issue.
yes..this is only a test code..i will try to make names much easier.

without document fragment i think its not possible to insert a node to a particular position.ie i want to specify where to insert the node exactly.


You could make you code much easier to read with better naming conventions. Also, there is no need to create the document fragment at all really. And you could select the list of nodes easier if you used xpath I suspect.

Last edited by ammu86; January 6th, 2010 at 12:33 AM..
 
Old January 6th, 2010, 04:25 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

XmlDocument.ImportNode works just fine using the original node, rather than the document fragment:

Code:
currNode.InsertAfter(LoadXmlDoc.ImportNode(selectednode1 , True), currNode.lastChild)
Perhaps you could give a small example of the two XML documents and what you expect the output to look like?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 6th, 2010, 04:47 AM
Registered User
 
Join Date: Jan 2010
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default RE:

hi................
Quote:
Originally Posted by samjudson View Post
XmlDocument.ImportNode works just fine using the original node, rather than the document fragment:

Import node will work only if the node wants to be add at the end of the document and it's not possible to specify the location where it to be inserted.
Code:
currNode.InsertAfter(LoadXmlDoc.ImportNode(selectednode1 , True), currNode.lastChild)
Perhaps you could give a small example of the two XML documents and what you expect the output to look like?
here im giving a simple example of two files.
first.xml
<root>
<person>
<name>a</name>
<age>25</age>
</person>

<person>
<name>b</name>
<age>35</age>
<person>
</root>

second.xml
<root>
<person>
<name>a</name>
<age>28</age>
</person>

so i want the output as follows

<root>
<person>
<name>a</name>
<age>25</age>
</person>
<root>
<person>
<name>a</name>
<age>28</age>
</person>
<person>

<name>b</name>
<age>35</age>
<person>
</root>

not like this
<root>
<person>
<name>a</name>
<age>25</age>
</person>

<person>
<name>b</name>
<age>35</age>
<person>


<person>
<name>a</name>
<age>28</age>
</person>
</root>

Last edited by ammu86; January 6th, 2010 at 04:49 AM..
 
Old January 6th, 2010, 05:13 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

ImportNode doesn't insert the node you are importing into the tree, that is the job of InsertAfter, which I am still using exactly as in your original code, therefore my code should work just fine thanks.

Your example XML above makes no reference to the SB, SBN etc elements, therefore I can't use it to work out what your code is trying to do.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
 
Old January 6th, 2010, 06:08 AM
Registered User
 
Join Date: Jan 2010
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi........................

Quote:
Originally Posted by samjudson View Post
ImportNode doesn't insert the node you are importing into the tree, that is the job of InsertAfter, which I am still using exactly as in your original code, therefore my code should work just fine thanks.

I gave only a sample of how to be the output should be.
Here is the original xml files that im using.

first.xml

<root>
<SB>
<SBN>1010</SBN>
<SBCN>0</SBCN>
<SBMID>11</SBMID>
</SB>
<SB>
<SBN>1020</SBN>
<SBCN>0</SBCN>
<SBMID>11</SBMID>
</SB>
</root>


second.xml
<root>
<SB>
<SBN>1010</SBN>
<SBCN>25</SBCN>
<SBMID>11</SBMID>
</SB>

<SB>
<SBN>1010</SBN>
<SBCN>50</SBCN>
<SBMID>11</SBMID>
</SB>
</root>

output should be
<root>
<SB>
<SBN>1010</SBN>
<SBCN>0</SBCN>
<SBMID>11</SBMID>
</SB>

<SB>
<SBN>1010</SBN>
<SBCN>0</SBCN>
<SBMID>11</SBMID>
</SB>
<SB>
<SBN>1010</SBN>
<SBCN>25</SBCN>
<SBMID>11</SBMID>
</SB>

<SB>
<SBN>1010</SBN>
<SBCN>50</SBCN>
<SBMID>11</SBMID>
</SB>
<SB>
<SBN>1020</SBN>
<SBCN>0</SBCN>
<SBMID>11</SBMID>
</SB>
</root>

Your example XML above makes no reference to the SB, SBN etc elements, therefore I can't use it to work out what your code is trying to do.
 
Old January 6th, 2010, 07:22 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Try something like this:

Code:
        doc1.Load("first.XML")
        doc2.Load("second.XML")
        Dim nodeList1 As XmlNodeList = doc1.SelectNodes("//SB/SBN")
        Dim nodeList2 As XmlNodeList = doc2.SelectNodes("//SB/SBN")

        Try
            For Each selectednode1 As XmlNode In nodeList1

                For Each selectednode2 As XmlNode In nodeList2
                    If selectednode1.InnerText = selectednode2.InnerText Then
                        Dim newNode As XmlNode = doc1.ImportNode(selectednode2.ParentNode, True)
                        doc1.SelectSingleNode("root").InsertAfter(newNode, selectednode1.ParentNode)
                    End If
                Next
            Next
        Catch ex As Exception
        End Try
It might not be exactly what you are after, but should give you some hints on how to go about doing things better.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?





Similar Threads
Thread Thread Starter Forum Replies Last Post
VB.net, adding XML data to an existing XML file saikoboarder XML 11 April 17th, 2008 04:19 PM
XML DOM :modifying the existing nodes attributes i sharmasourabh54 XML 1 February 14th, 2005 05:04 AM
javascript - add element to existing xml? willgotoo XML 4 October 8th, 2004 09:33 AM
Add and Remove Nodes in XML document. tutul128 XML 3 March 1st, 2004 10:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.