XML serialization results in duplicate nodes

I have a structure of the object, which I'm trying to convert to the xml, which results in duplicated level node. I'm pretty sure this has something to do with the subclass because I had to implement my deserialization, but I'm not sure what is going on in the other direction. The same xml structure is used as input when deserializing my data when the application starts, since it will be saved later when reinitializing.

Here, it looks like an erroneous conclusion xml:

<Keyboarding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Exercises>
    <Exercise>
      <Exercise Id="3" ActivityNumber="5" SubActivityNumber="b" Type="Standard">
        <Title>Test Title</Title>
        <Instructions>Downloaded Update Instructions</Instructions>
      </Exercise>
    </Exercise>
  </Exercises>
</Keyboarding>

Here it should look (and looks like the initial deserialization):

<Keyboarding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Exercises>
      <Exercise Id="3" ActivityNumber="5" SubActivityNumber="b" Type="Standard">
        <Title>Test Title</Title>
        <Instructions>Downloaded Update Instructions</Instructions>
      </Exercise>
  </Exercises>
</Keyboarding>

. - , Type. XML- , , .

, XML-:

[XmlArray("Exercises")]
[XmlArrayItem("Exercise", Type = typeof (ExerciseXmlSerializer<Exercise>))]
public Collection<Exercise> UnprocessedExercises { get; set; }

:

[Serializable]
[XmlType(AnonymousType = true)]
public class Exercise

:

[Serializable]
[XmlType(AnonymousType = true)]
[XmlRoot(ElementName = "Exercise", IsNullable = false)]
public class StandardExercise : Exercise

XML-:

public class ExerciseXmlSerializer<T> : IXmlSerializable where T : class
{
    private T _data;
    ...
    public void WriteXml(XmlWriter writer)
    {
        Type type = _data.GetType();

        new XmlSerializer(type).Serialize(writer, _data);
    }
    ...
}

WriteXml() , node ? ?

+5
2

, :

[XmlArrayItem("Exercise", Type = typeof (ExerciseXmlSerializer<Exercise>))]

, , . IXmlSerializable, , .

:

public class Keyboarding
{
    [XmlArray("Exercises")]
    [XmlArrayItem("Exercise")]
    public Collection<Exercise> UnprocessedExercises { get; set; }

    public Keyboarding()
    {
        UnprocessedExercises = new Collection<Exercise>();
        UnprocessedExercises.Add(new StandardExercise());
    }
}

[XmlInclude(typeof(StandardExercise))]
public class Exercise
{
}
[Serializable]
public class StandardExercise : Exercise
{
}

, :

<?xml version=\"1.0\"?>
<Keyboarding xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
  <Exercises>
    <Exercise xsi:type=\"StandardExercise\" />
  </Exercises>
</Keyboarding>

, xsi:type , .

+1

XmlAttributeOverrides, , . , , - . , , , , .

List<Type> extraTypes = new List<Type>();
XmlAttributes attrs = new XmlAttributes();

extraTypes.Add(typeof(Exercise));
extraTypes.Add(typeof(StandardExercise));

XmlElementAttribute attr = new XmlElementAttribute
{
    ElementName = "Exercise",
    Type = typeof(Exercise)
};
attrs.XmlElements.Add(attr);

attr = new XmlElementAttribute
{
    ElementName = "StandardExercise",
    Type = typeof(StandardExercise)
};
attrs.XmlElements.Add(attr);

XmlAttributeOverrides overrides = new XmlAttributeOverrides();
overrides.Add(typeof(Keyboarding), "Keboarding", attrs);

return new XmlSerializer(typeof(Keyboarding), overrides, extraTypes.ToArray(),
    new XmlRootAttribute("Keyboarding"), string.Empty);

XML, . , , .

<Keyboarding xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Exercises>
        <StandardExercise Id="3" ActivityNumber="5" SubActivityNumber="b">
            <Title>Test Title</Title>
            <Instructions>Downloaded Update Instructions</Instructions>
        </StandardExercise >
    </Exercises>
</Keyboarding>
0

All Articles