Dynamically writing child nodes to an XML file.

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...

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
1) What does your XML file look like?  

2) .NET version

PatrickJacquesAuthor Commented:
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.
Bob LearnedCommented:
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).

JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

PatrickJacquesAuthor Commented:
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
Bob LearnedCommented:
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()


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PatrickJacquesAuthor Commented:
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?

Bob LearnedCommented:
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.

PatrickJacquesAuthor Commented:
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?
PatrickJacquesAuthor Commented:
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.
Bob LearnedCommented:
I ran this on 2005, using a Windows Forms application, and it ran just fine.  What kind of application is this code running under?

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.