I’m working with an application that needs to compare data in a database to data in an XML file. The problem is that when it compares, there is a failure when missing elements in the XML file are treated as empty strings and thus are not matched correctly to the data in the database.
The application itself creates the XML file, so I have control over that. What I’d like to do is set the default for all elements in the XML file so that missing elements in the XML file are converted to NULL when that XML file is later read into a dataset.
The source data is a dataset — the data could come from different tables or views in a different database. The only solution I’ve been able to find for this type of problem has been using something like [XmlElement("object1"), DefaultValue(NULL)] before the object when setting up the class. How could this be accomplished for a dataset when the columns in the table(s) would be different for different source tables?
Here is the current code that does the serializing to an XML file:
TextWriter tw = new StreamWriter(fileNameAndPath);
XmlSerializer xs = new XmlSerializer(typeof(DataCopy));
xs.Serialize(tw, DataCopyLog1);
Here is the class definition of the DataCopy:
public class DataCopy
{
public DateTime LastCopy { get; set; }
public DataSet Data { get; set; }
public DataCopy()
{
Data = new DataSet();
LastCopy = DateTime.Now;
}
}
Since no one ever commented, I thought I’d leave how this was resolved: it wasn’t. I couldn’t find a way to properly get nulls into the XML and read back out as nulls when loaded into the DataTable; every time it would be loaded as an empty string.
The underlying problem was attacked in a different way instead. The project now requires primary keys on the tables it is working with, so that way there are no nulls in the actual data that is being compared for if it is a new row, deleted row, etc.