I have an XML file like this example :
<?xml version="1.0"?>
<ArrayOfBloc xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Bloc>
<table>
<name>OM_MUSER</name>
</table>
<listColumn>
<Column>
<name>CODE_USER</name>
<value>SUPER</value>
<type>VARCHAR</type>
<pk>1</pk>
</Column>
<Column>
<name>ID_MPERSONNE</name>
<value>1</value>
<type>INT</type>
</Column>
<Column>
<name>PASSWORD_USER</name>
<value>TESTJPA</value>
<type>VARCHAR</type>
</Column>
</listColumn>
</Bloc>
<!-- ... -->
</ArrayOfBloc>
And I can deserialize this one using this :
class XmlManager
{
public static List<Bloc> ReadXML(string path)
{
System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(List<Bloc>));
System.IO.StreamReader file = new System.IO.StreamReader(path);
List<Bloc> listB = new List<Bloc>();
listB = (List<Bloc>)reader.Deserialize(file);
file.Close();
return listB;
}
}
public class Bloc
{
public Table table { get; set; }
public List<Column> listColumn { get; set; }
public Bloc()
{
table = new Table();
listColumn = new List<Column>();
}
}
public class Table
{
public string name { get; set; }
public List<Column> listColumn { get; set; }
public Table() { listColumn = new List<Column>(); }
}
public class Column
{
public string name { get; set; }
public string value { get; set; }
public string type { get; set; }
public int pk { get; set; }
public string defaultValue { get; set; }
public int nullable { get; set; }
public Column() { }
}
But if I want to deserialize the next XML example with the same C# code, it doesn't work and I don't know why...
<?xml version="1.0"?>
<ArrayOfBloc xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Bloc>
<table name="OM_MUSER"/>
<listColumn>
<Column name="CODE_USER" value="SUPER" type="VARCHAR" pk="1"/>
<Column name="ID_MPERSONNE" value="1" type="INT"/>
<Column name="PASSWORD_USER" value="TESTJPA" type="VARCHAR"/>
</listColumn>
</Bloc>
<!-- ... -->
</ArrayOfBloc>
Do you know what is required to do please ?
Thanks a lot for your help !
Try this:
[XmlRoot("ArrayOfBloc")]
public class BlocContainer
{
[XmlElement("Bloc")]
public List<Bloc> BlocCollection { get; set; }
}
public class Table
{
[XmlAttribute("name")]
public string name { get; set; }
public List<Column> listColumn { get; set; }
public Table() { listColumn = new List<Column>(); }
}
public class Column
{
[XmlAttribute("name")]
public string name { get; set; }
[XmlAttribute("value")]
public string value { get; set; }
[XmlAttribute("type")]
public string type { get; set; }
[XmlAttribute("pk")]
public int pk { get; set; }
[XmlAttribute("defaultValue")]
public string defaultValue { get; set; }
[XmlAttribute("nullable")]
public int nullable { get; set; }
public Column() { }
}
Deserialize to BlocContainer
public static List<Bloc> ReadXML(string path)
{
System.Xml.Serialization.XmlSerializer reader = new System.Xml.Serialization.XmlSerializer(typeof(BlocContainer));
System.IO.StreamReader file = new System.IO.StreamReader(path);
BlocContainer bc = null;
bc = (BlocContainer)reader.Deserialize(file);
file.Close();
if(bc != null) { return bc.BlocCollection; } else { return new List<Bloc>(); }
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.