【问题标题】:Empty array of object after serializing XML response序列化 XML 响应后的空对象数组
【发布时间】:2020-09-08 08:55:25
【问题描述】:

我有一个 xml 响应作为 wcf 肥皂网络服务,如下所示:

<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/">
   <env:Header/>
   <env:Body>
      <ws:getSubjectFieldsByNameResponse xmlns:ws="https://localhost:8443/ira/ws">
         <return xmlns:ns2="https://localhost:8443/ira/ws">
            <item>
               <subjectDnField>DN_PKIX_COMMONNAME</subjectDnField>
               <defaultValue/>
               <isRequired>true</isRequired>
               <isModifiable>true</isModifiable>
            </item>
            <item>
               <subjectDnField>DN_PKIX_SERIALNUMBER</subjectDnField>
               <defaultValue/>
               <isRequired>true</isRequired>
               <isModifiable>true</isModifiable>
            </item>
         </return>
      </ws:getSubjectFieldsByNameResponse>
   </env:Body>
</env:Envelope>  

为了解析这个 xml 文件的主体,我使用了下面的响应类:

[MessageContractAttribute(WrapperName = "getSubjectFieldsByNameResponse", WrapperNamespace = "https://localhost:8443/ira/ws", IsWrapped = true)]
public partial class getSubjectFieldsByNameResponse
{

    [MessageBodyMemberAttribute(Namespace = "", Order = 0)]
    [XmlArrayItemAttribute("return", Namespace = "", Form = XmlSchemaForm.Unqualified, IsNullable = false)]
    public SubjectItem[] items;

    public getSubjectFieldsByNameResponse()
    {
    }

    public getSubjectFieldsByNameResponse(SubjectItem[] items)
    {
        this.items = items;
    }
}

[XmlTypeAttribute]
public partial class SubjectItem
{
    [XmlElementAttribute(Form = XmlSchemaForm.Unqualified, Order = 0)]
    public string subjectDnField { get; set; }

    [XmlElementAttribute(Form = XmlSchemaForm.Unqualified, Order = 1)]
    public string defaultValue { get; set; }

    [XmlElementAttribute(Form = XmlSchemaForm.Unqualified, Order=2)]
    public string value { get; set; }

    [XmlElementAttribute(Form = XmlSchemaForm.Unqualified, Order=3)]
    public bool isRequired { get; set; }

    [XmlElementAttribute(Form = XmlSchemaForm.Unqualified, Order=4)]
    public bool isModifiable { get; set; }
}  

但最终结果始终是SubjectItem 的空数组,我尝试更改一些XML 属性但没有成功。我应该在响应类中进行哪些更改才能获得预期的结果?

【问题讨论】:

  • 替换 :[XmlArrayItemAttribute("return", Namespace = "", Form = XmlSchemaForm.Unqualified, IsNullable = false)] 如下: [XmlArray(ElementName = "return", Namespace = "" )] [XmlArrayItem(ElementName = "item", Namespace = "")]
  • @jdweng 感谢您的回复,但没有区别,仍然得到一个空数组的项目

标签: c# xml serialization .net-core xml-serialization


【解决方案1】:

在 Visual Studio 中,您可以通过以下方式简单地从 XML 生成 c# 类:

  • 在VS中打开一个.cs文件
  • 从顶部的“编辑”菜单中,选择“选择性粘贴”->“将 XML 粘贴为类”

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-06
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多