Best method to save array to XML file

lmurillo

Member
Joined
Feb 3, 2010
Messages
7
Programming Experience
1-3
I need some assistance with creating an XML file based off an array that I have.

In the application I have a class named ticketTable, which is basically an array composed of ticketData objects.

I've created an array, called virtualTable, which contains one or more arrays of the ticketTable type and the ticketTable arrays contain one or more objects of the ticketData type.

Hopefully I'm explaining myself correctly.

I tried to serialize the data to an XML file but it turns out that since the constructor of ticketTable and ticketData require parameters the serialization fails with the following error

ILC_Report_Tool.ticketTable cannot be serialized because it does not have a parameterless constructor.

So how can I save the data in a XML file and be able to recreate the array when loading the file again?
 
Can't you add a parameterless constructor to TicketTable class also?
 
The ticketTable object requires the name of a Queue to which the list of tickets belong to, I tried adding a parameterless constructor but that didn't work, I got the following error

There was an error reflecting type 'ILC_Report_Tool.ticketTable[]'.

Since I'm new to this whole saving to XML process, I pretty much don't get what it really means.
 
If you want default Xml serialization you must organize things in a fashion that enables that to happen. For example by exposing the constructor parameter also as a property, and also adding the required parameterless constructor, the serialization engine can do it's job. If making these simple adjustments is not an option with your class design an alternative is to implement IXmlSerializable Interface (System.Xml.Serialization) in your classes where you write the code to read/write the Xml content yourself.

You mentioned "Queue", if that means a publicly exposed class member of Queue(T) type that is also a problem for the default XmlSerializer, Queue objects can't be serialized to Xml. You'd have to accommodate by applying the XmlIgnore attribute and provide it's contents by other means, for example with a 'QItems' array property where the collective items can be get/set and serialized through.
 
I've created an array, called virtualTable, which contains one or more arrays of the ticketTable type and the ticketTable arrays contain one or more objects of the ticketData type.

Instead of an array, could you store the data in a DataTable/DataSet? This might be overkill for whatever your actually using the array for but its certainly easy to read/write back and forth to an XML file and may provide you with some additional functionaly that is not available to an array.
 
Tom, that is actually not that a bad of an idea, the ticketData objects consist of different strings anyways so I shouldn't have an issue with that...I'll definitely give that a try
 
Tom, your suggestion actually works wonderfully. Now I have a question regarding an error I'm getting when reading back the XML file that's saved using that method.

The ticketData may have some fields without any string of text so when saved to XML it would look something like this

VB.NET:
<name>John</name>
<phone>555-6510</phone>

<name>Jane</name>

As you can see, from the example above, Jane doesn't have a phone and the node <phone> doesn't exist for her entry, so when I read it I'll get the following error

VB.NET:
Column 'phone' does not belong to table Contact.

Now, how can I get around this? Would I have to try...catch each of the columns?
 
Glad it worked out. An xml file can handle blank values but the tags which represent the columns must be present. If your outputing/writing from your dataset to your xml file and any of the field values are blank it should still place the column tags on the file, well actually if just places the end tag. How are you writing the file? As said if auto outputingfrom a dataset (datset.writefile("yourfile")) it should still place a phone tag regardless if the value is blank or not.

VB.NET:
<DataSetName>
    <tblContacts>
        <name>John</name>
        <phone>555-6510</phone>
    </tblContacts>

    <tblContacts>
        <name>Jane</name>
        </phone>
    </tblContacts>
<DataSetName>
 
Ok, I actually found out what was happening, let me explain...

Since there was no data in one of the columns which caused for the tag to not even be written to the XML file and when reading the file I ran into the issue where the code would look for the tag but since it's not there then the programs runs into that error.

The work around is to include the following line

VB.NET:
If dt.Tables(0).Columns.Contains("Comment") Then .setComment(IIf(IsDBNull(dtRow("Comment")), "", dtRow("Comment")))

That way I'm checking if the tag or column exists prior to attempting to get any data from there and I'm also checking if it has any value.

Would this be overkill or unoptimized method?
 
I have some clients that occasional send files missing some tags too so checking in this instance is necessary for me since they wont fix it. However if you are the one creating the xml file, I'm still at a loss to why any tag would be missing. I would need to know more about how you are creating the file in the first place. For instance if you are itteratting thru a contact list; creating a new datarow which gets added to a datatable and then exporting the datatable/tableset to an xml file at the end, it should automatically create all tags regardless if a value is null or not. If you are the one creating this file, I would fix it at the source rather then attempt to handle the problem afterwards.
 
I have NEVER had a problem with reading and writing datatables in this fashion. I've attached an example project to show that there isn't a problem storing and retrieving blank and null values in a table, persisting it to xml and back again

What are you doing so differently that yours is crashing?!
 

Attachments

  • WindowsApplication7.zip
    19.6 KB · Views: 35
The main difference is probably the fact that I don't use anything like the DataSet1.xsd that you use in your example, I use something like the code below

VB.NET:
Dim dt as New DataSet

dt.ReadXml(FileName)

The I just proceed with calling each of the fields so that I can populate the objects

VB.NET:
Dim tmpTicket as ticketData

tmpTicket = New ticketData(dt.Tables(0).Rows("ID"))
tmpTicket.setTime(dt.Tables(0).Rows("Time"))

I guess that because of this it causes for a call to a missing column to fail
 
Back
Top