将对象序列化为 XML 时出现意外结果

Unexpected results when serializing objects to XML

我在创建以下架构时遇到问题...

<DocumentProperties>
    <Document>
        <Properties>
            <propertyName>CNumber</propertyName>
            <propertyValue>00645007803</propertyValue>
        </Properties>
        <targetFolder>345678[=11=]645007803\</targetFolder>  
    </Document>
    <Document>
        <Properties>
            <propertyName>CNumber</propertyName>
            <propertyValue>00645007804</propertyValue> 
        </Properties>
        <targetFolder>345678[=11=]645007804\</targetFolder>
    </Document>
</DocumentProperties>

我创建了以下 类 来执行此操作

public class DocumentProperties
{

   public DocumentProperties()
   {
       Document = new List<Document>();
   }

   public List<Document> Document { get; set; }
}

public class Document
{
     public Document()
     {
         Properties = new List<Properties>();
     }

     public List<Properties> Properties { get; set; }
     public string targetFolder { get; set; }
}

public class Properties
{
    public string propertyName { get; set; }
    public string propertyValue { get; set; }
}

public class RetrieveMultipleDocumentsRequest
{
    public SystemProperty SystemProperty { get; set; }
    public RequestProperty RequestProperty { get; set; }
      public DocumentProperties DocumentProperties { get; set; }
}

我得到的输出是 "Document" 和 "Properties" 两次作为彼此的父子。我该如何解决?

我的 类

的输出
 <DocumentProperties>
    <Document>
      <Document>
        <Properties>
          <Properties>
            <propertyName>DizzzyGelespe</propertyName>
            <propertyValue>8E077A60</propertyValue>
          </Properties>
          <Properties />
        </Properties>
        <targetFolder>C:\BXml\TargetFolder\</targetFolder>
      </Document>
    </Document>
  </DocumentProperties>

生成输出的代码:

public string Serialize(RetrieveMultipleDocumentsRequest details)
{
    XmlSerializer serializer = new XmlSerializer(typeof(RetrieveMultipleDocumentsRequest));

    using(StringWriter textWriter = new StringWriter())
    {
        serializer.Serialize(textWriter, details);
        return textWriter.ToString();
    }
}

您的问题在于命名。

文档列表应称为 "documents" 而不是 "document",属性也是如此。

如果您进行这些命名更改,那么您会发现您的 XML 输出是正确且有意义的。

显然,您的命名约定让 XML 序列化程序有些困惑。只需如下显式装饰元素,它应该可以正常工作:

public class DocumentProperties
{
    public DocumentProperties()
    {
        Document = new List<Document>();
    }

    [XmlElement("Document")]
    public List<Document> Document { get; set; }
}

public class Document
{
    public Document()
    {
        Properties = new List<Properties>();
    }

    [XmlElement("Properties")]
    public List<Properties> Properties { get; set; }
    public string targetFolder { get; set; }
}

您需要如下所示注释您的对象模型,以更改默认的序列化行为。 XmlElement 属性的这种应用将根据遇到的 属性 阻止发出父标记,而只发出包含数据。

public class DocumentProperties
{

    public DocumentProperties()
    {
        Document = new List<Document>();
    }

    [XmlElement("Document")]
    public List<Document> Document { get; set; }
}

public class Document
{
    public Document()
    {
        Properties = new List<Properties>();
    }

    [XmlElement("Properties")]
    public List<Properties> Properties { get; set; }
    public string targetFolder { get; set; }
}