У меня есть набор XML файлов с такой же структурой, кроме указанной c части того же XML. Отличается только раздел ExtraData.
<Message Id="00099101">
<Header>
<A>text</A>
</Header>
<Details>
<AA Id="IdId" Code="999" DateTime="21012020_102139">
<Result>
<Param Name="Type" Type="CHAR_PTR">text</Param>
<Param Name="Subtype" Type="WORD">1</Param>
<Struct Name="Data">
<Param Name="Class" Type="WORD">1</Param>
<Param Name="Type" Type="WORD">1</Param>
<Struct Name="ExtraData">
<Param Name="AAA" Type="CHAR_PTR">1</Param>
<Param Name="BBB" Type="CHAR_PTR">text</Param>
<Param Name="CCC" Type="CHAR_PTR">text</Param>
<Param Name="DDD" Type="CHAR_PTR">text</Param>
</Struct>
</Struct>
</Result>
</AA>
</Details>
</Message>
<Message Id="00099102">
<Header>
<A>text</A>
</Header>
<Details>
<AA Id="IdId" Code="998" DateTime="21012020_102139">
<Result>
<Param Name="Type" Type="CHAR_PTR">text</Param>
<Param Name="Subtype" Type="WORD">1</Param>
<Struct Name="Data">
<Param Name="Class" Type="WORD">1</Param>
<Param Name="Type" Type="WORD">1</Param>
<Struct Name="ExtraData">
<Param Name="EEE" Type="CHAR_PTR">1</Param>
<Param Name="FFF" Type="CHAR_PTR">text</Param>
<Param Name="GGG" Type="CHAR_PTR">text</Param>
<Param Name="HHH" Type="CHAR_PTR">text</Param>
<Param Name="III" Type="CHAR_PTR">1</Param>
<Param Name="JJJ" Type="CHAR_PTR">text</Param>
</Struct>
</Struct>
</Result>
</AA>
</Details>
</Message>
<Message Id="00099103">
<Header>
<A>text</A>
</Header>
<Details>
<AA Id="IdId" Code="997" DateTime="21012020_102139">
<Result>
<Param Name="Type" Type="CHAR_PTR">text</Param>
<Param Name="Subtype" Type="WORD">1</Param>
<Struct Name="Data">
<Param Name="Class" Type="WORD">1</Param>
<Param Name="Type" Type="WORD">1</Param>
<Struct Name="ExtraData">
<Param Name="MMM" Type="CHAR_PTR">1</Param>
</Struct>
</Struct>
</Result>
</AA>
</Details>
</Message>
Я определяю класс одного из XML, копируя XML и вставляя его в Visual Studio. Класс генерируется и читается без проблем.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Message
{
private MessageHeader headerField;
private MessageDetails detailsField;
private uint idField;
/// <remarks/>
public MessageHeader Header
{
get
{
return this.headerField;
}
set
{
this.headerField = value;
}
}
/// <remarks/>
public MessageDetails Details
{
get
{
return this.detailsField;
}
set
{
this.detailsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public uint Id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MessageHeader
{
private string aField;
private string bField;
private string cField;
private string dField;
/// <remarks/>
public string A
{
get
{
return this.aField;
}
set
{
this.aField = value;
}
}
/// <remarks/>
public string B
{
get
{
return this.bField;
}
set
{
this.bField = value;
}
}
/// <remarks/>
public string C
{
get
{
return this.cField;
}
set
{
this.cField = value;
}
}
/// <remarks/>
public string D
{
get
{
return this.dField;
}
set
{
this.dField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MessageDetails
{
private MessageDetailsAA aaField;
/// <remarks/>
public MessageDetailsAA AA
{
get
{
return this.aaField;
}
set
{
this.aaField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MessageDetailsAA
{
private MessageDetailsAAResult resultField;
private string idField;
private ushort codeField;
private string dateTimeField;
/// <remarks/>
public MessageDetailsAAResult Result
{
get
{
return this.resultField;
}
set
{
this.resultField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public ushort Code
{
get
{
return this.codeField;
}
set
{
this.codeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string DateTime
{
get
{
return this.dateTimeField;
}
set
{
this.dateTimeField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MessageDetailsAAResult
{
private MessageDetailsAAResultParam[] paramField;
private MessageDetailsAAResultStruct structField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Param")]
public MessageDetailsAAResultParam[] Param
{
get
{
return this.paramField;
}
set
{
this.paramField = value;
}
}
/// <remarks/>
public MessageDetailsAAResultStruct Struct
{
get
{
return this.structField;
}
set
{
this.structField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MessageDetailsAAResultParam
{
private string nameField;
private string typeField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MessageDetailsAAResultStruct
{
private MessageDetailsAAResultStructParam[] paramField;
private MessageDetailsAAResultStructStruct structField;
private string nameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Param")]
public MessageDetailsAAResultStructParam[] Param
{
get
{
return this.paramField;
}
set
{
this.paramField = value;
}
}
/// <remarks/>
public MessageDetailsAAResultStructStruct Struct
{
get
{
return this.structField;
}
set
{
this.structField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MessageDetailsAAResultStructParam
{
private string nameField;
private string typeField;
private byte valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public byte Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MessageDetailsAAResultStructStruct
{
private MessageDetailsAAResultStructStructParam[] paramField;
private string nameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Param")]
public MessageDetailsAAResultStructStructParam[] Param
{
get
{
return this.paramField;
}
set
{
this.paramField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MessageDetailsAAResultStructStructParam
{
private string nameField;
private string typeField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
string xml = File.ReadAllText(@"C:\example.xml");
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.Write(xml);
writer.Flush();
stream.Position = 0;
var reader = XmlReader.Create(stream, new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document });
var xmlDeserialized = new XmlSerializer(typeof(MachineMessage)).Deserialize(reader);
Проблема возникает, когда я копирую второй XML и последующие. Есть идеи?