2

I need to deserialize this xml (that I can't change):

<foo:a xmlns:foo="http://example.com">
  <b>string</b>
</foo:a>

I made this class:

[DataContract(Name = "a", Namespace = "http://example.com")]
public class A
{
    [DataMember(Name = "b", Order = 0)]
    public string B;
}

And I did:

using (var streamObject = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
{
    var ser = new DataContractSerializer(typeof(A));
    return (A)ser.ReadObject(streamObject);
}

I get an object of class A, but the content of B is always null. I know it would work if the xml was using <foo:b>string</foo:b>, but that is not the case. What can I do to deserialize a DataMember with no namespace?

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Good one. Do you have to use DataContract serialization? – d_z Apr 18 '14 at 13:22
  • you can remove namespace at all `Namespace = ""` in your datacontract attribute. then you'll end up having and – Andrew Apr 18 '14 at 13:23
  • 1
    @Andrew, than the xml won't deserialize – d_z Apr 18 '14 at 13:24
  • sorry, I thought he uses this class to create the xml as well – Andrew Apr 18 '14 at 13:25
  • @d_z no, I don't have to... but it would save me some trouble (like: I did everything else for the app, including soap and json, with DataContractSerializer). I would dislike to mix the handling of this and json differently. – Cœur Apr 18 '14 at 14:08
  • @Cœur, Got it. I asked because XmlSerializer has this capability and DataContractSerializer doesn't – d_z Apr 18 '14 at 14:10

1 Answers1

2

if you can do the pre-processing of xml before deserializing it, then try to do the following:

make namespace in your datacontract empty:

[DataContract(Name = "a", Namespace = "")]
public class A
{
    [DataMember(Name = "b", Order = 0)]
    public string B;
}

remove the namespace attribute from your xml before deserializing it

XDocument doc = XDocument.Parse("your xml here");
XElement root = doc.Root;

XAttribute attr = root.Attribute("xmlns");
if (attr != null) 
{
    attr.Remove();
}

and then deserialize the updated xml:

string newXml = doc.ToString();   

A result = null;
DataContractSerializer serializer = new DataContractSerializer(typeof(A));

using (StringReader backing = new StringReader(newXml))
{
    using (XmlTextReader reader = new XmlTextReader(backing))
    {
        result = (A) serializer.ReadObject(reader);
    }
}
Andrew
  • 3,648
  • 1
  • 15
  • 29
  • Thanks, removing namespace altogether was my current workaround. I was using a Regexp.Replace(); I'll see if I can use XAttribute.Remove() as you suggest. But some issue with this solution is that I also need to hack the serialization to insert the namespace later on. – Cœur Apr 18 '14 at 13:58
  • why an XmlTextReader instead of a MemoryStream? Any link to why it would be better? – Cœur Apr 18 '14 at 13:59
  • to add namespace back, please have a look at the question here: http://stackoverflow.com/questions/11021931/how-do-i-set-namespace-attributes-on-an-xelement or http://msdn.microsoft.com/en-us/library/bb387075.aspx. You can use MemoryStream, I'm just used to work with XmlTextReader. – Andrew Apr 18 '14 at 14:13