473,322 Members | 1,719 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,322 software developers and data experts.

add a child element using c#

Hi,

I have a xml document which is like this:

<profile document>
<Child name = profile1 id = 1>
</Child>
</profile document>
I want to add another element into this one and what is doing right now
with my code is overwrite the existing element... can someone help me?

<profile document>
<Child name = profile1 id = 1>
</Child>
<Child name = profile2 id = 2>
</Child>
</profile document>
XmlElement ChildElement =
parent.OwnerDocument.CreateElement(ChildElementNam e);
ChildElement.SetAttribute(ChildNameAttributeName, Child.Name);
ChildElement.SetAttribute(ChildIDAttributeName, Child.ID);

parent.AppendChild(ChildElement);

Cheers!

Claudi

*** Sent via Developersdex http://www.developersdex.com ***
Jun 27 '08 #1
2 4848
Claudia Fong wrote:
I have a xml document which is like this:

<profile document>
<Child name = profile1 id = 1>
</Child>
</profile document>
That is not an XML document as spaces inside element names (e.g "profile
document") are not allowed and as attribute values need to be quoted.
I want to add another element into this one and what is doing right now
with my code is overwrite the existing element... can someone help me?

<profile document>
<Child name = profile1 id = 1>
</Child>
<Child name = profile2 id = 2>
</Child>
</profile document>
XmlElement ChildElement =
parent.OwnerDocument.CreateElement(ChildElementNam e);
ChildElement.SetAttribute(ChildNameAttributeName, Child.Name);
ChildElement.SetAttribute(ChildIDAttributeName, Child.ID);

parent.AppendChild(ChildElement);
I don't see how that code could overwrite an existing element as you
create a new element and append it.
What lacks is e.g.
parent.OwnerDocument.Save("file.xml");
to persist the changes made.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 27 '08 #2
On Jun 23, 8:38*am, Claudia Fong <cdolphi...@yahoo.co.ukwrote:
Hi,

I have a xml document which is like this:

<profile document>
<Child name = profile1 id = 1>
</Child>
</profile document>

I want to add another element into this one and what is doing right now
with my code is overwrite the existing element... can someone help me?

<profile document>
<Child name = profile1 id = 1>
</Child>
<Child name = profile2 id = 2>
</Child>
</profile document>

XmlElement ChildElement =
parent.OwnerDocument.CreateElement(ChildElementNam e);
ChildElement.SetAttribute(ChildNameAttributeName, Child.Name);
ChildElement.SetAttribute(ChildIDAttributeName, Child.ID);

parent.AppendChild(ChildElement);

Cheers!

* * Claudi

*** Sent via Developersdexhttp://www.developersdex.com***
Hi,

It's impossible that you have that document. It's not a valid XMl
document, by far.

After that, how are you keeping your document in memory?
If you are using XmlDocument use the AppendChild method
Jun 27 '08 #3

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

Similar topics

0
by: Don Bate | last post by:
I'm having a problem with the "unique" feature of XML Schema. I'm relatively new to using this feature so it may be that I'm misunderstanding something. Let's assume that I have the following...
4
by: Doug Estep | last post by:
Below is a sample of my XML… <MetaData> <Table name="mytable"> <PrimaryKey name="pk"><Column name="mycolumn"/></PrimaryKey> </Table> </MetaData> <Sequencer> <Table name="mytable">
0
by: Peter Aberline | last post by:
Hi all, I'm trying to write a schema which defines the following document. <root> <letters> <a>A</a> <dog>DOG<dog> <b>B</b> <fish>FISH</fish>
13
by: kaeli | last post by:
Can anyone explain this to me? It's driving me insane. Save this and run it in IE or Opera and then in Mozilla or Netscape 6+. In IE/Opera, I get the expected 4 alerts. In Mozilla/Netscape, I...
2
by: Rob T. | last post by:
Given the following in a stylesheet: #parent { width: 50px; height: 50px; } #child {
1
by: George W. | last post by:
Okay, I'm a C#/XML newbie, and I've been wrestling with this for a while now, checked dotnet sites, articles, MSDN Library, etc. and haven't been able to determine why this is happening. I have...
1
by: adamredwards | last post by:
I have a page with some form elements that are dynamically generated. They are inserted into the dom by first cloning a node, changing the values like name, and then inserted with insertBefore(). ...
4
by: patrizio.trinchini | last post by:
Hi all, I'm new to XSLT and maybe my problem have a very trivial answer, but I need an expert that point me in the right direction. What I would obtain is to remove all the elements that have a...
1
by: pingalkar | last post by:
Hi, In my application, I call one popup winodow by using this link.. <a href="#" onClick="return showWindow('1','XYZ');"> <img src="images/magnifier.gif" ALT="Chemicals"...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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
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.