Solved

Inserting Xelement in XML file before the root

Posted on 2008-06-26
4
1,104 Views
Last Modified: 2013-12-16
Hi Experts,
Still learning LINQ and stuck on this one..
I have a pretty simple XML file:

Xdoc document = XDoc.Load("
<root>
<hi>
  <node1></node1>
  <node2></node2>
  <node3></node3>
</hi>
<hi>
  <node1></node1>
  <node2></node2>
  <node3></node3>
</hi>
</root>
");

How can I insert another <hi> XElement before before the </root>. Seems like the only options are to Add right after the <root> or as a child to the <root>.
Thank you in advance.
0
Comment
Question by:techsuppoprt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 400 total points
ID: 21878669
Hi techsuppoprt;

I am on the learning curve with Linq myself. But here is an answer to your question, see sample.

The Add extension method adds the element to the end, should have called this method AddLast, If you want to add it to the front use the AddFirst method.

Fernando
XDocument document = new XDocument(
	new XElement("root",
		new XElement("hi",
			new XElement("node1", ""),
			new XElement("node2", ""),
			new XElement("node3", "")),
		new XElement("hi",
			new XElement("node1", ""),
			new XElement("node2", ""),
			new XElement("node3", ""))
	));
		
/* Result of the code above
<root>
  <hi>
    <node1></node1>
    <node2></node2>
    <node3></node3>
  </hi>
  <hi>
    <node1></node1>
    <node2></node2>
    <node3></node3>
  </hi>
</root>
*/
 
// Add a new element to the end of the tree just before </root>
document.Element("root").Add(new XElement(new XElement("hi",
			new XElement("node1", "Value1"),
			new XElement("node2", "Value2"),
			new XElement("node3", "Value3"))));
 
/* This is the result of the add
<root>
  <hi>
    <node1></node1>
    <node2></node2>
    <node3></node3>
  </hi>
  <hi>
    <node1></node1>
    <node2></node2>
    <node3></node3>
  </hi>
  <hi>
    <node1>Value1</node1>
    <node2>Value2</node2>
    <node3>Value3</node3>
  </hi>
</root>
*/

Open in new window

0
 
LVL 1

Author Comment

by:techsuppoprt
ID: 21879012
Oh nice, that was simple.

I was trying to do
document.Add(.....)
and all I had to do the name of the "root element I want to add my XElement to... makes perfect sence!
Thanks again Fernando!
0
 
LVL 1

Author Closing Comment

by:techsuppoprt
ID: 31471128
Thanks again
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 21880647
Not a problem, always glad to help. ;=)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question