InvalidOperationException when calling PostAsXmlAsync

When I call the HttpClient PostAsXmlAsync method, I get the following InvalidOperationException:

Token EndElement in the final state document will 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(); } } 
+3
source share

Source: https://habr.com/ru/post/1201096/


All Articles