J'essaye de désérialiser un fichier XML avec XmlSerializer
en C#.Désérialise l'élément XML avec xsi: nil = "true" dans C#
La classe de destination qui suit a été générée automatiquement à l'aide de l'utilitaire xsd.
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
public partial class location
{
private string cityField;
private string countryField;
private string stateField;
private string textField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string city
{
get
{
return this.cityField;
}
set
{
this.cityField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string country
{
get
{
return this.countryField;
}
set
{
this.countryField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string state
{
get
{
return this.stateField;
}
set
{
this.stateField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Text
{
get
{
return this.textField;
}
set
{
this.textField = value;
}
}
}
Tout fonctionne bien jusqu'à ce que je tire cette partie du fichier:
<locations>
<location country="PARAGUAY" city="Ciudad del Este" state="Alto Parana" xsi:nil="true"/>
<location country="BRAZIL" city="Passo Fundo" state="Rio Grande do Sul" xsi:nil="true"/>
</locations>
Comme stated in the MSDN, un élément avec xsi: nil = « true » sera désérialisé comme un objet nul, perdant tous les attributs complètement. En C# cela se traduit par un objet nul.
Existe-t-il un moyen de modifier ce comportement pour avoir les trois propriétés désérialisées?
Merci d'avance pour tout conseil!
EDIT 1:
Ceci est l'espace de nommage:
<records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="structure.xsd">
(location is within here somewhere)
</records>
Ce n'est pas bien formé xml. Le préfixe 'xsi' n'est lié à aucun espace de noms. –
Il a une spécification, je l'ai simplement omis pour la vie privée des entreprises. J'ai ajouté une version modifiée. – GigiSan
Cela ressemble à une copie de [Puis-je avoir un attribut null et un autre attribut sur la même balise en XML créé par la classe XSD C# générée?] (Https://stackoverflow.com/questions/32903839/can-i-have-null- attribut-et-autre-attribut-at-the-same-tag-in-xml-created-by) – dbc