简体   繁体   中英

Customizing xml output with IXmlSerializable

What I am trying to achieve seems fairly simple.

Given the class

public class Wrapper<T> { 
   T Data { get; set; }
   bool Success { get;set; }
   List<Error> Errors { get; set; }
}

I simply want to have the XML output for the Data parameter wrapped in a <Data></Data> tag.

IE: Desired output:

<Wrapper>
    <Data>
       <Person first="Bob" last="Robertson"/>
    </Data>
   <Errors/>
    <Success>true</Success>
</Wrapper

Actual output:

<Wrapper>
    <Person first="Bob" last="Robertson"/>
    <Errors/>
    <Success>true</Success>
</Wrapper>

Is there a simple implementation of IXmlSerializable where I can leave the serialization alone for the most part and just wrap the one member?

Edit: This is in a WCF service so I don't (as far as I know) have access to the creation of the XmlSerializer.

What I ended up doing:

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteElementString("Success", Success.ToString());
        writer.WriteStartElement("Errors");

        foreach (var error in Errors)
        {
            Util.XmlSerialize(error, writer);
        }

        writer.WriteStartElement("Data");
        Util.XmlSerialize(this.Data, writer);
        writer.WriteEndElement();
    }

-

public class Util
{
    public static void XmlSerialize<T>(T obj, XmlWriter writer)
    {
        var nsSerializer = new XmlSerializerNamespaces();
        nsSerializer.Add("", "");

        var ser = new XmlSerializer(typeof (T));
        ser.Serialize(writer, obj, nsSerializer);
    }
}

You can use XmlOverrides:

var xmlOverrides = new XmlAttributeOverrides();
var attributes = new XmlAttributes();
attributes.XmlElements
     .Add(new XmlElementAttribute("Person", typeof (Person)));
xmlOverrides.Add(typeof(Wrapper<Person>), "Data", attributes);

var serializer = new XmlSerializer(typeof(Wrapper<ExampleObject>), xmlOverrides);

Schema

public class Wrapper<T>
{
    public T Data { get; set; }
    public bool Success { get; set; }
}


public class Person
{
    public string first;
    public string last;
}

XML Serialization

        Wrapper<Person> f = new Wrapper<Person>();
        f.Data = new Person();
        f.Data.first = "Bob";
        f.Data.last = "Robertson";
        XmlSerializer SerializerObj = new XmlSerializer(typeof(Wrapper<Person>));
        TextWriter WriteFileStream = new StreamWriter(@"C:\test.xml");
        SerializerObj.Serialize(WriteFileStream, f);
        WriteFileStream.Close();

XML Output

<?xml version="1.0" encoding="utf-8"?>
<WrapperOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Data>
    <first>Bob</first>
    <last>Robertson</last>
  </Data>
  <Success>false</Success>
</WrapperOfPerson>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM