Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WebAPI列表<;T>;序列化XML/JSON输出_C#_Xml_Json_Serialization_Asp.net Web Api - Fatal编程技术网

C# WebAPI列表<;T>;序列化XML/JSON输出

C# WebAPI列表<;T>;序列化XML/JSON输出,c#,xml,json,serialization,asp.net-web-api,C#,Xml,Json,Serialization,Asp.net Web Api,我试图在ApiController中创建一个如下所示的方法: public DemoList<Demo> GetAll() { var result = new DemoList<Demo>() { new Demo(){Y=2}, new Demo(), new Demo(){Y=1} }; result.Name = "Test"; return result; } public interface INamedEnumerable<o

我试图在ApiController中创建一个如下所示的方法:

public DemoList<Demo> GetAll()
{
    var result = new DemoList<Demo>() { new Demo(){Y=2}, new Demo(), new Demo(){Y=1} };
    result.Name = "Test";
    return result;
}
public interface INamedEnumerable<out T> : IEnumerable<T>
{
    string Name { get; set; }
}

public class Demo
{
    public int X { get { return 3; } }
    public int Y { get; set; }
}

public class DemoList<T> : List<T>, INamedEnumerable<T>
{
    public DemoList()
    {
    }

    public string Name { get; set; } 
}
并得到以下结果:

XML(接受标题设置为application/XML)

201

JSON(接受头设置为application/JSON)

[{X:3,Y:2},{X:3,Y:0},{X:3,Y:1}]

我的问题很简单:为什么X变量没有用XML版本序列化(我以为只读属性是序列化的)更重要的是,为什么在这两种情况下
Name
属性(可写)都没有序列化??
有什么办法可以让这项工作达到我的预期

编辑:
请注意,我处于WebAPI环境中!默认情况下,XmlSerializer自动设置为XmlMediaTypeFormatter,JSONSerializer自动设置为JsonMediaTypeFormatter。XML序列化程序只允许对提供了“set”的属性进行序列化。

您使用什么来序列化它?如果您不需要属性,可以使用前面提到的DataContractSerializer。默认情况下,不带集合的属性不会序列化,但是使用DataContractSerializer或实现IXmlSerializable应该可以实现这一点

using System;
using System.Runtime.Serialization;
using System.Xml;
[DataContract]
class MyObject {
    public MyObject(Guid id) { this.id = id; }
    [DataMember(Name="Id")]
    private Guid id;
    public Guid Id { get {return id;}}
}
static class Program {
    static void Main() {
        var ser = new DataContractSerializer(typeof(MyObject));
        var obj = new MyObject(Guid.NewGuid());
        using(XmlWriter xw = XmlWriter.Create(Console.Out)) {
            ser.WriteObject(xw, obj);
        }
    }
}

这似乎是一个错误。。。使用以下解决方法实现了这一技巧:

public class ListWrapper<T>
{
    public ListWrapper(INamedEnumerable<T> list)
    {
        List = new List<T>(list);
        Name = list.Name;
    }

    public List<T> List { get; set; }

    public string Name { get; set; }
}
公共类ListWrapper
{
公共ListWrapper(INamedEnumerable列表)
{
列表=新列表(列表);
Name=list.Name;
}
公共列表{get;set;}
公共字符串名称{get;set;}
}

那么为什么
Name
没有序列化?顺便说一下,请参见我的编辑,我没有使用此序列化程序
public class ListWrapper<T>
{
    public ListWrapper(INamedEnumerable<T> list)
    {
        List = new List<T>(list);
        Name = list.Name;
    }

    public List<T> List { get; set; }

    public string Name { get; set; }
}