简体   繁体   English

XML到C#:反序列化

[英]XML to C# : Deserialization

I have an XML file like this example : 我有一个像这个例子的XML文件:

<?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... 但是,如果我想使用相同的C#代码反序列化下一个XML示例,它将无法正常工作,我也不知道为什么...

<?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 反序列化到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>(); }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM