PostAsXmlAsync调用上的InvalidOperationException

时间:2014-08-11 19:51:47

标签: c# xml-serialization dotnet-httpclient

调用HttpClient方法PostAsXmlAsync时,我得到以下InvalidOperationException:

  

状态End Document中的Token EndElement将导致XML文档无效。

为什么我会收到此异常?

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

0 个答案:

没有答案
相关问题