Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Serialize class with multiple different schemas

Serialize class with multiple different schemas

Scheduled Pinned Locked Moved C#
xmljsondatabasesaleshelp
8 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hpjchobbes
    wrote on last edited by
    #1

    I have an API that I am trying to use that uses XML for it's requests. Each of these objects have multiple different requests (Add, Modify, and Delete), along with different versions that may exclude some fields. Is there a way to utilize serialization with different schema to account for these differences? For example, using the following Customer object:

    public class Customer
    {
    public int ID;
    public string EditSequence;
    public string Name;
    public bool IsActive;
    public int? ParentID;
    }

    On a Delete request, I have to only supply the ID and the EditSequence fields. If I include the other fields, I get an error. For an Add I need to include all of them. Also, for null fields, they can not be included at all (i.e. no element). I don't think this is possible with regular XML serialization and I'll probably have to code this myself. If so, I was thinking of using custom attributes and reflection. Is there perhaps a better way to accomplish what I am trying?

    B J L 3 Replies Last reply
    0
    • H hpjchobbes

      I have an API that I am trying to use that uses XML for it's requests. Each of these objects have multiple different requests (Add, Modify, and Delete), along with different versions that may exclude some fields. Is there a way to utilize serialization with different schema to account for these differences? For example, using the following Customer object:

      public class Customer
      {
      public int ID;
      public string EditSequence;
      public string Name;
      public bool IsActive;
      public int? ParentID;
      }

      On a Delete request, I have to only supply the ID and the EditSequence fields. If I include the other fields, I get an error. For an Add I need to include all of them. Also, for null fields, they can not be included at all (i.e. no element). I don't think this is possible with regular XML serialization and I'll probably have to code this myself. If so, I was thinking of using custom attributes and reflection. Is there perhaps a better way to accomplish what I am trying?

      B Offline
      B Offline
      BillWoodruff
      wrote on last edited by
      #2

      Please clarify what you are doing: are you making calls to an API passing run-time field values, or, are you calling an API passing some pointer to a serialized xml file, or, to an in-memory xml stream ? How does serialization come into play here ?

      «While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)

      H 1 Reply Last reply
      0
      • H hpjchobbes

        I have an API that I am trying to use that uses XML for it's requests. Each of these objects have multiple different requests (Add, Modify, and Delete), along with different versions that may exclude some fields. Is there a way to utilize serialization with different schema to account for these differences? For example, using the following Customer object:

        public class Customer
        {
        public int ID;
        public string EditSequence;
        public string Name;
        public bool IsActive;
        public int? ParentID;
        }

        On a Delete request, I have to only supply the ID and the EditSequence fields. If I include the other fields, I get an error. For an Add I need to include all of them. Also, for null fields, they can not be included at all (i.e. no element). I don't think this is possible with regular XML serialization and I'll probably have to code this myself. If so, I was thinking of using custom attributes and reflection. Is there perhaps a better way to accomplish what I am trying?

        J Offline
        J Offline
        jschell
        wrote on last edited by
        #3

        Serialization has nothing to do with functionality. It is solely about data. So it doesn't matter how you modify/update/create that data.

        hpjchobbes wrote:

        On a Delete request, I have to only supply the ID and the EditSequence fields

        What you are referring to is generally about a 'difference'. And it has nothing to do with the object itself. The object represents a set of data and functionality associated with the data. You are attempting to push functionality that involves a specific API and delivery of that data into the object itself. Bad idea. What needs to happen is that the API layer (your code) needs to understand what it needs to deliver and then provide a way to do that. And not one that relies on the object to do it. To provide a difference you MUST have two instances of the object. A before and an after. Then the API layer provides a way to either generally or specifically define the difference. Be very careful in your design decision because a general approach ties one to the idea that ALL objects will be delivered using a difference. If there is even one exception then one must break the architecture to deal with it. Which is not the same an understanding that many objects, but not all, need to be handled with a difference. One part of handling a difference also requires that one must be able to define the 'id'. That might not solely be one attribute. A general solution would require a way to recognize the specific attribute(s) that are the 'id'. That would either require metadata or hard coded by each object. One way, there ARE other ways, to provide the id is to require an interface on the the object that returns, as a string (generic solution) the id value.

        1 Reply Last reply
        0
        • H hpjchobbes

          I have an API that I am trying to use that uses XML for it's requests. Each of these objects have multiple different requests (Add, Modify, and Delete), along with different versions that may exclude some fields. Is there a way to utilize serialization with different schema to account for these differences? For example, using the following Customer object:

          public class Customer
          {
          public int ID;
          public string EditSequence;
          public string Name;
          public bool IsActive;
          public int? ParentID;
          }

          On a Delete request, I have to only supply the ID and the EditSequence fields. If I include the other fields, I get an error. For an Add I need to include all of them. Also, for null fields, they can not be included at all (i.e. no element). I don't think this is possible with regular XML serialization and I'll probably have to code this myself. If so, I was thinking of using custom attributes and reflection. Is there perhaps a better way to accomplish what I am trying?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Your question is too vague. You say you "get an error" ... What "error"? And "null" elements can be suppressed from the output xml using attributes; so this is not an issue (either). "Transactions" typically employ a "transaction code" that identifies what fields are "required", optional or not applicable. Using a common template for all you transactions should not be an issue with the proper "codes" and logic in place.

          "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

          H 1 Reply Last reply
          0
          • B BillWoodruff

            Please clarify what you are doing: are you making calls to an API passing run-time field values, or, are you calling an API passing some pointer to a serialized xml file, or, to an in-memory xml stream ? How does serialization come into play here ?

            «While I complain of being able to see only a shadow of the past, I may be insensitive to reality as it is now, since I'm not at a stage of development where I'm capable of seeing it.» Claude Levi-Strauss (Tristes Tropiques, 1955)

            H Offline
            H Offline
            hpjchobbes
            wrote on last edited by
            #5

            The API that I am using takes an XML string. The XML string is based on what request I am trying to do. So, if I have an instance of a customer in my application that does not yet exist in the API software, I create an AddRq XML string, which has all of the fields for the customer:

            John Smith
            1
            ...
            

            Once I process this Add request, I get the ID associated with it, which I save in my custom object. If I then decide I want to update the customer record, I need to pass an slightly different XML string:

            456
            John H. Smith
            1
            ...
            

            In my application, I still have just the one instance of this customer. Also, if I am wanting to delete this customer from the API software, I would pass this request:

            456
            

            In each of these cases I have to be specific about which fields get included in the request or it will be rejected. If I were to include any other fields besides the ID in the delete request, the entire request is rejected and ignored. There's also some other factors based on the version of the API that might be in use. For example, they added a feature for tracking Currency in version 12, but if my customer is still using version 11, I need to never include the Currency field in my Add or Mod request or the entire request is rejected.

            P 1 Reply Last reply
            0
            • L Lost User

              Your question is too vague. You say you "get an error" ... What "error"? And "null" elements can be suppressed from the output xml using attributes; so this is not an issue (either). "Transactions" typically employ a "transaction code" that identifies what fields are "required", optional or not applicable. Using a common template for all you transactions should not be an issue with the proper "codes" and logic in place.

              "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

              H Offline
              H Offline
              hpjchobbes
              wrote on last edited by
              #6

              The error is from the API. If I include a field in a request the entire request will be rejected. For example, in a Delete request I can ONLY pass the saved ID of this record. If I include any other information besides the ID (like the name of the customer) the entire request is rejected. If I include a blank tag in an Add request it gets rejected instead of ignored. If I don't include the ID in a Modification request, the request gets rejected. In this API, a Customer for example can be Added which has one XML Schema, if it already exists then I need to use the Mod XML schema which is slightly different. If I want to delete this Customer, then I have to use the Del XML Schema. All of the data for these schema are coming from my single customer instance.

              L 1 Reply Last reply
              0
              • H hpjchobbes

                The API that I am using takes an XML string. The XML string is based on what request I am trying to do. So, if I have an instance of a customer in my application that does not yet exist in the API software, I create an AddRq XML string, which has all of the fields for the customer:

                John Smith
                1
                ...
                

                Once I process this Add request, I get the ID associated with it, which I save in my custom object. If I then decide I want to update the customer record, I need to pass an slightly different XML string:

                456
                John H. Smith
                1
                ...
                

                In my application, I still have just the one instance of this customer. Also, if I am wanting to delete this customer from the API software, I would pass this request:

                456
                

                In each of these cases I have to be specific about which fields get included in the request or it will be rejected. If I were to include any other fields besides the ID in the delete request, the entire request is rejected and ignored. There's also some other factors based on the version of the API that might be in use. For example, they added a feature for tracking Currency in version 12, but if my customer is still using version 11, I need to never include the Currency field in my Add or Mod request or the entire request is rejected.

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #7

                One way to do this would be to separate out your creation of the serialization from the Customer class, so you would have a DeleteCustomer class, for instance, that would accept an instance of your customer and which would return the appropriate XML. This helps to separate the concerns of your code.

                This space for rent

                1 Reply Last reply
                0
                • H hpjchobbes

                  The error is from the API. If I include a field in a request the entire request will be rejected. For example, in a Delete request I can ONLY pass the saved ID of this record. If I include any other information besides the ID (like the name of the customer) the entire request is rejected. If I include a blank tag in an Add request it gets rejected instead of ignored. If I don't include the ID in a Modification request, the request gets rejected. In this API, a Customer for example can be Added which has one XML Schema, if it already exists then I need to use the Mod XML schema which is slightly different. If I want to delete this Customer, then I have to use the Del XML Schema. All of the data for these schema are coming from my single customer instance.

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  When I deal with XML API's, I often just convert the XML to classes (using "Paste special" initialy); operate on the classes; and then serialize any "output" classess to strings (i.e. xml) for transmission, using the XmlSerializer. [Morteza Sahragard - 'Paste Special': a less well-known feature in Visual Studio](https://weblogs.asp.net/morteza/Paste-Special-a-less-well-known-feature-in-Visual-Studio)

                  "(I) am amazed to see myself here rather than there ... now rather than then". ― Blaise Pascal

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups