简体   繁体   English

Http Post请求C#XML

[英]Http Post request c# xml

How to write this type of request? 如何编写此类请求?

<?xml version="1.0" encoding="UTF-8"?>
<epolice>
    <request subject="push" action="register_number" id="4">
        <push cert_num="AA123456" pre="12" code="AA" post="345"/>
    </request>
    <signature>signature here</signature>
</epolice>

I don't know how to write request parameters of this type of struct. 我不知道如何编写这种结构的请求参数。

Fist make an object of your data 拳头成为数据的对象

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace XmlSerialize
{
    [XmlRoot(ElementName="push")]
    public class Push {
        [XmlAttribute(AttributeName="cert_num")]
        public string Cert_num { get; set; }
        [XmlAttribute(AttributeName="pre")]
        public string Pre { get; set; }
        [XmlAttribute(AttributeName="code")]
        public string Code { get; set; }
        [XmlAttribute(AttributeName="post")]
        public string Post { get; set; }
    }

    [XmlRoot(ElementName="request")]
    public class Request {
        [XmlElement(ElementName="push")]
        public Push Push { get; set; }
        [XmlAttribute(AttributeName="subject")]
        public string Subject { get; set; }
        [XmlAttribute(AttributeName="action")]
        public string Action { get; set; }
        [XmlAttribute(AttributeName="id")]
        public string Id { get; set; }
    }

    [XmlRoot(ElementName="epolice")]
    public class Epolice {
        [XmlElement(ElementName="request")]
        public Request Request { get; set; }
        [XmlElement(ElementName="signature")]
        public string Signature { get; set; }
    }

}

Serialize 连载

private static string XMLSerializer(object obj)
    {
        string xml = "";
        XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
        {
            Encoding = Encoding.UTF8,
            Indent = true
        };
        using (var sww = new Utf8StringWriter())
        {
            using (XmlWriter writer = XmlWriter.Create(sww, xmlWriterSettings))
            {
                    XmlSerializer serializer = new XmlSerializer(obj.GetType());
                    serializer.Serialize(writer, obj);
                xml = sww.ToString();
            }
        }
        return xml;
    }
    private sealed class Utf8StringWriter : StringWriter
    {
        public override Encoding Encoding { get { return Encoding.UTF8; } }
    }

Then you can post your xml serialized Look this example post 然后,你可以发表你的XML序列看看这个例子

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

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