473,396 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Replacing Nodes in an XML file

7
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>feck!@#</title><prodGroup>Audio &amp; Video</prodGroup><version><major>7</major><minor>2</minor></version></header>"

newNode:
"<header id="3"><title>truck</title><prodGroup>Audio &amp; Video</prodGroup><version><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 1436

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

Similar topics

12
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
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...
2
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. ...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.