问题
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