Adding XElement

C

CSharper

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.
 
M

Martin Honnen

CSharper said:
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.
 
C

CSharper

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.

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,
 
M

Martin Honnen

CSharper said:
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());
 
C

CSharper

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,
 
M

Martin Honnen

CSharper said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top