473,324 Members | 2,268 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,324 software developers and data experts.

Changing value of node

Hi

When using XmlDocument, I can create nodes and attributes as I like. What I
didn't achieve is changing the Value of a node (created with createElement),
it claims that it's the wrong node type. When changing the InnerText, I
loose all child nodes. When cloning the node and reassigning the child nodes
of the original node to the cloned node, the children are not visilble in
the InnerXml.

To make it simple. How do I change the value of a node without loosing its
children?

Any hints are appeaciated.

Thanks, Urs

Nov 12 '05 #1
2 1702
"Urs Vogel" <uv****@msn.com> wrote in message news:O$**************@TK2MSFTNGP14.phx.gbl...
What I didn't achieve is changing the Value of a node (created with createElement), it claims that it's the wrong node type.
This is correct. Elements have no Value in the DOM. They have
child nodes. Think of them as containers of other nodes, not
terminal nodes.
When cloning the node and reassigning the child nodes of the original node to the cloned node,
I'd like to see the code of exactly how these reassignments are
being performed before commenting on why the child nodes
may not be visible in the InnerXml (if the nodes are properly
moved from the original element's children to the new - cloned
- element then it should work); but did you know you could also
do a "deep copy" clone by passing a boolean argument of true
to the CloneNode( bool) method if all you're going to do is
copy the original element's children anyway.
To make it simple. How do I change the value of a node without loosing its children?


Recognize the difference between nodes and elements (Elements
are nodes, but not all nodes are elements).

Then change the child nodes of the element. All an element has,
besides it's identity (QName: in the Prefix, LocalName and Name-
spaceUri properties), and attributes (GetAttributes(int)) -- is it's
children.

Also (purely speculating), make sure if you're cloning the element
and changing it, that you remember to ReplaceChild( ) the new
element for the original element at the appropriate position in the
document's node tree. It's not enough to merely CreateElement( )
as you must also remember to attach that element at the correct
spot in its owner document.
Derek Harmon
Nov 12 '05 #2
Derek

Thanks! By changing the code from .RemoveNode/.AppendChild to ReplaceNode(),
it works. Great!!

if(xNode.ChildNodes.Count > 0)
{
// childs must be saved.

xClone = xDoc.XmlDoc.CreateElement(NodeName);
xClone.InnerText = Value;

foreach(XmlNode xc in xNode.ChildNodes)
xClone.AppendChild(xc.Clone());

xParent.ReplaceChild(xClone, xNode);
xNode = xClone;
}
else
// no childs to save
xNode.InnerText = Value;
from .RemoveNode/.AppendChild
"Derek Harmon" <lo*******@msn.com> schrieb im Newsbeitrag
news:eV*************@TK2MSFTNGP14.phx.gbl...
"Urs Vogel" <uv****@msn.com> wrote in message
news:O$**************@TK2MSFTNGP14.phx.gbl...
What I didn't achieve is changing the Value of a node (created with
createElement), it claims that it's the wrong node type.


This is correct. Elements have no Value in the DOM. They have
child nodes. Think of them as containers of other nodes, not
terminal nodes.
When cloning the node and reassigning the child nodes of the original
node to the cloned node,


I'd like to see the code of exactly how these reassignments are
being performed before commenting on why the child nodes
may not be visible in the InnerXml (if the nodes are properly
moved from the original element's children to the new - cloned
- element then it should work); but did you know you could also
do a "deep copy" clone by passing a boolean argument of true
to the CloneNode( bool) method if all you're going to do is
copy the original element's children anyway.
To make it simple. How do I change the value of a node without loosing
its children?


Recognize the difference between nodes and elements (Elements
are nodes, but not all nodes are elements).

Then change the child nodes of the element. All an element has,
besides it's identity (QName: in the Prefix, LocalName and Name-
spaceUri properties), and attributes (GetAttributes(int)) -- is it's
children.

Also (purely speculating), make sure if you're cloning the element
and changing it, that you remember to ReplaceChild( ) the new
element for the original element at the appropriate position in the
document's node tree. It's not enough to merely CreateElement( )
as you must also remember to attach that element at the correct
spot in its owner document.
Derek Harmon

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Free-Ed, Ltd. | last post by:
I am going nuts trying to find a paragraph in a book that described how to change the text content (HTML) in a DIV. Actually I have an array of HTML strings that I want to drop into the DIV,...
9
by: F. Da Costa | last post by:
Hi, Does anybody know why IE5+ does *not* honour array objects (like a table) across a session? Example: Frame A contains a var tableVar which is set via form Frame B (on init) using...
8
by: Margaret MacDonald | last post by:
I'm a js novice trying to teach myself. I'm using Flanagan's 'Javascript, the definitive guide' from O'Reilly as a text. But either I'm dopier than usual or its layout doesn't match my learning...
5
by: Good Man | last post by:
Hi there I'm adding form fields on the fly with some javascript DOM programming. I basically just clone a hidden <div>, then adjust node properties to make this new <div> have unique values...
0
by: chris yoker via DotNetMonster.com | last post by:
hiya, I previously obtained code that enabled me the replace a node and its innerText.The code relied on the node that was to be replaced having ?childNodes? However, it seems that the nodes...
5
by: John N. | last post by:
Hi All, Here I have a linked list each containing a char and is double linked. Then I have a pointer to an item in that list which is the current insertion point. In this funtion, the user...
7
by: steve | last post by:
Hi All I need to change text in a Report Viewer document at runtime It is just the heading I want to change to match what the user has chosen as the Report to view How do I access the...
1
by: deepika patra | last post by:
hi all, i want to modify the value of a node of a xml file. i have to do this in c#.net language. i have to assume that the node value of the file wich i want to modify is already created.i...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.