c#serialise model to objectContent

时间:2017-04-28 06:42:02

标签: c# xml

我想要序列化以下课程:

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();   
    }
}

我希望输出看起来像这样:

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

我使用以下代码序列化对象:

    [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();
    }
}

然而,序列化的输出不能产生预期的结果。 xml包含在具有对象类名的元素中。 如何使用ObjectContent类获得所需的xml输出?

请注意,代码需要引用System.Net.Http.Formatting才能运行。

2 个答案:

答案 0 :(得分:1)

我不确定这两种方式是否兼容,但试试这个:

[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();   
    }
}

答案 1 :(得分:-1)

简单使用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)
            });


        }
    }
}

enter image description here