471,313 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,313 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 4743
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Don Bate | last post: by
reply views Thread by Peter Aberline | last post: by
reply views Thread by rosydwin | last post: by

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.