5 Replies - 562 Views - Last Post: 27 November 2012 - 08:17 AM Rate Topic: -----

#1 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

writing multiple xml entries into an XML file

Posted 26 November 2012 - 09:14 AM

So, I have this program that gathers 'source', 'destination', and 'job name' from a user and then adds this information to an XML file in this type of manner

-<Jobs> 
-<Job> 
<JobName>a</JobName> 
<Source>C:\Users\Public\Pictures\SamplePictures\Chrysanthemum.jpg</Source> 
<Source>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</Source>
<Source>C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg</Source>  
<Destination>C:\Users\Public\Pictures\Sample Pictures\a.zip</Destination> 
<Timestamp>11/23/2012 3:43:42 PM</Timestamp> 
</Job>


However, my issue comes from when i run the program a second time around. I want it to just collect the new information inputted in the program and append(add to) the XML file. However, when i run the program a second time the xml file only updates the source nodes for a single file (the last file read by the program).. So i am left with this after the initial run the second time around.

-<Job> 
<JobName>b</JobName> 
<Source>C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg</Source> <Destination>C:\Users\Public\Pictures\Sample Pictures\b.zip</Destination> 
<Timestamp>11/26/2012 10:39:50 AM</Timestamp>
 </Job>


**************(there should be more source files in the above code....this is my issue.)******************


[/HR]

[/HR]

i already have a check for if the file exists or not. This comes in at the very end when i click the 'finish' button

If Dir("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml") <> "" Then
        fjobdup()
    Else
        fJob()
    End If


Where the "fJob()" function writes the xml the first time through. And the "fJobDup()" function should append the xml file and add the new information below the initial info. I'll post these functions below.

Public Function fjobdup() As String

        Dim doc As new XmlDocument()


        doc.Load("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml")

        Dim job As XmlElement = doc.CreateElement("Job")
        Dim jName As XmlElement = doc.CreateElement("JobName")
        Dim jsource As XmlElement = doc.CreateElement("Source")
        Dim jdestin As XmlElement = doc.CreateElement("Destination")
        Dim jtime As XmlElement = doc.CreateElement("Timestamp")

        job.AppendChild(jName)
        job.AppendChild(jsource)
        job.AppendChild(jdestin)
        job.AppendChild(jtime)
        doc.DocumentElement.AppendChild(job)

        jName.InnerText = JobName.Text.ToString()

        Dim filesEnum3 As IEnumerator
        filesEnum3 = OpenFileDialog1.FileNames.GetEnumerator()


        While filesEnum3.MoveNext
            jsource.InnerText = filesEnum3.Current.ToString

        End While

        jdestin.InnerText = boxDestin.Text.ToString()
        jtime.InnerText = Now()

        doc.Save("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml")

    End Function


Public Function fJob() As String
        Dim Backxml As New XmlWriterSettings()
        Backxml.Indent = True
        Dim xmlwrt As XmlWriter = XmlWriter.Create("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml", Backxml)

        Dim filesEnum2 As IEnumerator
        filesEnum2 = OpenFileDialog1.FileNames.GetEnumerator()

        With xmlwrt
            .WriteStartDocument()
            .WriteComment("XML Backup.")
            .WriteStartElement("Jobs")

            .WriteStartElement("Job")

            Dim jName As String = JobName.Text.ToString
            .WriteElementString("JobName", jName.ToString)


            While filesEnum2.MoveNext
                .WriteStartElement("Source")
                .WriteString(filesEnum2.Current)
                .WriteEndElement()
            End While

            .WriteStartElement("Destination")
            .WriteString(boxDestin.Text)
            .WriteEndElement()

            .WriteStartElement("Timestamp")
            .WriteString(Now())
            .WriteEndElement()

            .WriteEndDocument()
            .Close()
        End With


    End Function


Is This A Good Question/Topic? 0
  • +

Replies To: writing multiple xml entries into an XML file

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,970
  • Joined: 02-June 10

Re: writing multiple xml entries into an XML file

Posted 26 November 2012 - 09:59 AM

Personally I think all the effort is going into the wrong direction.
You seem to be working hard at micro-managing the writing process. The biggest issue I have with this approach is the continual maintenance it takes. If you add any new property you have to scrub through all of your code to make sure you add it to the save/load methods.


Pesonally I would spend more time making a better class, then just serialize it. Serialization just loops through all the properties and writes your XML. You can tag a property as XMLIGNORE for things that don't need to be written in the file. But otherwise you don't have a lot of work every time you add a property. You just add it in the class and you're done.

The hierarchy of the class seems obvious based on this:
-<Job> 
<JobName>b</JobName> 
<Source>C:\Users\Public\Pictures\Sample Pictures\Hydrangeas.jpg</Source> <Destination>C:\Users\Public\Pictures\Sample Pictures\b.zip</Destination> 
<Timestamp>11/26/2012 10:39:50 AM</Timestamp>
 </Job>



So make a JOB class
C# (sorry}
class JOB
{
   public string Name { get; set; }
   public string Source { get; set; }
   public string Destination { get; set; }
   public DateTime LastExecutedDateTime { get; set;}

   public bool DoArchive()
   {
       bool successful = false;
       // Code that does the actual archiving here
       return successful;
   }
}


There is an in-depth tutorial on classes in my signature block.
Here is a tutorial on serialization:
Separating data from GUI - PLUS - serializing the data to XML
Was This Post Helpful? 0
  • +
  • -

#3 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Re: writing multiple xml entries into an XML file

Posted 26 November 2012 - 02:04 PM

I see what you are trying to get me to do with the serialization. And if i can't get this to work soon i'll probably end up re-doing the whole thing with that in mind.

It's probably a bit obvious i'm new to all this.

But the thing i'm wondering now is that if i went with the serialization route, how would i obtain the JobName, Source, and Destination and set them = to their respective strings when those items are coming from another class?

Wouldn't i want to use a function instead of creating a new clasS?
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5595
  • View blog
  • Posts: 11,970
  • Joined: 02-June 10

Re: writing multiple xml entries into an XML file

Posted 26 November 2012 - 03:21 PM

Take a look at the object tutorial I mentioned.

You've already been setting properties of one class from another - its nothing new to you.

When you make an instance of a new button, then set its text for example:

Button btnSend = new Button();
btnSend.Text = "Send";


Maybe you've only done that through drag and drop via the graphical designer, but that's the code being written for you if you look at the designer.cs file.

Trust me when I say this isn't scary and barely even new to you. Work through the tutorial and you'll see.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2268
  • View blog
  • Posts: 9,482
  • Joined: 29-May 08

Re: writing multiple xml entries into an XML file

Posted 26 November 2012 - 04:05 PM

You could always just use XML Literals.
Was This Post Helpful? 0
  • +
  • -

#6 maylortaylor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 26-November 12

Re: writing multiple xml entries into an XML file

Posted 27 November 2012 - 08:17 AM

I got it solved using another method. One that worked more closely with my code already. But thanks for the help guys. As i progress in my coding life - i'l be sure to try your techniques out.


Public Sub fjobdup() As String ' it's not a function because it doesn't return a value
         Dim doc As new XmlDocument()
 
        doc.Load("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml")
         Dim job As XmlElement = doc.CreateElement("Job")
        Dim jName As XmlElement = doc.CreateElement("JobName")
       Dim jdestin As XmlElement = doc.CreateElement("Destination")
        Dim jtime As XmlElement = doc.CreateElement("Timestamp")
 
        job.AppendChild(jName)
        jName.InnerText = JobName.Text.ToString()
 
        Dim filesEnum3 As IEnumerator
        filesEnum3 = OpenFileDialog1.FileNames.GetEnumerator()
 
        While filesEnum3.MoveNext
        Dim jsource As XmlElement = doc.CreateElement("Source") 'this effectively creates a new node on each cycle
         job.AppendChild(jsource)
         jsource.InnerText = filesEnum3.Current.ToString
        End While
 
        job.AppendChild(jdestin)
        jdestin.InnerText = boxDestin.Text.ToString()
 
        job.AppendChild(jtime)
        jtime.InnerText = Now()
 
        doc.DocumentElement.AppendChild(job)
 
        doc.Save("C:\Users\Matt Taylor\Desktop\Backup\Backup.xml")
 
    End Sub



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1