PC Review


Reply
Thread Tools Rate Thread

add a child element using c#

 
 
Claudia Fong
Guest
Posts: n/a
 
      23rd Jun 2008
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(ChildElementName);
ChildElement.SetAttribute(ChildNameAttributeName, Child.Name);
ChildElement.SetAttribute(ChildIDAttributeName, Child.ID);

parent.AppendChild(ChildElement);

Cheers!

Claudi

*** Sent via Developersdex http://www.developersdex.com ***
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      23rd Jun 2008
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(ChildElementName);
> 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/
 
Reply With Quote
 
Ignacio Machin ( .NET/ C# MVP )
Guest
Posts: n/a
 
      23rd Jun 2008
On Jun 23, 8:38*am, Claudia Fong <cdolphi...@yahoo.co.uk> wrote:
> 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(ChildElementName);
> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[LINQ TO SQL] Probleme for update a child element Le_Phasme Microsoft Dot NET 0 4th Jun 2009 09:03 PM
xml export with repeating child element pandamius Microsoft Excel Crashes 0 3rd Apr 2008 06:26 PM
<Compilation> invalid child element John Bailo Microsoft Dot NET 1 17th Mar 2007 07:10 AM
XML insert element to a certain nested child Kim Microsoft C# .NET 4 17th Aug 2006 10:01 AM
The element 'compilation' has invalid child element 'compilers'. =?Utf-8?B?Um9iZXJ0?= Microsoft ASP .NET 3 15th Nov 2005 01:48 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 11:33 PM.