locked
Set XSI attribute in automatically generated class from wsdl file RRS feed

  • Question

  • User565442435 posted

    So here is my issue, I need to generate xml that looks something like this

    <CartItems>
        <CartItem>
            <Quantity>1</Quantity>
            <Item xsi:type="Service">
                <ID>000123</ID>
            </Item>
        </CartItem>
    </CartItems>


    I have a bunch of C# classes I generated from their wsdl file using the wsdl.exe thing MS provides. The issue I have is I'm not 100% sure how to set that xsi type attribute.
    When I look at the actual item class in cart item, I don't see any way to set the xsi type.
    Here's the relevant code on setting the item

        CartItem[] items = { new CartItem() };
    
        CartItem citem = new CartItem();
    
        //right here is the item, there's some way I need to be able to set the xsi type of items[0].item to Service. 
        items[0].ID = itemId; //549
        items[0].IDSpecified = false;
        items[0].Quantity = 1;
        items[0].ActionSpecified = false;
    
        req.CartItems = items;


    The last thing is, in investigating the generated classes. I was looking at the cart item object, and there is no way to set a service in it, however, right below it there is a partial class which looks like it has what I need, I'm just not sure how to implement it. This generated code is pushing the limits of my OOP knowledge. Here's the cart item class and the partial class just for reference. A side note, the thing I think I need is at the bottom. Thanks,

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.6.81.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://clients.mindbodyonline.com/api/0_5")]
    public partial class CartItem : MBObject
    {
    
        private Item itemField;
    
        private decimal discountAmountField;
    
        private Appointment[] appointmentsField;
    
        private int[] enrollmentIDsField;
    
        private int[] classIDsField;
    
        private long[] courseIDsField;
    
        private long[] visitIDsField;
    
        private long[] appointmentIDsField;
    
        private ActionCode actionField;
    
        private bool actionFieldSpecified;
    
        private System.Nullable<int> idField;
    
        private bool idFieldSpecified;
    
        private System.Nullable<int> quantityField;
    
        private bool quantityFieldSpecified;
    
        /// <remarks/>
        public Item Item
        {
            get
            {
                return this.itemField;
            }
            set
            {
                this.itemField = value;
            }
        }
    
        /// <remarks/>
        public decimal DiscountAmount
        {
            get
            {
                return this.discountAmountField;
            }
            set
            {
                this.discountAmountField = value;
            }
        }
    
        /// <remarks/>
        public Appointment[] Appointments
        {
            get
            {
                return this.appointmentsField;
            }
            set
            {
                this.appointmentsField = value;
            }
        }
    
        /// <remarks/>
        public int[] EnrollmentIDs
        {
            get
            {
                return this.enrollmentIDsField;
            }
            set
            {
                this.enrollmentIDsField = value;
            }
        }
    
        /// <remarks/>
        public int[] ClassIDs
        {
            get
            {
                return this.classIDsField;
            }
            set
            {
                this.classIDsField = value;
            }
        }
    
        /// <remarks/>
        public long[] CourseIDs
        {
            get
            {
                return this.courseIDsField;
            }
            set
            {
                this.courseIDsField = value;
            }
        }
    
        /// <remarks/>
        public long[] VisitIDs
        {
            get
            {
                return this.visitIDsField;
            }
            set
            {
                this.visitIDsField = value;
            }
        }
    
        /// <remarks/>
        public long[] AppointmentIDs
        {
            get
            {
                return this.appointmentIDsField;
            }
            set
            {
                this.appointmentIDsField = value;
            }
        }
    
        /// <remarks/>
        public ActionCode Action
        {
            get
            {
                return this.actionField;
            }
            set
            {
                this.actionField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool ActionSpecified
        {
            get
            {
                return this.actionFieldSpecified;
            }
            set
            {
                this.actionFieldSpecified = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
        public System.Nullable<int> ID
        {
            get
            {
                return this.idField;
            }
            set
            {
                this.idField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool IDSpecified
        {
            get
            {
                return this.idFieldSpecified;
            }
            set
            {
                this.idFieldSpecified = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
        public System.Nullable<int> Quantity
        {
            get
            {
                return this.quantityField;
            }
            set
            {
                this.quantityField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool QuantitySpecified
        {
            get
            {
                return this.quantityFieldSpecified;
            }
            set
            {
                this.quantityFieldSpecified = value;
            }
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Tip))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Product))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Package))]
    [System.Xml.Serialization.XmlIncludeAttribute(typeof(Service))]
    [System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.6.81.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://clients.mindbodyonline.com/api/0_5")]
    public partial class Item : MBObject
    {
    }












    Tuesday, December 29, 2015 10:27 PM

All replies

  • User614698185 posted

    Hi mandrews,

    Use a derived type, and an XmlInclude attribute. For example:

    public class Book
    {
        public string Title;
        public string Author;
    }
    
    public class MyBookType : Book { }
    
    [XmlInclude(typeof(MyBookType))]
    [XmlRoot("Books")]
    public class Books : List<Book> { }
    
    public void Run()
    {
        var b =  new Books();
        b.Add(new MyBookType
            {
                Title = "The Art of War",
                Author = "Sun Tzu"
            });
        b.Add(new MyBookType
            {
                Title = "Great Expectations",
                Author = "Charles Dickens"
            });
    
        var s = new XmlSerializer(typeof(Books));
        s.Serialize(Console.Out, b);
    }

    Running this produces this output:

    <?xml version="1.0" encoding="IBM437"?>
    <Books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Book xsi:type="MyBookType">
        <Title>The Art of War</Title>
        <Author>Sun Tzu</Author>
      </Book>
      <Book xsi:type="MyBookType">
        <Title>Great Expectations</Title>
        <Author>Charles Dickens</Author>
      </Book>
    </Books>

    You will need to apply [XmlInclude] to whatever holds the collection of books. This could be a parameter in a webmethod, for example.

    You can automatically generate the appropriate XmlInclude attribute, starting from XSD and WSDL, if you define the types in XSD, with the inheritance relationship.

    In WSDL, the request message might take a Books type, which is a collection of Book. Separately, define a MyBookType which derives from Book but does not extend it.

    Best Regards,

    Candice Zhou

    Thursday, December 31, 2015 9:17 AM
  • User565442435 posted

    I found this exact post through googling, however, I don't see how I apply that to my current situation. 

    Thursday, December 31, 2015 3:47 PM
  • User614698185 posted

    Hi mandrews,

    Firstly, your code seems to use thid-party service: https://developers.mindbodyonline.com/

    Secondly, you could refer to the following sample code:

    https://github.com/mindbody/API-Examples/search?l=csharp&q=cartitem&utf8=%E2%9C%93

    https://github.com/mindbody/API-Examples/blob/6bab9d6d86bfad2c204b5aef455bf04113c34af1/C%23/Default.aspx.cs

    So, why you need to generate this xml file?

    Best Regards,

    Candice Zhou

    Tuesday, January 5, 2016 10:01 AM