tags:

views:

79

answers:

2

I just finished an app to import a fairly simple XML data file into a db. The app works fine but I'd like to know how I should handle a bad XML file. For example, the user selects an XML file that has a different layout than what is required for my app. Eventually, I'd also like to add the ability the select multiple files to be imported and would prefer to just alert the user when a malformed file is encountered rather than throw an exception.

Any suggestions?

Thanks!

Edit: sample XML file...

<export-database name="/data/data/dump.db">
<table name="gpsPoints">
    <row>
      <col name="_id">0</col> 
      <col name="latitude">0000000000</col> 
      <col name="longitude">0000000000</col> 
      <col name="rssi">-00</col> 
      <col name="logdate">00 000 00 00:00:00</col> 
      <col name="mcc">000</col> 
      <col name="mnc">000</col> 
      <col name="lac">00</col> 
      <col name="cellid">000</col> 
      <col name="site_lat">0</col> 
      <col name="site_lng">0</col> 
      <col name="tech">0000000000</col> 
      <col name="ber">-000</col> 
      <col name="callstate">0000000000/col> 
      <col name="roaming">0000000000</col> 
    </row>
</table>
</export-database>
+5  A: 

that sounds like a simple try-catch thing to me... no? Things do tend to get complicated when the structure isn't the problem, but the data supplied is wrong in some way and you need the ability to "reject" specific records/nodes, but that does not sound like your case. Also it can be somewhat useful to know how exactly you reading XML (i.e. DOM, XML serialization, XML Linq).

liho1eye
So wrap the code of the read method(s) in a try-catch?
Scott
Sure. One thing you probably want is to give user more details on wherever your error is due to malformed XML or because of the bad data in it. The exception that you catch should contain details on former, and its up to you to handle the latter.
liho1eye
+2  A: 

Use a simple try/catch, parsing the XML file and checking if it's valid will probably take more effort to write and will take longer to process.

If you were populating a dataset for example, the code would look like this

try {
    DataSet ds = new DataSet();
    ds.LoadXml("myfile.xml");
}
catch { 
    ErrorLabel.Text = "Your XML isn't valid";
}
Marko