4

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();
    }
}
Caramiriel
  • 7,029
  • 3
  • 30
  • 50
nlstack01
  • 789
  • 2
  • 7
  • 30
  • 1
    Your class structure seems fine, what code is generating that XML? – Rj Geraci Oct 07 '15 at 15:05
  • How does an XML structure like your original support multiple properties? In your second example the second `Properties` should probably be `Property` and then it makes sense as a structure! – Jamiec Oct 07 '15 at 15:09
  • @Rj I added the Serialize Code that generates the xml – nlstack01 Oct 07 '15 at 15:09
  • @nlstack01 could you check formatting/indenting of code being posted? The C# code was stylized as xml before, and had quite random indentation. Nothing major though. :) – Caramiriel Oct 07 '15 at 15:12

3 Answers3

1

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; }
}
Volkan Paksoy
  • 6,727
  • 5
  • 29
  • 40
1

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; }
}
Tedford
  • 2,842
  • 2
  • 35
  • 45
0

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.

Rj Geraci
  • 185
  • 1
  • 12