views:

296

answers:

2

Duplicate:
Omitting all xml namespaces when serializing an object? Not the same.. I want in the other way: Deserialize!


I have a C# class as bellow:

[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.portalfiscal.inf.br/nfe")]
[System.Xml.Serialization.XmlRootAttribute("NFe", Namespace = "http://www.portalfiscal.inf.br/nfe", IsNullable = false)]
public partial class TNFe
{

    private TNFeInfNFe infNFeField;

    private SignatureType signatureField;

    /// <remarks/>
    public TNFeInfNFe infNFe
    { ...

I use this class to serialize/deserialize XML files by user request. But I've got a problem: namespaces definition were added on the new version of this software. The XML is still the same, only adding namespaces definition.

Eg., last version...

<?xml version="1.0" encoding="utf-8" ?>
  <NFe>
    <infNFe version="1.10">
      ...

and new version...

<?xml version="1.0" encoding="utf-8" ?> 
  <NFe xmlns="http://www.portalfiscal.inf.br/nfe"&gt;
    <infNFe version="2.10">
      ...

I need to load XML files with and without these namespaces. I have a lot of nested classes and each of it has its own namespaces definition.

I would like to use the same classes for both XML, with and without namespaces.

I tryied to create an XmlTextReader and overwrite the NamespaceURI method, but I still receives an exception with no much info. I think .NET engine is trying to force the class namespace definition against the XML.

Any help is welcome. Tks!

A: 

You need to implement IXmlSerializable to improve your custom serialization.

necrostaz
I have lots of nested classes. Would be terrible to implement it on each of these classes just to remove the namespaces!!!!I would have to add a XmlSchemaProvider for each class, returning different schemas depending on the version.Isn't there an easier way to do deserialize? Just like adding an empty namespace string to the serialize function?
Rodrigo Queiroz
A: 

You could read the file in as text, remove the offending namespace text, then deserialize it.

You may need to write the "good" text back into a [memory/string/etc] stream so that the XmlSerializer's Deserialize can be called.

TimW
Yes... this seems to be the "best" way. Tks!
Rodrigo Queiroz