Dynamically writing child nodes to an XML file.

Posted on 2006-04-25
Medium Priority
Last Modified: 2012-06-22
Hi, I'm writting a little application that is going to send an email to a bunch of addresses that are listed in a listbox. I am reading this information from an XML file and I want to be able to write back the new list when the user press SAVE. I was thinking of choosing the NODE and remove all childnodes then recreate them with every items in the listbox but I can't find how I could do that.

Any idea?

BTW I used a value in my parent node so I can't simply use removeall, it removes the attribute too...

Question by:PatrickJacques
  • 5
  • 5
LVL 96

Expert Comment

by:Bob Learned
ID: 16540251
1) What does your XML file look like?  

2) .NET version


Author Comment

ID: 16546087
Hi, using VB .NET 2005. Here's my XML :

    <sunday>04:20:00 AM-08:00:00 PM</sunday>
    <monday>07:20:00 AM-09:00:00 PM</monday>
    <tuesday>03:20:00 AM-10:00:00 PM</tuesday>
    <wednesday>04:20:00 AM-08:00:00 PM</wednesday>
    <thursday>05:20:00 AM-08:00:00 PM</thursday>
    <friday>06:20:00 AM-08:30:00 PM</friday>
    <saturday>07:20:00 AM-08:00:00 AM</saturday>
  <EMAILS enabled="1">
    <p>Software crashed, process have been restarted.</p>
  <DBWATCH enabled="1">

So as you can see under Emails, I have two email addresses. If in the listbox I change, delete or add email adresses, I might have to add nodes... I was thinking of deleting all child nodes and then recreate one by one each node for each items in the listbox... I just don't know how to proceed, unless you have a better way to do things?

Thanks man.
LVL 96

Expert Comment

by:Bob Learned
ID: 16546184
You can clear children easily by setting the InnerXml to nothing for the node.

Here is an example:

Dim doc As New XmlDocument
Dim node As XmlNode = doc.SelectSingleNode("//EMAILS")
node.InnerXml = ""

The resulting OuterXml would be:
    <EMAILS enabled="1"></EMAILS>

You could either create the hierarchy by adding children to the node, or just by setting InnerXml to a string (then you would only need to set a new value and not clear the value first).

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 16546361
Do you have an exemple how to proceed with the creation of the children nodes? That was my the purpose of my question actually ;) I might have explained badly..

Thanks Bob
LVL 96

Accepted Solution

Bob Learned earned 375 total points
ID: 16546591
I would do it like this:

    Dim doc As New XmlDocument
    Dim node As XmlNode = doc.SelectSingleNode("//EMAILS")

    Dim sb As New System.Text.StringBuilder
    For Each item As String In Me.ListBox1.Items
      sb.Append("<p>" & item & "</p>")

    node.InnerXml = sb.ToString()


Author Comment

ID: 16547036
Hi, it kind of work, but when I look at the file using notepad I get something like that :


When I open the file using Internet Explorer, everything is fine. The thing is that when I load the XML file in my program, I look for the node "p" (which is supposed to be <p> not &lt;p&gt;) and then I write evey item in the listbox. Now when I load my program, it doesn't show anything in the box because the node is not written properly.

Any ideas?

LVL 96

Expert Comment

by:Bob Learned
ID: 16547154
1) I am using 2003
2) When I created my test XML file, I left this declaration:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

3) I don't get that problem.


Author Comment

ID: 16547241
Hi I used <?xml version="1.0"?> instead of <?xml version="1.0" encoding="UTF-8" standalone="yes"?>. I changed it to yours but still do the same thing... I get :

<EMAILS enabled="1">&lt;p&gt;email@test.ca&lt;/p&gt;&lt;p&gt;email2@test.ca&lt;/p&gt;</EMAILS> in pure text (notepad).

I don't have a clue why it shows garbage instead of a simple <>.

More ideas?

Author Comment

ID: 16547663
I read something that I guess has something to do with the weird unreadable characters. It's all about CDATA and PCDATA : http://www.w3schools.com/dtd/dtd_building.asp

I just don't know how to handle that.
LVL 96

Expert Comment

by:Bob Learned
ID: 16551586
I ran this on 2005, using a Windows Forms application, and it ran just fine.  What kind of application is this code running under?


Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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 explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Screencast - Getting to Know the Pipeline
Suggested Courses
Course of the Month16 days, 13 hours left to enroll

864 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