I am trying to produce a generic "audit report" for
various transactions in our application. Each transaction
type defines a document (and has an associated schema).
When a transaction leads to a "write to the database", we
store the dataset's diffgram in the audit table. To
render the audit report we load the diffgram into a
generic DataSet and bind it to a grid. All works well
unless the document's schema had an <xs:import> to bring
in common GlobalTypes.
The orignal code used an XmlTextReader. I thought that we
could use an XmlValidatingReader and exploit its
XmlSchemaCollection property. In the code below
everything is a local file just to keep the verification
of the concept simple. But the wheels seem to fall off
when I used the XmlValidatingReader. For XML files without
<import>s the resulting DataSet object, dsResults, is
Nothing. For files with <import>s deserialization fails
with a schema error (types defined in the import are not
being found -- suggesting that the schema cache does
nothing for the <import>)
Note that experiments have shown xsc.Add() is finding the
refernced schema (misname the file or put an error in the
file and Add() throws an exception). I later desparately
threw in the XmlUriResolver, to no effect.
Any suggestions?
dsResults.Clear()
' A FileStream is needed to read the XML document.
Dim filename As String = "SmfDg.xml"
Dim fs As New FileStream(filename, FileMode.Open)
Dim xtr As New XmlTextReader(fs)
Try
Dim vxtr As XmlValidatingReader = New
XmlValidatingReader(xtr)
vxtr.ValidationType = ValidationType.None
Dim xsc As New XmlSchemaCollection
xsc.Add
("http://vanguard.com/fima2/TransactionTypes.xsd", "C:\\Fim
a2\\Solutions\\Projects\\Common\\DataSets\\Transac tions\\"
& "TransactionTypes.xsd")
xsc.Add
("http://vanguard.com/fima2/SmfDataSet.xsd", "C:\\Fima2
\\Solutions\\Projects\\Common\\DataSets\\Smf\\"
& "SMFDataSet.xsd")
vxtr.Schemas.Add(xsc)
Dim ser As XmlSerializer = New XmlSerializer
(GetType(DataSet))
'Your results may vary between xtr and vxtr
dsResults = CType(ser.Deserialize(vxtr),
DataSet)
dgAuditDetail.DataSource = dsResults
dgAuditDetail.DataMember = dsResults.Tables
(0).TableName
Catch ex As Exception
Trace.WriteLine(ex.ToString)
Finally
fs.Close()
End Try