简体   繁体   中英

How to serialize object into Soap XML format

I am calling a WCF service and passing some input values that give me output values as a response. I am doing with the demo requests in the string format. but I need to make it more dynamic so I am thinking to serialize an object into Soap XML format but I don't know how to do that.

 public void callservice() { string reqdata = "<MSRequest>"+ @"<RequestData id="TESTING">"+ @"<Attributes>"+ @"<attribute key="ProductID">534</attribute>"+ @"<attribute key="AGE">29</attribute>"+ @"<attribute key="Gender">0</attribute>"+ "</Attributes>"+ "<RequestOutput>"+ @"<attribute key="ProductName" />"+ @"<attribute key="Premiumamount" />"+ "</RequestOutput>"+ "</RequestData>"+ "</MSRequest>"; Product.Productservice service = new Product.Productservice(); var response = service.processrequest(reqdata); }

//Above is the code I am using. As you guys can see I am passing a string with hardcoded values. But I want to serialize into soap XML that will serialize objects into a given below format.

 <attribute key="ProductID">534</attribute>

Try xml linq:

        public void callservice()
        {

            string id = "TESTING";
            string productID = "534";
            int age = 29;
            int gender = 0;

            XElement reqData = new XElement("MSRequest", new object[] {
                new XElement("RequestData", new object[] {
                    new XAttribute("id", id),
                    new XElement("Attributes", new object[] {
                        new XElement("attribute", new object[] {
                            new XAttribute("key", "ProductID"),
                            productID,
                        }),
                        new XElement("attribute", new object[] {
                            new XAttribute("AGE", "ProductID"),
                            age,
                        }),
                        new XElement("attribute", new object[] {
                            new XAttribute("key", "Gender"),
                            gender,
                        })
                    }),
                    new XElement("RequestOutput", new object[] {
                        new XElement("attribute", new object[] {
                            new XAttribute("key", "ProductName")
                        }),
                        new XElement("attribute", new object[] {
                            new XAttribute("AGE", "Premiumamount")
                        })
                    })
                })
            });


            Product.Productservice service = new Product.Productservice();
            var response = service.processrequest(reqData.ToString());

For serialization use following classes:

    public class MSRequest
    {
        [XmlElement("RequestData")]
        public List<RequestData> requestData { get; set; }
    }
    public class RequestData
    {
        [XmlAttribute("id")]
        public string id { get; set; }

        [XmlArray("Attributes")]
        [XmlArrayItem("attribute")]
        List<Attribute> attributes { get; set; }
    }
    public class Attribute
    {
        [XmlAttribute("key")]
        public string key { get; set; }

        [XmlText]
        public string value { get; set; }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM