Hello there, I have an XML file of structure that looks like this:
Expand|Select|Wrap|Line Numbers
- <?xml version="1.0"?>
- <documents>
- <header id="1">
- <title></title>
- <prodGroup></prodGroup>
- <version>
- <major></major>
- <minor></minor>
- </version>
- </header>
- <header id="2">
- <title></title>
- <prod</prodGroup>
- <version>
- <major></major>
- <minor></minor>
- </version>
- </header>
- </documents>
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
- Private Function SaveHeader() As Boolean
- 'get the entire xml file to be saved to
- Dim headerDocument As XmlDocument = FileManager.GetQAHeaderFile(m_header.ProductGroup)
- 'read the appropriate Node and load it into it's class
- Dim oldHeader As QAHeader = QAHeader.Find(m_header.ProductGroup, m_header.Id)
- 'If an old header exists, overwrite, else add new header
- If oldHeader Is Nothing Then
- headerDocument.DocumentElement.AppendChild(m_heade r.CreateXMLNode(headerDocument))
- Else
- 'make a node out of the instance read above
- Dim oldNode As XmlNode = oldHeader.CreateXMLNode(headerDocument)
- 'make a node out of the information given via form
- Dim newNode As XmlNode = m_header.CreateXMLNode(headerDocument)
- 'THIS LINE HERE!
- headerDocument.ReplaceChild(newNode, oldNode)
- End If
- 'Now the save the xml file to disk
- headerDocument.Save(FileManager.GetQAHeaderLocatio n(m_header.ProductGroup))
- Return True
- 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
- <?xml version="1.0"?>
- <documents>
- <header id="1">
- <title>Whut</title>
- <prodGroup>Audio & Video</prodGroup>
- <version>
- <major>1</major>
- <minor>3</minor>
- </version>
- </header>
- <header id="2">
- <title>teh</title>
- <prodGroup>Audio & Video</prodGroup>
- <version>
- <major>5</major>
- <minor>0</minor>
- </version>
- </header>
- <header id="3">
- <title>feck!@#</title>
- <prodGroup>Audio & Video</prodGroup>
- <version>
- <major>7</major>
- <minor>2</minor>
- </version>
- </header>
- </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 & Video</prodGroup><version><major>7</major><minor>2</minor></version></header>"
newNode:
"<header id="3"><title>truck</title><prodGroup>Audio & 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.