简体   繁体   中英

c# serialise model to objectContent

I have the following class I want to serialise:

public class UpdateDoorCommand : IXmlSerializable
{
    // string such as D1
    public string DoorId { get; }
    public string Name { get; }
    public string Notes { get; }

    public UpdateDoorCommand(string doorId, string name, string notes)
    {
        DoorId = doorId;
        Name = name;
        Notes = notes;
    }
    public UpdateDoorCommand()
    {

    }

    public XmlSchema GetSchema()
    {
        throw new NotImplementedException();
    }

    public void ReadXml(XmlReader reader)
    {
        throw new NotImplementedException();
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteStartElement("Door");
        writer.WriteAttributeString("Address", "D1");
        writer.WriteElementString("Name", Name);
        writer.WriteElementString("Notes", Notes);
        writer.WriteEndElement();   
    }
}

I want the output to look like this:

  <Door Address="D1">
    <Name>Name1</Name>
    <Notes>Notes1</Notes>
  </Door>

I use the following code to serialise the object:

    [TestMethod]
    public async Task XmlSerialisationTest()
    {
        var model = new UpdateDoorCommand("D1", "Name1", "Notes1");
        var mediaTypeFormatters = new MediaTypeFormatterCollection();
        mediaTypeFormatters.XmlFormatter.UseXmlSerializer = true;
        mediaTypeFormatters.XmlFormatter.WriterSettings.OmitXmlDeclaration = true;
        var content = new ObjectContent<UpdateDoorCommand>(model, mediaTypeFormatters.XmlFormatter);
        // this does not look like the type 
        var str = await content.ReadAsStringAsync();
    }
}

However the the output of the serialisation does not give the desired results. The xml is wrapped in an element with the classname of the object. How can I get desired xml output using the ObjectContent class?

Note that the code needs a reference to System.Net.Http.Formatting in order to run.

I'm not sure if the two ways are compatible but try this:

[XmlRoot(ElementName = "Door", DataType = "string")]
public class UpdateDoorCommand : IXmlSerializable
{
    // *snip*

    public void WriteXml(XmlWriter writer)
    {
        //writer.WriteStartElement("Door");
        writer.WriteAttributeString("Address", "D1");
        writer.WriteElementString("Name", Name);
        writer.WriteElementString("Notes", Notes);
        //writer.WriteEndElement();   
    }
}

Simple with Xml Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            UpdateDoorCommand updateDoorCommand = new UpdateDoorCommand("D1","Name1","Note1");
            updateDoorCommand.WriteXml();
        }
    }
    public class UpdateDoorCommand
    {
        // string such as D1
        public string DoorId { get; set; }
        public string Name { get; set; }
        public string Notes { get; set;  }

        public UpdateDoorCommand(string doorId, string name, string notes)
        {
            DoorId = doorId;
            Name = name;
            Notes = notes;
        }
        public UpdateDoorCommand()
        {

        }

        public void ReadXml(XmlReader reader)
        {
            throw new NotImplementedException();
        }

        public void WriteXml()
        {
            XElement doorCommand = new XElement("Door", new object[] {
                new XAttribute("Address", DoorId),
                new XElement("Name", Name),
                new XElement("Notes1", Notes)
            });


        }
    }
}

在此处输入图片说明

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