views:

51

answers:

1

I extracted the following node from XmlReader:

string xml = "<FeatureType xmlns=\"http://www.opengis.net/wfs\" > </FeatureType>"

In order to deserialize to a predefined class, I attempted:

using (StringReader elementReader = new StringReader("<?xml version='1.0'?>" + xml ))
{
    // TODO: Can data contract serializer be used?
    XmlSerializer deserializer = serializers[typeof(FeatureType)];
    featureTypes.Add((FeatureType)deserializer.Deserialize(elementReader));
}

Upon deserialization, XmlSerializer throws an exception with the following message:

"<FeatureType xmlns='http://www.opengis.net/wfs'&gt; was not expected."

If I remove the namespace declaration, I can deserialize. Without having to further manipulate with the output of the reader, how do I fix this? Also, why is the reader injecting the namespace declaration, when it extracts each node?

TIA.

+1  A: 

Just make sure you use the default XML namespace when you construct your XmlSerializer for this class:

XmlSerializer deserializer = new XmlSerializer(typeof(FeatureType), 
                                               "http://www.opengis.net/wfs");

This is the constructor for XmlSerializer which takes an optional second parameter, defaultNamespace.

Using this approach, you can easily deserialize your XML string without any problem whatsoever.

marc_s
That was it, thank you.
e28Makaveli
+1 that was it, thanks...
AlexanderN