Hi,

i have one XML file,... in that i am maintaining the server name and database name,.../

Now i want to delete one set of server name and database name and insert those element into the first position of the same xml document,... for getting the recently accessed element,...

<Connections>
  <Connection>
    <Server>x1</Server>
    <Database>d1</Database>
  </Connection>
  <Connection>
    <Server>x2</Server>
    <Database>d2</Database>
  </Connection>
</Connections>

In the above code,
i want to delete the server x2 and d2 and insert before x1 and d1 like this given below,...

<Connections>
  <Connection>
    <Server>x2</Server>
    <Database>d2</Database>
  </Connection>
  <Connection>
    <Server>x1</Server>
    <Database>d1</Database>
  </Connection>

</Connections>

Can any one help me?

Recommended Answers

All 5 Replies

It's fairly trivial to move an element using Linq to XML (System.Xml.Linq namespace). Here's one such method.

XDocument document = XDocument.Load(xmlFile);
XElement element = document.Root.Element("Connection"); // gets first Connection element
element.Remove();
document.Root.Add(element); // adds after other elements
document.Save(newFile);

i want the changes to be done in the same xml document,
but you specified to save in new file.
whether it is a new xml document or the loaded same xml document?

You can absolutely save it to the same file. Just use the same variable or file name for the Load and Save operations.

Hi apegram,
Presume i have to be add another server and database x3 and d3, and last access server and db also same, in that time i want to add,

<Connections>
<Connection>
<Server>x3</Server>
<Database>d3</Database>
</Connection>
<Connection>
<Server>x2</Server>
<Database>d2</Database>
</Connection>
<Connection>
<Server>x1</Server>
<Database>d1</Database>
</Connection>
</Connections>

but now i have use your code,i have get

<Connections>

<Connection>
<Server>x2</Server>
<Database>d2</Database>
</Connection>
<Connection>
<Server>x3</Server>
<Database>d3</Database>
</Connection>
<Connection>
<Server>x1</Server>
<Database>d1</Database>
</Connection>
</Connections>

what is the reason?Can you please help me?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.