473,770 Members | 4,355 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Replacing Nodes in an XML file

7 New Member
Hello there, I have an XML file of structure that looks like this:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <documents>
  3.   <header id="1">
  4.     <title></title>
  5.     <prodGroup></prodGroup>
  6.     <version>
  7.       <major></major>
  8.       <minor></minor>
  9.     </version>
  10.   </header>
  11.   <header id="2">
  12.     <title></title>
  13.     <prod</prodGroup>
  14.     <version>
  15.       <major></major>
  16.       <minor></minor>
  17.     </version>
  18.   </header>
  19. </documents>
  20.  
The program I am currently writting will need to replace an entire header node. This is what the replace method looks like:
Expand|Select|Wrap|Line Numbers
  1. Private Function SaveHeader() As Boolean
  2.  
  3.             'get the entire  xml file to be saved to
  4.             Dim headerDocument As XmlDocument = FileManager.GetQAHeaderFile(m_header.ProductGroup)
  5.         'read the appropriate Node and load it into it's class
  6.             Dim oldHeader As QAHeader = QAHeader.Find(m_header.ProductGroup, m_header.Id)
  7.  
  8.             'If an old header exists, overwrite, else add new header
  9.             If oldHeader Is Nothing Then
  10.                 headerDocument.DocumentElement.AppendChild(m_header.CreateXMLNode(headerDocument))
  11.             Else
  12.                 'make a node out of the instance read above
  13.                 Dim oldNode As XmlNode = oldHeader.CreateXMLNode(headerDocument)
  14.         'make a node out of the information given via form
  15.                 Dim newNode As XmlNode = m_header.CreateXMLNode(headerDocument)
  16.                 'THIS LINE HERE!
  17.                 headerDocument.ReplaceChild(newNode, oldNode)
  18.             End If
  19.  
  20.             'Now the save the xml file to disk
  21.             headerDocument.Save(FileManager.GetQAHeaderLocation(m_header.ProductGroup))
  22.             Return True
  23.         End Function
The problem here is that I keep getting a runtime error claiming that "The node to be removed is not a child of this node." I tried to make oldNode contain everything in <documents></documents> but then the ReplaceChild methods deletes all other <header id=""></header> nodes and I'm left with only the contents of newNode.

Allow me to give you an example. I have
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <documents>
  3.   <header id="1">
  4.     <title>Whut</title>
  5.     <prodGroup>Audio &amp; Video</prodGroup>
  6.     <version>
  7.       <major>1</major>
  8.       <minor>3</minor>
  9.     </version>
  10.   </header>
  11.   <header id="2">
  12.     <title>teh</title>
  13.     <prodGroup>Audio &amp; Video</prodGroup>
  14.     <version>
  15.       <major>5</major>
  16.       <minor>0</minor>
  17.     </version>
  18.   </header>
  19. <header id="3">
  20.   <title>feck!@#</title>
  21.   <prodGroup>Audio &amp; Video</prodGroup>
  22.   <version>
  23.     <major>7</major>
  24.     <minor>2</minor>
  25.   </version>
  26. </header>
  27. </documents>
The user wishes to change <title>feck!@ #</title> to something a little less vulgar. Iterating thru the method presented above we have the outerXML of newNode & oldNode to read:

oldNode:
"<header id="3"><title>f eck!@#</title><prodGrou p>Audio &amp; Video</prodGroup><vers ion><major>7</major><minor>2</minor></version></header>"

newNode:
"<header id="3"><title>t ruck</title><prodGrou p>Audio &amp; Video</prodGroup><vers ion><major>1</major><minor>0</minor></version></header>"

At this time I'd like to say that regardless of what element has been changed I would like the entire contents of the appropriate <header> node to be rewritten. Is this possible and how?

Any help/suggestions are greatly appreciated. Thank you for your time and patience.
Oct 20 '06 #1
0 1457

Sign in to post your reply or Sign up for a free account.

Similar topics

12
2604
by: anonymous | last post by:
Hello, I need to replace this char  with another char. However I am not able to acieve this. I tried this but it doesnt work: str = str.Replace(chr(asc(194)), "") Can somebody help ?
1
1254
by: dotnetnoob | last post by:
i need to copy several xml files and use it as xml element and insert them into a xml file. i use DOM support in .Net and from the pervious help i was able to copy and insert them into the xml file that i want but i need to change some xml node value before i insert. what is the best way to do this?
2
2370
by: James Fifth | last post by:
Hello and God Bless, I am stumped trying to get a simple xml database replacing certain data with other data programmatically. This is what my xml looks like. **************************************** <Root_Element> <Topic index="1"> <Sub_Topic> <Notes></Notes>
0
1643
by: theFLY | last post by:
This has posted in the ASP sub-forum, to no avail I re-post here. Hello there, I have an XML file of structure that looks like this: <?xml version="1.0"?> <documents> <header id="1"> <title></title>
0
9602
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10071
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7431
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3987
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3589
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.