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


Thanks!
PatrickJacquesAsked:
Who is Participating?
 
Bob LearnedCommented:
I would do it like this:

    Dim doc As New XmlDocument
    doc.Load("c:\temp\test.xml")
    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>")
    Next

    node.InnerXml = sb.ToString()
    doc.Save("c:\temp\test.xml")

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

2) .NET version

Bob
0
 
PatrickJacquesAuthor Commented:
Hi, using VB .NET 2005. Here's my XML :

<Configuration>
    <DATES>
    <sunday>1</sunday>
    <monday>1</monday>
    <tuesday>0</tuesday>
    <wednesday>1</wednesday>
    <thursday>1</thursday>
    <friday>0</friday>
    <saturday>1</saturday>
  </DATES>
  <TIME>
    <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>
  </TIME>
  <EMAILS enabled="1">
<p>user1@test.ca</p>
<p>test@test.ca</p>
  </EMAILS>
    <MESSAGE>
    <p>Software crashed, process have been restarted.</p>
  </MESSAGE>
  <DBWATCH enabled="1">
    <threshold>1500</threshold>
    <localmsg>1</localmsg>
    <sendmail>1</sendmail>
    <sendwarning>1</sendwarning>
    <warningthreshold>1000</warningthreshold>
    <warningunit>MB</warningunit>
  </DBWATCH>
</Configuration>


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.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
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
doc.Load("c:\temp\test.xml")
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).

Bob
0
 
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
0
 
PatrickJacquesAuthor Commented:
Hi, it kind of work, but when I look at the file using notepad I get something like that :

&lt;p&gt;test@test.ca&lt;/p&gt;&lt;p&gt;test1@test.ca&lt;/p&gt;&lt;p&gt;test2@test.ca&lt;/p&gt;

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?

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

Bob
0
 
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?
0
 
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.
0
 
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?

Bob
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.