Using LINQ to write to XML File

Hello all so sorry I have been offline for so long.

What have I been up to? Well KJM Solutions site is up and running again (http://www.kjmsolutions.com ). It is a site that will be devoted mostly to development of my own skills but also will offer custom development of .NET software if you should be interested :). Right now just the default page for the provider is there but finding a host and getting it going is a real challenge. I won’t be hosting my own servers anymore and i will miss that but had to make some choices. m6.net is the provider I chose if anyone is interested.

In addition, I am in the process of reforming Jericho, the Christian parody rock band I had in Sheboygan. So that is taking a lot of time. Also playing with some other churches, but really just trying to get in the groove of playing keyboards again after a long time of not playing.

Anyway, today’s topic is writing to a XML file using LINQ. Make it a great day!

Lets say you have a XML file called Settings.xml that looks like this

<?xml version="1.0" encoding="utf-8"?>
<Network>
<Host url="123.14.54.01">
   <Location>New York City</Location>
   <Administrator>John Smith</Administrator>
   <System>Windows 2003 Advance Server</System>
</Host>
<Host url="223.41.45.10">
   <Location>Florida</Location>
   <Administrator>Jonathan Plush</Administrator>
   <System>Windows 2005 Server</System>
</Host>
</Network>

The following code will open the XML file and add a Host node with children to the end of the  Host node list.

Dim xml As XDocument = XDocument.Load("Settings.xml")

Dim Network As XElement = xml.Root
Dim newHost As New XElement("Host", New XAttribute("url", txtURL.Text), _
                          New XElement("Location", txtLocation.Text), _
                          New XElement("Administrator", txtAdministrator.Text), _
                          New XElement("System", txtSystem.Text))
Network.Add(newHost)
xml.Save("Settings.xml")

If you want to load it to the very first node just after Network node then replace this line of code:

Network.Add(newHost)

With this line of code:

Network.AddFirst(newHost)

 

Advertisements
  1. #1 by Ken on August 27, 2008 - 6:17 am

    I prefer using the new xml features for creating the xml element. It is easier to read
     

            Dim newHost = <Host url=<%= txtUrl.Text %>>                          <Location><%= txtLocation.Text %></Location>                          <Administrator><%= txtAdministrator.Text %></Administrator>                          <System><%= txtSystem.Text %></System>                      </Host>

  2. #2 by Kelly on August 27, 2008 - 9:31 am

    Thanks Ken. Good Suggestion. Working my way through it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: