为什么 DataContractSerializer 生成一个名为“z:anytype”的节点?

发布于 2024-11-25 15:14:54 字数 159 浏览 1 评论 0原文

当使用 DataContractSerializer 时,我得到了很多我并不真正想要使用的命名空间的额外属性。我在不同的项目中生成了类似的数据结构,没有所有额外的标记,所以我知道它是可能的。

为什么 DataContractSerializer 生成一个名为“z:anytype”的节点?

When working with the DataContractSerializer I am getting alot of extra attributes for namespaces that I dont really want to work with. I have generated a similar data structure in a different project without all the extra markup so I know its possible.

Why does DataContractSerializer Generate a node with the name : "z:anytype" ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿咻 2024-12-02 15:14:54

我的问题是我将接口作为类型传递给我的 DataContractSerializer。

它很容易被错过,因为我的界面被命名为 IInvestment。

如果您遇到此问题,请检查您传递给 DataContractSerializer 构造函数的类型。

My problem was that I was passing an interface as a type to my DataContractSerializer.

It was easy to miss because my interface was named IInvestment.

If you ever have this issue check the type that you are passing to the DataContractSerializer Constructor.

满地尘埃落定 2024-12-02 15:14:54

如果您要序列化 ​​object 的某些集合(数组、列表、字典等),您就会遇到这种情况 - anyType 实际上意味着任何类型都可以存在(尽管您有一个指定实际类型的属性)为值)。在下面的例子中它表明了这一点。

public class StackOverflow_6780831
{
    [DataContract(Name = "Order")]
    public class Order
    {
        [DataMember(Order = 1)]
        public int Id;
        [DataMember(Order = 2)]
        public List<object> List;
    }
    public static void Test()
    {
        MemoryStream ms = new MemoryStream();
        Order order = new Order
        {
            Id = 1,
            List = new List<object>
            {
                1, "some string", DateTime.Now
            },
        };
        DataContractSerializer dcs = new DataContractSerializer(typeof(Order));
        XmlWriter w = XmlWriter.Create(ms, new XmlWriterSettings
        {
            Indent = true,
            IndentChars = "  ",
            OmitXmlDeclaration = true
        });
        dcs.WriteObject(w, order);
        w.Flush();
        Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
    }
}

You'll have that if you're serializing some collection (array, list, dictionary, etc) of object - anyType really means that any type could be there (although you have an attribute specifying the actual type for the value). In the example below it shows that.

public class StackOverflow_6780831
{
    [DataContract(Name = "Order")]
    public class Order
    {
        [DataMember(Order = 1)]
        public int Id;
        [DataMember(Order = 2)]
        public List<object> List;
    }
    public static void Test()
    {
        MemoryStream ms = new MemoryStream();
        Order order = new Order
        {
            Id = 1,
            List = new List<object>
            {
                1, "some string", DateTime.Now
            },
        };
        DataContractSerializer dcs = new DataContractSerializer(typeof(Order));
        XmlWriter w = XmlWriter.Create(ms, new XmlWriterSettings
        {
            Indent = true,
            IndentChars = "  ",
            OmitXmlDeclaration = true
        });
        dcs.WriteObject(w, order);
        w.Flush();
        Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文