PC Review


Reply
Thread Tools Rate Thread

Adding XElement

 
 
CSharper
Guest
Posts: n/a
 
      17th Apr 2009
I have an xml file with bunch of nodes of "Personal" information. I
have a XElement which I need to add it to the end of the file of
existing Personal XDocument. What is the best way of doing this in
Linq or any other way? I can always read with XmlReader and add it at
the end but there must be a better way of doing this.
Thanks.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      17th Apr 2009
CSharper wrote:
> I have an xml file with bunch of nodes of "Personal" information. I
> have a XElement which I need to add it to the end of the file of
> existing Personal XDocument. What is the best way of doing this in
> Linq or any other way?


Use the LINQ to XML classes like XDocument/XElement e.g.

XElement root = XElement.Load("file.xml");
root.Add(yourXElement);

That would add yourXElement as a child of the root element of file.xml.
You can of course add it elsewhere but then you need to share how your
XML looks and tell us where you want to insert.

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
 
 
 
CSharper
Guest
Posts: n/a
 
      17th Apr 2009
On Apr 17, 8:52*am, Martin Honnen <mahotr...@yahoo.de> wrote:
> CSharper wrote:
> > I have an xml file with bunch of nodes of "Personal" information. I
> > have a XElement which I need to add it to the end of the file of
> > existing Personal XDocument. What is the best way of doing this in
> > Linq or any other way?

>
> Use the LINQ to XML classes like XDocument/XElement e.g.
>
> * *XElement root = XElement.Load("file.xml");
> * *root.Add(yourXElement);
>
> That would add yourXElement as a child of the root element of file.xml.
> You can of course add it elsewhere but then you need to share how your
> XML looks and tell us where you want to insert.
>
> --
>
> * * * * Martin Honnen --- MVP XML
> * * * *http://msmvps.com/blogs/martin_honnen/


Thank you. This is what I have now but code fails with 'This operation
would create an incorrrectly structured document'.
I am using following code

doc.Add(CreateNewNode());
where the CreateNewNode() method returns XElement.

Am i supposed to return something other than XElement? When I create
new document I don't have a problem. it is only when I append.

Thanks,
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      17th Apr 2009
CSharper wrote:
> On Apr 17, 8:52 am, Martin Honnen <mahotr...@yahoo.de> wrote:
>> CSharper wrote:
>>> I have an xml file with bunch of nodes of "Personal" information. I
>>> have a XElement which I need to add it to the end of the file of
>>> existing Personal XDocument. What is the best way of doing this in
>>> Linq or any other way?

>> Use the LINQ to XML classes like XDocument/XElement e.g.
>>
>> XElement root = XElement.Load("file.xml");
>> root.Add(yourXElement);
>>
>> That would add yourXElement as a child of the root element of file.xml.
>> You can of course add it elsewhere but then you need to share how your
>> XML looks and tell us where you want to insert.



> Thank you. This is what I have now but code fails with 'This operation
> would create an incorrrectly structured document'.
> I am using following code
>
> doc.Add(CreateNewNode());
> where the CreateNewNode() method returns XElement.


What is doc? If it is an XElement as in my example above then you can
Add another XElement to it.
I guess you have an XDocument instead, then you need
doc.Root.Add(CreateNewNode());

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
 
Reply With Quote
 
CSharper
Guest
Posts: n/a
 
      17th Apr 2009
On Apr 17, 9:35*am, Martin Honnen <mahotr...@yahoo.de> wrote:
> CSharper wrote:
> > On Apr 17, 8:52 am, Martin Honnen <mahotr...@yahoo.de> wrote:
> >> CSharper wrote:
> >>> I have an xml file with bunch of nodes of "Personal" information. I
> >>> have a XElement which I need to add it to the end of the file of
> >>> existing Personal XDocument. What is the best way of doing this in
> >>> Linq or any other way?
> >> Use the LINQ to XML classes like XDocument/XElement e.g.

>
> >> * *XElement root = XElement.Load("file.xml");
> >> * *root.Add(yourXElement);

>
> >> That would add yourXElement as a child of the root element of file.xml..
> >> You can of course add it elsewhere but then you need to share how your
> >> XML looks and tell us where you want to insert.

> > Thank you. This is what I have now but code fails with 'This operation
> > would create an incorrrectly structured document'.
> > I am using following code

>
> > doc.Add(CreateNewNode());
> > where the CreateNewNode() method returns XElement.

>
> What is doc? If it is an XElement as in my example above then you can
> Add another XElement to it.
> I guess you have an XDocument instead, then you need
> * *doc.Root.Add(CreateNewNode());
>
> --
>
> * * * * Martin Honnen --- MVP XML
> * * * *http://msmvps.com/blogs/martin_honnen/- Hide quoted text -
>
> - Show quoted text -


Ok that did the trick. Out of curiosity, what is the difference
between doc.add and doc.root.add??

Thanks,
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      17th Apr 2009
CSharper wrote:

> Ok that did the trick. Out of curiosity, what is the difference
> between doc.add and doc.root.add??


If doc is an XDocument then doc.add(XNodeInstance) as the XNodeInstance
as a child of that XDocument. If doc already contains an element (its
root element) then you can't add a further element to the document itself.

If doc is an XDocument then doc.Root is the root element of that
XDocument and doc.Root.Add(XNodeInstance) adds the XNodeInstance as a
child of the root element and not as child of the doc itself.

--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
 
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
Selecting XML nodes w/ namespace and XElement Michael Bray Microsoft C# .NET 11 10th Jul 2008 05:45 PM
XElement, XAttribute Default Values Rory Becker Microsoft VB .NET 4 25th Jun 2008 12:44 PM
LINQ XElement.load method not working correctly with strings withperiods in them Mike N. Microsoft C# .NET 2 4th Jun 2008 03:37 AM
DataBinding CheckBoxList to an XElement Andy B Microsoft ASP .NET 0 20th May 2008 08:11 AM
XElement questions CSharper Microsoft C# .NET 4 14th May 2008 03:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:39 AM.