7

I'm trying to deserialize the following XML:

<?xml version="1.0" encoding="utf-8" ?> 
<mf:somedata xmlns:mf="urn:somedata">
    <CurrentAccount>
        <AccountType>test</AccountType>
        <Charge>
            <ChargeType>test</ChargeType>
        </Charge>
    </CurrentAccount>
    <CurrentAccount>
        <AccountType>test 2</AccountType>
        <Charge>
            <ChargeType>test 2</ChargeType>
        </Charge>
    </CurrentAccount>
</mf:somedata>

Using the following classes:

[XmlRoot("somedata", Namespace = "urn:somedata")]
public class MfCurrentAccounts
{
    [XmlElement("CurrentAccount")]
    public CurrentAccount[] CurrentAccounts { get; set; }
}

public class CurrentAccount
{
    public string AccountType { get; set; }

    [XmlElement("Charge")]
    public Charge[] Charges { get; set; }
}

public class Charge
{
    public string ChargeType { get; set; }
}

var c = new XmlSerializer(typeof(MfCurrentAccounts)).Deserialize(new StringReader(xml)) as MfCurrentAccounts;

c.CurrentAccounts // <-- is always null

But no matter what I try, the CurrentAccounts array is null when I deserialize it. I've tried every combination I can think of with the attributes (I've tried XmlArray and XmlArrayItem too).

What am I doing wrong? :S

Tom
  • 1,561
  • 4
  • 20
  • 29
  • Sorry, updated the question with the deserialization code. – Tom Feb 08 '13 at 19:38
  • Do you not need to mark the classes with `[Serializable()]`? – Nope Feb 08 '13 at 19:38
  • @François I have tried that, but apparently it's not needed. – Tom Feb 08 '13 at 19:39
  • possible duplicate of [Deserializing into a List without a container element in XML](http://stackoverflow.com/questions/5271442/deserializing-into-a-list-without-a-container-element-in-xml) – Sam Axe Feb 08 '13 at 19:46
  • @Dan-o How can that be a duplicate when *this* code already does as that answer suggests? The only difference from the annotation/property with *this* code to the "duplicate answer" is that it uses an Array instead of a List. –  Feb 08 '13 at 20:00

2 Answers2

3

The issue is with Namespaces.

When I created that entire class setup in a test situation, i got a very different looking output. here is what I think you should be trying to read in:

    <?xml version="1.0"?>
    <mf:somedata xmlns:mf="urn:somedata">
        <mf:CurrentAccount>
            <mf:AccountType>something 1</mf:AccountType>
            <mf:Charge>
                <mf:ChargeType>Charge Type 1</mf:ChargeType>
            </mf:Charge>
        </mf:CurrentAccount>
        <mf:CurrentAccount>
            <mf:AccountType>something 2</mf:AccountType>
            <mf:Charge>
                <mf:ChargeType>Charge Type 2</mf:ChargeType>
            </mf:Charge>
        </mf:CurrentAccount>
    </mf:somedata>

Notice all the extra mf:. When you declare the namespace, the serializer will work with that and only de-serialize nodes that properly belong to that namespace. You either need to get rid of it entirely or fix your input appropriately. Here is the code that I used to generate that output note: the class definitions are completely unchanged

XmlSerializer ser = new XmlSerializer(typeof(MfCurrentAccounts));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("mf", "urn:somedata");

MemoryStream ms = new MemoryStream();
ser.Serialize(ms, a, ns);

and when reading it back in:

ms.Position = 0;
b = ser.Deserialize(ms) as MfCurrentAccounts;

after running both sections, b is now a perfect clone of a, and the xml I showed above is the generated xml.

Nevyn
  • 2,623
  • 4
  • 18
  • 32
  • Thanks for that. Unfortunately the XML is coming from a third party, so I have no control over it. I tried adding this to the CurrentAccounts property: `[XmlElement("CurrentAccount", Namespace = "urn:somedata")]` but to no avail. – Tom Feb 08 '13 at 21:34
  • Can you edit the xml before attempting to deserialize it? If you use some Regex on that first line and eliminate the "mf:" from the top node, then you will have a uniform classdef and it should deserialize properly... – Nevyn Feb 08 '13 at 22:49
  • 3
    @Tom have you tried [XmlElement("CurrentAccount", Namespace = String.Empty)] ? – jbl Feb 08 '13 at 22:50
  • @jbl You saved another life Namespace = "", worked!! – GMD Sep 18 '20 at 12:17
0

Maybe you should try replacing your MfCurrentAccounts class with :

[XmlRoot("somedata", Namespace = "urn:somedata")]
public class MfCurrentAccounts : List<CurrentAccount>
{
   public MfCurrentAccounts():base()
   {}

}

or give a look at https://stackoverflow.com/a/364410/1236044

Hope this will help

Community
  • 1
  • 1
jbl
  • 15,179
  • 3
  • 34
  • 101
  • That just returns an empty list. :( I'll play around with it though, as it seems like it should work. I'll have a look at writing to disk and using an XSD file, but I'd rather not go down that route. – Tom Feb 08 '13 at 21:45
  • @Tom the class might need a public constructor. I updated my answer – jbl Feb 08 '13 at 21:55
  • That didn't seem to work either, but your comment on the previous answer works. :) – Tom Feb 09 '13 at 10:17