0

I have the following c# object:

class Modification {
    public string Name;
    public string Value;
}

I want to use the serializer to serialize my object the following way:

<name>value</name>

Example: Let's say we set those variables to

Name = "Autoroute"
Value = 53

I want the xml to look like:

<test>
    <Autoroute>53</Autoroute>
</test>

I saw somewhere that this feature is not supported by the serializer, but is there a way to overload the serializer to allow this kind of behavior ?

Changing the XML structure is not an option since it is already a convention.

2
  • you can do it without the serializer. create your own xml Commented Nov 27, 2014 at 14:45
  • I don't think it is possible, to me you want does not seem to make sense, how would the serialiser know that the value of the Value property relates to the Name property? Commented Nov 27, 2014 at 14:47

1 Answer 1

1

You can use IXmlSerializable to do this, though this doesn't give you control over the root element name - you have to set this in the serializer (which may present other challenges when you come to read it as part of a larger xml structure...).

public class Modification : IXmlSerializable
{
    public string Name;
    public string Value;

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        reader.ReadStartElement();
        Name = reader.Name;
        Value = reader.ReadElementContentAsString();
        reader.ReadEndElement();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteElementString(Name, Value);
    }
}

Usage,

Modification modification = new Modification()
{
    Name = "Autoroute",
    Value = "53"
};

Modification andBack = null;

string rootElement = "test";    
XmlSerializer s = new XmlSerializer(typeof(Modification), new XmlRootAttribute(rootElement));
using (StreamWriter writer = new StreamWriter(@"c:\temp\output.xml"))
    s.Serialize(writer, modification);

using (StreamReader reader = new StreamReader(@"c:\temp\output.xml"))
    andBack = s.Deserialize(reader) as Modification;

Console.WriteLine("{0}={1}", andBack.Name, andBack.Value);

The XML produced by this looks like this,

<?xml version="1.0" encoding="utf-8"?>
<test>
   <Autoroute>53</Autoroute>
</test>
Sign up to request clarification or add additional context in comments.

2 Comments

There's one thing that I left out, Autoroute would not be the root element, but simply added in the document. I guess that would mean that in WriteXml I would have write.WriteName(Name) and then writer.WriteString(Value) ?
Is there a reason why when I execute your code, I only get the first <test> not the closing tag ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.