Mam prawdziwy problem z próbą deserializacji XML i miałem nadzieję, że ktoś może zaoferować pomoc. Czytałem wiele podobnych postów, ale nie jestem w stanie tego rozwiązać.Błąd Deserializowanie Xml do Object - xmlns = '' nie było oczekiwane
XML Ja próbuje deserializowania
<register-account success="false">
<user-name>xxxxx</user-name>
<password>fghgh</password>
<email>[email protected]</email>
<error>
<errorcode>120</errorcode>
<errormessage>The password is invalid</errormessage>
</error>
</register-account>
klasa próbuję deserializowania do:
[Serializable, XmlRoot(ElementName = "register-account", Namespace = "MyNamespace")]
[XmlType("register-account")]
public class RegisterAccountResponse
{
[XmlAttribute("success")]
public bool Success { get; set; }
/// <summary>
/// Gets or sets the Tennant email address
/// </summary>
[XmlElement("email")]
public string Email { get; set; }
/// <summary>
/// Gets or sets the tennant password
/// </summary>
[XmlElement("password")]
public string Password { get; set; }
/// <summary>
/// Gets or sets the Tennant username
/// </summary>
[XmlElement("user-name")]
public string Username { get; set; }
/// <summary>
/// A Tenant Portal error relating to the RegisterAccountRequest
/// </summary>
[XmlElement("error")]
public QubeError Error;
}
deserializacjia Metoda
public static T Deserialize<T>(string data) where T : class
{
if (data == null)
{
return null;
}
if (data.Trim().Length == 0)
{
return null;
}
var ser = new XmlSerializer(typeof(T));
using (var sr = new StringReader(data))
{
return (T)ser.Deserialize(sr);
}
}
Metoda Deserializacji połączeń
var data = Helper.Deserialize<RegisterAccountResponse>(xml);
Wyjątek:
Występuje błąd w dokumencie XML (1, 2). ---> System.InvalidOperationException: nie było oczekiwane . w Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderData.Read5_data()
Exception Wewnętrznej następująco:
<register-account xmlns=''> was not expected.
Marc byłeś na miejscu! Nie mogę ci wystarczająco podziękować, ponieważ wydzierałem sobie włosy z tego. – ProNotion
Dziękuję bardzo! Dodatkowe informacje na temat przestrzeni nazw były rozwiązaniem, którego potrzebowałem. – varagrawal
Czy mógłbyś powiedzieć więcej o tym, dlaczego "Namespace =" w klasie jest niepotrzebny? – gfan