【问题标题】:Deserialize simple XML array object in C# for WCF service在 C# 中为 WCF 服务反序列化简单的 XML 数组对象
【发布时间】:2020-02-17 08:14:19
【问题描述】:

3 天以来,我一直面临同样的问题,但我无法弄清楚我做错了什么。

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

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

到目前为止尝试过:

  • 使用 xsd 4.0(目前在 WCF 项目中使用)和 4.7 创建 C# 类。
  • 添加多个属性,例如:

    [ServiceContract(Namespace = "urn:oasis:names:tc:SPML:2:0"), XmlSerializerFormat].
    [System.Xml.Serialization.XmlArrayItemAttribute("attr", IsNullable = false)] 
    

    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(当前在 wcf 项目中用于其他对象的 4.0)我从 xsd 获得了这个 c# 类:

//------------------------------------------------------------------------------
// <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# 类运行此控制台应用程序并调试数据,您将获得一个带有 items = null 的数据对象。

有人可以让我朝着正确的方向前进吗?

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

亲切的问候, 彼得

【问题讨论】:

    标签: c# xml wcf xsd deserialization


    【解决方案1】:

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

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

    【讨论】:

    • 我刚刚对此进行了测试,它确实有效。令我惊讶的是,我使用visualstudio中的特殊粘贴将我的xml转换为类,它给了我一个同样有效的类。 xsd 工具出了点问题。我学到的东西:我完全信任 xsd 生成的文件,并不认为这是一个错误。下次我会更挑剔...谢谢你帮助我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-18
    相关资源
    最近更新 更多