0

I am working with a 3rd party who has an xml structure for multiple different requests.

Each request has a common header structure and then some specific footer data.

Unfortunately, the header and footer sections are not wrapped within their own element tags and this is something that I can't change.

Some sample contrived requests are shown below:

Sample Request 1

<?xml version="1.0" encoding="UTF - 8" standalone="yes"?>
<Request>
    <RequestType>1</RequestType>
    <User>User01</User>
    <id>1234</id>
    <Name>John</Name>
    <Age>20</Age>
</Request>

Sample Request 2

<?xml version="1.0" encoding="UTF - 8" standalone="yes"?>
<Request>
    <RequestType>2</RequestType>
    <User>User02</User>
    <id>1235</id>
    <School>The School</School>
    <Teacher>Mrs Smith</Teacher>
</Request>

Sample Request 3

<?xml version="1.0" encoding="UTF - 8" standalone="yes"?>
<Request>
    <RequestType>3</RequestType>
    <User>User01</User>
    <id>223</id>
    <Work>The Office</Word>
    <Boss>Mr White</Boss>
    <Phone>1234567</Phone>
    <Payday>Friday</Payday>
</Request>

You can see that each request has a RequestType, User and id.

My question relates to writing C# code that will encapsulate this Xml for serialization.

To me, it seems wrong to have each of my C# classes having the repeated header (RequestType, User and id) data.

I have tried using generics (see sample code below) but that leads to my question.

Question: How can I serialize my generic object Footer so that it is not wrapped within "root" Footer element?

[System.Xml.Serialization.XmlRoot("Request")]
public class GenericRequest<typeT>
{
    public GenericRequest()
    {
    }

    public int RequestType { get; set; }

    public string User { get; set; }

    public int id { get; set; }

    public typeT Footer { get; set; }
}
2
  • This is not a valid xml. If you know the delimiter you can do a split and parse one by one. Commented Sep 11, 2017 at 10:46
  • I have edited the question to make the samples clearer - apologies for any confusion caused. Commented Sep 11, 2017 at 10:56

1 Answer 1

1

You can create a class with interface IFooter, which can have different implementor as per requirement (e.g.

interface IFooter : ISerializable 
{
 //Define common member.
}

public class Footer1: IFooter
{
 // define members (e.g. work) 
}

public class Footer2: IFooter
{
 // define members 
}

public class Footer3: IFooter
{
 // define members 
}

Now serialize main class

[System.Xml.Serialization.XmlRoot("Request")]
public class GenericRequest
{
  public GenericRequest()
  {
  }


  public int RequestType { get; set; }

  public string User { get; set; }

  public int id { get; set; }

  public IFooter Footer { get; set; }
}

Visit How can I serialize an object that has an interface as a property? for reference.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.