在 C# 中为 WCF 服务反序列化简单 XML 数组对象

Deserialize simple XML array object in C# for WCF service

3 天来,我一直面临同样的问题,但我不知道自己做错了什么。

上下文:我正在为固定 XML 创建一个新的 WCF 服务。

问题:看起来 XML 的反序列化出错了。我确实取回了 data 对象,但没有填充 属性 items

到目前为止已尝试:

这是 xml 我要收到的:

<spml:data xmlns:spml="urn:oasis:names:tc:SPML:2:0">
    <attr name="mailone" xmlns="urn:oasis:names:tc:DSML:2:0:core">
        <value>xxxx@gmail.com</value>
    </attr>
    <attr name="mailtwo" xmlns="urn:oasis:names:tc:DSML:2:0:core">
        <value>xxxx@gmail.com</value>
    </attr>
    <attr name="mailthree" xmlns="urn:oasis:names:tc:DSML:2:0:core">
        <value>xxxx@gmail.com</value>
    </attr>
</spml:data>

使用 xsd(4.0 目前在 wcf 项目中用于其他对象)我从 xsd:

得到这个 c# class
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.33440.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:tc:SPML:2:0")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:oasis:names:tc:SPML:2:0", IsNullable = false)]
public partial class data
{
    private attr[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("attr")]
    public attr[] Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:tc:SPML:2:0")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:oasis:names:tc:SPML:2:0", IsNullable = false)]
public partial class attr
{

    private string valueField;

    private string nameField;

    /// <remarks/>
    public string value
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }
}

我写这个程序是为了简化映射问题:

using System;
using System.Xml;
using System.Xml.Serialization;

namespace XmlDeserializer
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlSerializer ser = new XmlSerializer(typeof(data));
            data data;

            using (XmlReader reader = XmlReader.Create(PATH))
            {
                data = (data)ser.Deserialize(reader);
            }
        }
    }
}

如果您 运行 此控制台应用程序与 C# class 在同一个项目中并调试数据,您将得到一个数据对象 items = null

有人能告诉我正确的方向吗?

编辑:它与名称空间有关:我删除了 XML 和 c# 对象中的所有名称空间并且它起作用了。

亲切的问候, 彼得

以下代码有效。我更改了名称空间:

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

namespace ConsoleApplication137
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(data));
            data d = (data)serializer.Deserialize(reader);
        }

    }
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:oasis:names:tc:SPML:2:0", IsNullable = false)]
    public partial class data
    {
        private attr[] itemsField;

        /// <remarks/>
        [XmlElement(ElementName = "attr", Namespace = "urn:oasis:names:tc:DSML:2:0:core")]
        public attr[] Items
        {
            get
            {
                return this.itemsField;
            }
            set
            {
                this.itemsField = value;
            }
        }
    }

    [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:oasis:names:tc:DSML:2:0:core", IsNullable = false)]
    public partial class attr
    {

        private string valueField;

        private string nameField;

        /// <remarks/>
        public string value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string name
        {
            get
            {
                return this.nameField;
            }
            set
            {
                this.nameField = value;
            }
        }
    }

}