views:

2148

answers:

3

Can I make XmlSerializer ignore the namespace (xmlns attribute) on deserialization so that it doesn't matter if the attribute is added or not or even if the attribute is bogus? I know that the source will always be trusted so I don't care about the xmlns attribute.

A: 

Why try to make the XmlSerializer forget how XML works? It's a fact of XML that two elements with the same name but different namespaces are different elements.

If you want to process XML that has no namespaces, then you should pre-process it to remove the namespaces, and then pass it to the serializer.

John Saunders
+4  A: 

Yes, you can tell the XmlSerializer to ignore namespaces during de-serialization.

Define an XmlTextReader that ignores namespaces. Like so:

// helper class to ignore namespaces when de-serializing
public class NamespaceIgnorantXmlTextReader : XmlTextReader
{
    public NamespaceIgnorantXmlTextReader(System.IO.TextReader reader): base(reader) { }

    public override string NamespaceURI
    {
        get { return ""; }
    }
}

// helper class to omit XML decl at start of document when serializing
public class XTWFND  : XmlTextWriter {
    public XTWFND (System.IO.TextWriter w) : base(w) { Formatting= System.Xml.Formatting.Indented;}
    public override void WriteStartDocument () { }
}

Here's an example of how you would de-serialize using that TextReader:

public class MyType1 
{
    public string Label
    {
        set {  _Label= value; } 
        get { return _Label; } 
    }

    private int _Epoch;
    public int Epoch
    {
        set {  _Epoch= value; } 
        get { return _Epoch; } 
    }        
}



    String RawXml_WithNamespaces = @"
      <MyType1 xmlns='urn:booboo-dee-doo'>
        <Label>This document has namespaces on its elements</Label>
        <Epoch xmlns='urn:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'>0</Epoch>
      </MyType1>";


    System.IO.StringReader sr;
    sr= new System.IO.StringReader(RawXml_WithNamespaces);
    var o1= (MyType1) s1.Deserialize(new NamespaceIgnorantXmlTextReader(sr));
    System.Console.WriteLine("\n\nDe-serialized, then serialized again:\n");
    s1.Serialize(new XTWFND(System.Console.Out), o1, ns);
    Console.WriteLine("\n\n");

The result is like so:

    <MyType1>
      <Label>This document has namespaces on its elements</Label>
      <Epoch>0</Epoch>
    </MyType1>
Cheeso
Note this is the kind of thing I meant. You are not telling the XmlSerializer to ignore namespaces - you are giving it XML that has no namespaces.
John Saunders
??? I don't understand. I gave it XML that had namespaces on the elements. The code de-serialized (read) the XML document and ignored the namespaces.
Cheeso
A: 

be careful, if you overwrite NamespaceURI it not only affects all of the elements but attributes as well. Sometimes that causes deserializer to ignore attributes which will set them all to null.

Stan R.
obviously there are pitfalls of ignoring namespaces. You could say the same thing about the elements - if the deserializer is expecting a namespace-qualified element, and finds an element in no namespace, then it will not set the associated property in the deserialized instance. Those are just the rules of the road. It worked in the example I gave because all elements had no namespace.
Cheeso