【发布时间】: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