views:

30

answers:

2

Hello,

I need to support XML deserialization of two very similar, yet different xml files.

File 1:

<?xml version="1.0" encoding="UTF-8"?>
<lr:LogReport xmlns:dcml="http://www.x1.org/schemas/Types/"
    xmlns:ds="http://www.x3.org/2000/09/xmldsig#"
    xmlns:lr="http://www.x.org/schemas/LogRecord/"
    xmlns:xs="http://www.x3.org/2001/XMLSchema"
    xmlns:xsi="http://www.x3.org/2001/XMLSchema-instance"&gt;
    <lr:reportDate>2010-03-05T07:00:52-08:00</lr:reportDate>

File 2:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<LogReport xmlns="http://www.x.org/schemas/LogRecord" 
    xmlns:dcml="http://www.x1.org/schemas/Types" 
    xmlns:ds="http://www.x3.org/2000/09/xmldsig#" 
    xmlns:lr="http://www.x.org/schemas/LogRecord" 
    xmlns:xs="http://www.x3.org/2001/XMLSchema" 
    xmlns:xsi="http://www.x3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.x.org/schemas/LogRecord ./LogRecord.xsd  http://www.x1.org/schemas/Types ./Types.xsd">
    <lr:reportDate>2010-02-26T07:00:02-08:00</lr:reportDate>

Class Definition:

<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42"), _
 System.SerializableAttribute(), _
 System.Diagnostics.DebuggerStepThroughAttribute(), _
 System.ComponentModel.DesignerCategoryAttribute("code"), _
 System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://www.smpte-ra.org/schemas/430-4/2008/LogRecord"), _
 System.Xml.Serialization.XmlRootAttribute("LogReport", [Namespace]:="http://www.smpte-ra.org/schemas/430-4/2008/LogRecord", IsNullable:=False)> _
Partial Public Class LogReport

Files matching File1 fail while files matching File2 succeed. The key difference is the trailing slash two of the namespace definitions.

Code Sample:

Dim oLogReport As New LogReport
Dim oType As System.Type = oLogReport.GetType
Dim oReader As System.Xml.XmlReader = Nothing
Dim oSerializer As New XmlSerializer(oType)
oReader = System.Xml.XmlReader.Create(sFileName)
oLogReport = CType(oSerializer.Deserialize(oReader), LogReport)

Error from File1:

{"<LogReport xmlns='http://www.x.org/schemas/LogRecord/'&gt; was not expected."}

Already tried: http://stackoverflow.com/questions/1254544/how-do-i-specify-xml-serialization-attributes-to-support-namespace-prefixes-durin

These files come from a third-party so I cannot change the serialization process. Is there a way I can support both xml formats? Do I need two separate classes?

Thanks!

+1  A: 

Two separate class models is an option, although admittedly not a very good one.

Do you actually get these files as XML that you then process in code, or are you using smoe sort of proxy that's supposed to auto-deserialize the XML?

If you're doing your own deserialization, I would consider creating a simple XSL Transform to convert the namespaces in-memory before executing the deserialization. A transform to simply replace one pair of namespaces with another pair will be pretty short and simple.

Toby
I'm doing the deserialization. I'll look into an XSL Transfrom. Let me know if you know of a good link/sample.
MattH
A: 

If the namespaces are different, then the XML is different.

John Saunders