nlstack01
nlstack01

Reputation: 839

Unexpected results when serializing objects to XML

I am having issues creating the schema below...

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

I created the following classes to do this

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

The output I am getting is "Document" and "Properties" twice as a parent child of each other. How do I resolve this?

Output from my classes

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

Code that is generating the output:

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

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

Upvotes: 4

Views: 86

Answers (3)

Tedford
Tedford

Reputation: 2932

You will need to annotate your object model as shown below in order to change the default serialization behavior. This application of the XmlElement attribute will prevent emiting out the parent tag based upon the encountered property and instead only emit out the containing data.

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

Upvotes: 1

Volkan Paksoy
Volkan Paksoy

Reputation: 6977

Apparently your naming convention confused the XML serializer a bit. Just explicitly decorate the elements as below and it should work fine:

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

Upvotes: 1

Rj Geraci
Rj Geraci

Reputation: 185

Your problem here is with naming.

A list of documents should be called "documents" not "document", same goes for properties.

If you make these naming changes then you will see that your XML output is correct and makes sense.

Upvotes: 0

Related Questions