Deserialize with XmlSerializer having external xmlns reference

614 Views Asked by At

I have an ever increasing number of BCP (t-sql) format files in XML that I need to read. I create the xsd file using xsd.exe and a number of BCP format files and attempt to read the xml file as an object. But it fails like this:

Unhandled Exception: System.InvalidOperationException: There is an error in XMLdocument (4, 6). ---> System.InvalidOperationException: The specified type was not recognized: name='CharTerm', namespace='http://schemas.microsoft.com/sqlserver/2004/bulkload/format', at .

The XML file is like this:

<?xml version="1.0" encoding="utf-8" ?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <RECORD>
    <FIELD ID="1" xsi:type="CharTerm" TERMINATOR=";" MAX_LENGTH="32"/>
    <FIELD ID="2" xsi:type="CharTerm" TERMINATOR=";" MAX_LENGTH="4"/>
    <FIELD ID="3" xsi:type="CharTerm" TERMINATOR=";" MAX_LENGTH="4"/>
    <FIELD ID="4" xsi:type="CharTerm" TERMINATOR=";" MAX_LENGTH="20"/>
    <FIELD ID="5" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="16"/>
  </RECORD>
  <ROW>
    <COLUMN SOURCE="1" NAME="col1" xsi:type="SQLNVARCHAR"/>
    <COLUMN SOURCE="2" NAME="col2" xsi:type="SQLINT"/>
    <COLUMN SOURCE="3" NAME="col3" xsi:type="SQLINT"/>
    <COLUMN SOURCE="4" NAME="col4" xsi:type="SQLNVARCHAR"/>
    <COLUMN SOURCE="5" NAME="col5" xsi:type="SQLFLT4"/>
  </ROW>
</BCPFORMAT>

I read the XML file like this:

        FileStream fs = File.OpenRead(formatFileName);
        XmlSerializer serializer = new XmlSerializer(typeof(FormatSchemasXml.BCPFORMAT));
        FormatSchemasXml.BCPFORMAT bcp_format = (FormatSchemasXml.BCPFORMAT)serializer.Deserialize(fs);
        fs.Close();

The external xmlns reference does not seem to be used. I have searched a lot of documentation, but failed at seeing how I can fix this. Preferably without having to modify the BCP XML format files (I'd like to use them as is).

Suggestions?

0

There are 0 best solutions below