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?

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

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);

Edited 6 Years Ago by apegram: n/a

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?

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

This article has been dead for over six months. Start a new discussion instead.