InvalidOperationException on a PostAsXmlAsync call

*爱你&永不变心* 提交于 2020-01-14 14:08:09

问题


When calling HttpClient method PostAsXmlAsync, I'm getting the following InvalidOperationException:

Token EndElement in state End Document would result in an invalid XML document.

Why am I getting this exception?

var user = new RXGRequestUser();
var client = new HttpClient();

var response = await client.PostAsXmlAsync(@"myurl.com", user);

RXGRequestUser:

public class RXGRequestUser : IXmlSerializable
{
    public string Username { get; set; }
    public string Password { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public int UsagePlanID { get; set; }

    public void WriteXml(XmlWriter writer)
    {
        var xml = new XDocument(
            new XDeclaration("1.0", Encoding.UTF8.EncodingName, "yes"),
            new XElement("record",
                new XElement("login", Username),
                new XElement("password", Password),
                new XElement("password_confirmation", Password),
                new XElement("first_name", FirstName),
                new XElement("last_name", LastName),
                new XElement("email", Email),
                new XElement("usage-plan", UsagePlanID),
                new XElement("do_apply_usage_plan", 1)
            )
        );
        xml.WriteTo(writer);
    }

    public XmlSchema GetSchema()
    {
        throw new NotSupportedException();
    }

    public void ReadXml(XmlReader reader)
    {
        throw new NotSupportedException();
    }
}

来源:https://stackoverflow.com/questions/25251240/invalidoperationexception-on-a-postasxmlasync-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!