繁体   English   中英

如何将object序列化为Soap XML格式

[英]How to serialize object into Soap XML format

我正在调用 WCF 服务并传递一些输入值,这些输入值给了我 output 值作为响应。 我正在处理字符串格式的演示请求。 但我需要让它更加动态,所以我想将 object 序列化为 Soap XML 格式,但我不知道该怎么做。

 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); }

//上面是我正在使用的代码。 正如你们所看到的,我正在传递一个带有硬编码值的字符串。 但我想序列化为 soap XML ,它将对象序列化为以下给定的格式。

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

尝试 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());

对于序列化,请使用以下类:

    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; }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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