2

I am wondering if there is a way to get XmlSerializer in C# to change what it outputs for one property of one object. For example, if I have something like:

public class MyClass
{
    public string prop1{get;}
    public uint prop2{get;}
    public MyClass2 class2{get;}
    public MyClass3 class3{get;}
}

public class MyClass2
{
    public string prop3{get;}
}

public class MyClass3
{
    //Lots of properties that I want to serialize as normal
}

Now in somewhere in my code, I have something like this:

private void SerializeStuff(List<MyClass> list, string path)
{
    XmlSerializer serialize = new XmlSerializer(typeof(List<MyClass>));
    TextWriter writer = new StreamWriter(path);
    serialize.Serialize(writer, list);
    writer.Close();
}

What I want is for the serialization to work as normal, but with prop3 replaced with some other stuff. Example:

<MyClass>
    <prop1>whatever</prop1>
    <prop2>345</prop2>
    <class2>
        <somethingNotProp3>whatever</somethingNotProp3>
        <somethingElseNotProp3>whatever</somethingElseNotProp3>
    </class2>
    <class3>
        ...
    </class3>
</MyClass>

Is there a way to customize XmlSerializer so I don't have to write the entire Xml file manually, or is there no way to do that?

Edit: I am pretty sure that the solution could have something to do with implementing ISerializable's GetObjectData method; however, I am not exactly sure how to implement it. I tried making MyClass2 inherit from ISerializable and implementing GetObjectData, but nothing changed. prop3 was still output in the XML file.

Daniel
  • 6,595
  • 9
  • 38
  • 70
  • Yes you can, but what do you want to change? Just the name of the prop in the xml? – Matt Sep 02 '11 at 16:18
  • @Matt: The name of the property and also the value. (I mean that the string `"whatever"` is not the value of any property in `class2`. – Daniel Sep 02 '11 at 16:20

1 Answers1

3

Use the attributes in the System.Xml.Serialization namespace to affect the way that the instance is serialized.

If you need very specialized serialization for MyClass3 that the attributes cannot handle, then implement the IXmlSerializable interface which will give you complete control over the serialization process (you can even decide on an instance-by-instance basis how to serialize the content/property).

Based on the comments, it would seem that you want to implement IXmlSerializable, as you want to change the name of the property and the value; the attributes will let you change the name of the property/element/attribute, but not allow you to perform transformations on the value (and I assume you don't want to corrupt your representation and add an extra property/value for this purpose).

casperOne
  • 73,706
  • 19
  • 184
  • 253
  • I believe that you are right about that; however, I cannot find the documentation on MSDN about how to change the element name _and_ value. – Daniel Sep 02 '11 at 16:56
  • Ah I get it now. I didn't understand how the WriteXml `method` worked. This has been very helpful. Thank you. – Daniel Sep 02 '11 at 17:02
  • Is there a way so that if I do something like `writer.WriteStartElement("element"); writer.WriteEndElement();`, it will output `` instead of ``? Edit: Never mind, MSDN says there's no way to do that. – Daniel Sep 02 '11 at 18:56