Я пытаюсь десериализовать XML-файл с XmlSerializer
в C#.Deserialize XML-элемент с xsi: nil = "true" в C#
Следующий класс назначения, который следует, был автоматически сгенерирован с использованием утилиты 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;
}
}
}
Все работает прекрасно, пока я не достигну этой части файла:
<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>
Как stated in the MSDN, элемент с XSI: ноль = «истина» будет десериализации как нулевой объект, теряет все атрибуты полностью. В C# это означает нулевой объект.
Есть ли способ изменить это поведение, чтобы иметь три свойства десериализации?
Заранее благодарим за любой совет!
EDIT 1:
Это связано пространство имен:
<records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="structure.xsd">
(location is within here somewhere)
</records>
Неплохо оформленный xml. Префикс 'xsi' не привязан к любому пространству имен. –
У этого есть specfication, я просто опустил это для неприкосновенности бизнеса. Я добавил модифицированную версию. – GigiSan
Кажется, это дубликат [Может ли я иметь атрибут null и другой атрибут в том же теге в XML, созданном классом сгенерированного XSD C#?] (Https://stackoverflow.com/questions/32903839/can-i-have-null- attribute-and-other-attribute-at-the-same-tag-in-xml-created-by) – dbc