472,954 Members | 2,372 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,954 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 4832
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...

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.