我在下面创建架构时遇到问题…
<DocumentProperties>
<Document>
<Properties>
<propertyName>CNumber</propertyName>
<propertyValue>00645007803</propertyValue>
</Properties>
<targetFolder>\12345678\00645007803\</targetFolder>
</Document>
<Document>
<Properties>
<propertyName>CNumber</propertyName>
<propertyValue>00645007804</propertyValue>
</Properties>
<targetFolder>\12345678\00645007804\</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; }
}
我得到的输出是“文档”和“属性”两次作为彼此的父子.我该如何解决这个问题?
我班级的输出
<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();
}
}
最佳答案 您需要注释您的对象模型,如下所示,以便更改默认的序列化行为. 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; }
}