正确反序列化XML的反序列化

时间:2012-11-20 09:46:55

标签: c# .net

我不能在我的C#类上反序列化xml文件。反序列化后,我没有看到Corteges列表。它是空的。但是没有生成异常。其他属性还可以。 我的错误在哪里?

<?xml version="1.0" encoding="UTF-8"?>
<Registry>
<HKEY_CURRENT_USER>
    <Path Name="Name1">
        <Binary Name="Name3" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name5" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name7" Value="0"/>
        <DWord32 Name="Name9" Value="0"/>
        <DWord64 Name="Name11" Value="-1073741773"/>
        <DWord64 Name="Name13" Value="-1073741773"/>
        <Text Name="Name15" Value="Value1"/>
        <Text Name="Name17" Value="Value3"/>
        <ExpandText Name="Name19" Value="Value5"/>
        <ExpandText Name="Name21" Value="Value7"/>
        <MultylineText Name="Name23" Value="Value9"/>
        <MultylineText Name="Name25" Value="Value11"/>
        <Binary Name="Name27" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name29" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name31" Value="0"/>
        <DWord32 Name="Name33" Value="0"/>
        <DWord64 Name="Name35" Value="-1073741773"/>
        <DWord64 Name="Name37" Value="-1073741773"/>
        <Text Name="Name39" Value="Value13"/>
        <Text Name="Name41" Value="Value15"/>
        <ExpandText Name="Name43" Value="Value17"/>
        <ExpandText Name="Name45" Value="Value19"/>
        <MultylineText Name="Name47" Value="Value21"/>
        <MultylineText Name="Name49" Value="Value23"/>
    </Path>
    <Path Name="Name51">
        <Binary Name="Name53" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name55" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name57" Value="0"/>
        <DWord32 Name="Name59" Value="0"/>
        <DWord64 Name="Name61" Value="-1073741773"/>
        <DWord64 Name="Name63" Value="-1073741773"/>
        <Text Name="Name65" Value="Value25"/>
        <Text Name="Name67" Value="Value27"/>
        <ExpandText Name="Name69" Value="Value29"/>
        <ExpandText Name="Name71" Value="Value31"/>
        <MultylineText Name="Name73" Value="Value33"/>
        <MultylineText Name="Name75" Value="Value35"/>
        <Binary Name="Name77" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name79" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name81" Value="0"/>
        <DWord32 Name="Name83" Value="0"/>
        <DWord64 Name="Name85" Value="-1073741773"/>
        <DWord64 Name="Name87" Value="-1073741773"/>
        <Text Name="Name89" Value="Value37"/>
        <Text Name="Name91" Value="Value39"/>
        <ExpandText Name="Name93" Value="Value41"/>
        <ExpandText Name="Name95" Value="Value43"/>
        <MultylineText Name="Name97" Value="Value45"/>
        <MultylineText Name="Name99" Value="Value47"/>
    </Path>
</HKEY_CURRENT_USER>
<HKEY_CLASSES_ROOT>
    <Path Name="Name101">
        <Binary Name="Name103" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name105" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name107" Value="0"/>
        <DWord32 Name="Name109" Value="0"/>
        <DWord64 Name="Name111" Value="-1073741773"/>
        <DWord64 Name="Name113" Value="-1073741773"/>
        <Text Name="Name115" Value="Value49"/>
        <Text Name="Name117" Value="Value51"/>
        <ExpandText Name="Name119" Value="Value53"/>
        <ExpandText Name="Name121" Value="Value55"/>
        <MultylineText Name="Name123" Value="Value57"/>
        <MultylineText Name="Name125" Value="Value59"/>
        <Binary Name="Name127" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name129" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name131" Value="0"/>
        <DWord32 Name="Name133" Value="0"/>
        <DWord64 Name="Name135" Value="-1073741773"/>
        <DWord64 Name="Name137" Value="-1073741773"/>
        <Text Name="Name139" Value="Value61"/>
        <Text Name="Name141" Value="Value63"/>
        <ExpandText Name="Name143" Value="Value65"/>
        <ExpandText Name="Name145" Value="Value67"/>
        <MultylineText Name="Name147" Value="Value69"/>
        <MultylineText Name="Name149" Value="Value71"/>
    </Path>
    <Path Name="Name151">
        <Binary Name="Name153" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name155" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name157" Value="0"/>
        <DWord32 Name="Name159" Value="0"/>
        <DWord64 Name="Name161" Value="-1073741773"/>
        <DWord64 Name="Name163" Value="-1073741773"/>
        <Text Name="Name165" Value="Value73"/>
        <Text Name="Name167" Value="Value75"/>
        <ExpandText Name="Name169" Value="Value77"/>
        <ExpandText Name="Name171" Value="Value79"/>
        <MultylineText Name="Name173" Value="Value81"/>
        <MultylineText Name="Name175" Value="Value83"/>
        <Binary Name="Name177" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name179" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name181" Value="0"/>
        <DWord32 Name="Name183" Value="0"/>
        <DWord64 Name="Name185" Value="-1073741773"/>
        <DWord64 Name="Name187" Value="-1073741773"/>
        <Text Name="Name189" Value="Value85"/>
        <Text Name="Name191" Value="Value87"/>
        <ExpandText Name="Name193" Value="Value89"/>
        <ExpandText Name="Name195" Value="Value91"/>
        <MultylineText Name="Name197" Value="Value93"/>
        <MultylineText Name="Name199" Value="Value95"/>
    </Path>
</HKEY_CLASSES_ROOT>
<HKEY_LOCAL_MACHINE>
    <Path Name="Name201">
        <Binary Name="Name203" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name205" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name207" Value="0"/>
        <DWord32 Name="Name209" Value="0"/>
        <DWord64 Name="Name211" Value="-1073741773"/>
        <DWord64 Name="Name213" Value="-1073741773"/>
        <Text Name="Name215" Value="Value97"/>
        <Text Name="Name217" Value="Value99"/>
        <ExpandText Name="Name219" Value="Value101"/>
        <ExpandText Name="Name221" Value="Value103"/>
        <MultylineText Name="Name223" Value="Value105"/>
        <MultylineText Name="Name225" Value="Value107"/>
        <Binary Name="Name227" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name229" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name231" Value="0"/>
        <DWord32 Name="Name233" Value="0"/>
        <DWord64 Name="Name235" Value="-1073741773"/>
        <DWord64 Name="Name237" Value="-1073741773"/>
        <Text Name="Name239" Value="Value109"/>
        <Text Name="Name241" Value="Value111"/>
        <ExpandText Name="Name243" Value="Value113"/>
        <ExpandText Name="Name245" Value="Value115"/>
        <MultylineText Name="Name247" Value="Value117"/>
        <MultylineText Name="Name249" Value="Value119"/>
    </Path>
    <Path Name="Name251">
        <Binary Name="Name253" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name255" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name257" Value="0"/>
        <DWord32 Name="Name259" Value="0"/>
        <DWord64 Name="Name261" Value="-1073741773"/>
        <DWord64 Name="Name263" Value="-1073741773"/>
        <Text Name="Name265" Value="Value121"/>
        <Text Name="Name267" Value="Value123"/>
        <ExpandText Name="Name269" Value="Value125"/>
        <ExpandText Name="Name271" Value="Value127"/>
        <MultylineText Name="Name273" Value="Value129"/>
        <MultylineText Name="Name275" Value="Value131"/>
        <Binary Name="Name277" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name279" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name281" Value="0"/>
        <DWord32 Name="Name283" Value="0"/>
        <DWord64 Name="Name285" Value="-1073741773"/>
        <DWord64 Name="Name287" Value="-1073741773"/>
        <Text Name="Name289" Value="Value133"/>
        <Text Name="Name291" Value="Value135"/>
        <ExpandText Name="Name293" Value="Value137"/>
        <ExpandText Name="Name295" Value="Value139"/>
        <MultylineText Name="Name297" Value="Value141"/>
        <MultylineText Name="Name299" Value="Value143"/>
    </Path>
</HKEY_LOCAL_MACHINE>
<HKEY_USERS>
    <Path Name="Name301">
        <Binary Name="Name303" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name305" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name307" Value="0"/>
        <DWord32 Name="Name309" Value="0"/>
        <DWord64 Name="Name311" Value="-1073741773"/>
        <DWord64 Name="Name313" Value="-1073741773"/>
        <Text Name="Name315" Value="Value145"/>
        <Text Name="Name317" Value="Value147"/>
        <ExpandText Name="Name319" Value="Value149"/>
        <ExpandText Name="Name321" Value="Value151"/>
        <MultylineText Name="Name323" Value="Value153"/>
        <MultylineText Name="Name325" Value="Value155"/>
        <Binary Name="Name327" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name329" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name331" Value="0"/>
        <DWord32 Name="Name333" Value="0"/>
        <DWord64 Name="Name335" Value="-1073741773"/>
        <DWord64 Name="Name337" Value="-1073741773"/>
        <Text Name="Name339" Value="Value157"/>
        <Text Name="Name341" Value="Value159"/>
        <ExpandText Name="Name343" Value="Value161"/>
        <ExpandText Name="Name345" Value="Value163"/>
        <MultylineText Name="Name347" Value="Value165"/>
        <MultylineText Name="Name349" Value="Value167"/>
    </Path>
    <Path Name="Name351">
        <Binary Name="Name353" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name355" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name357" Value="0"/>
        <DWord32 Name="Name359" Value="0"/>
        <DWord64 Name="Name361" Value="-1073741773"/>
        <DWord64 Name="Name363" Value="-1073741773"/>
        <Text Name="Name365" Value="Value169"/>
        <Text Name="Name367" Value="Value171"/>
        <ExpandText Name="Name369" Value="Value173"/>
        <ExpandText Name="Name371" Value="Value175"/>
        <MultylineText Name="Name373" Value="Value177"/>
        <MultylineText Name="Name375" Value="Value179"/>
        <Binary Name="Name377" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name379" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name381" Value="0"/>
        <DWord32 Name="Name383" Value="0"/>
        <DWord64 Name="Name385" Value="-1073741773"/>
        <DWord64 Name="Name387" Value="-1073741773"/>
        <Text Name="Name389" Value="Value181"/>
        <Text Name="Name391" Value="Value183"/>
        <ExpandText Name="Name393" Value="Value185"/>
        <ExpandText Name="Name395" Value="Value187"/>
        <MultylineText Name="Name397" Value="Value189"/>
        <MultylineText Name="Name399" Value="Value191"/>
    </Path>
</HKEY_USERS>
<HKEY_CURRENT_CONFIG>
    <Path Name="Name401">
        <Binary Name="Name403" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name405" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name407" Value="0"/>
        <DWord32 Name="Name409" Value="0"/>
        <DWord64 Name="Name411" Value="-1073741773"/>
        <DWord64 Name="Name413" Value="-1073741773"/>
        <Text Name="Name415" Value="Value193"/>
        <Text Name="Name417" Value="Value195"/>
        <ExpandText Name="Name419" Value="Value197"/>
        <ExpandText Name="Name421" Value="Value199"/>
        <MultylineText Name="Name423" Value="Value201"/>
        <MultylineText Name="Name425" Value="Value203"/>
        <Binary Name="Name427" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name429" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name431" Value="0"/>
        <DWord32 Name="Name433" Value="0"/>
        <DWord64 Name="Name435" Value="-1073741773"/>
        <DWord64 Name="Name437" Value="-1073741773"/>
        <Text Name="Name439" Value="Value205"/>
        <Text Name="Name441" Value="Value207"/>
        <ExpandText Name="Name443" Value="Value209"/>
        <ExpandText Name="Name445" Value="Value211"/>
        <MultylineText Name="Name447" Value="Value213"/>
        <MultylineText Name="Name449" Value="Value215"/>
    </Path>
    <Path Name="Name451">
        <Binary Name="Name453" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name455" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name457" Value="0"/>
        <DWord32 Name="Name459" Value="0"/>
        <DWord64 Name="Name461" Value="-1073741773"/>
        <DWord64 Name="Name463" Value="-1073741773"/>
        <Text Name="Name465" Value="Value217"/>
        <Text Name="Name467" Value="Value219"/>
        <ExpandText Name="Name469" Value="Value221"/>
        <ExpandText Name="Name471" Value="Value223"/>
        <MultylineText Name="Name473" Value="Value225"/>
        <MultylineText Name="Name475" Value="Value227"/>
        <Binary Name="Name477" Value="ZGVmYXVsdA=="/>
        <Binary Name="Name479" Value="ZGVmYXVsdA=="/>
        <DWord32 Name="Name481" Value="0"/>
        <DWord32 Name="Name483" Value="0"/>
        <DWord64 Name="Name485" Value="-1073741773"/>
        <DWord64 Name="Name487" Value="-1073741773"/>
        <Text Name="Name489" Value="Value229"/>
        <Text Name="Name491" Value="Value231"/>
        <ExpandText Name="Name493" Value="Value233"/>
        <ExpandText Name="Name495" Value="Value235"/>
        <MultylineText Name="Name497" Value="Value237"/>
        <MultylineText Name="Name499" Value="Value239"/>
    </Path>
</HKEY_CURRENT_CONFIG>

[Serializable]
[XmlRoot("Registry")]
public class Registry
{
    [XmlElement(typeof(HKEY_CURRENT_USER)),
     XmlElement(typeof(HKEY_CLASSES_ROOT)),
     XmlElement(typeof(HKEY_LOCAL_MACHINE)),
     XmlElement(typeof(HKEY_USERS)),
     XmlElement(typeof(HKEY_CURRENT_CONFIG))]
    public List<Section> Sections { get; set; }
}

[Serializable]
[XmlInclude(typeof(HKEY_CURRENT_USER))
,XmlInclude(typeof(HKEY_CLASSES_ROOT))
,XmlInclude(typeof(HKEY_LOCAL_MACHINE))
,XmlInclude(typeof(HKEY_USERS))
,XmlInclude(typeof(HKEY_CURRENT_CONFIG))]
public class Section
{
    [XmlElement("Path")]
    public List<Brunch> Branches { get; set; }
}

[Serializable]
[XmlRoot("HKEY_CURRENT_USER")]
public class HKEY_CURRENT_USER : Section { }

[Serializable]
[XmlRoot("HKEY_CLASSES_ROOT")]
public class HKEY_CLASSES_ROOT : Section { }

[Serializable]
[XmlRoot("HKEY_LOCAL_MACHINE")]
public class HKEY_LOCAL_MACHINE : Section { }

[Serializable]
[XmlRoot("HKEY_USERS")]
public class HKEY_USERS : Section { }

[Serializable]
[XmlRoot("HKEY_CURRENT_CONFIG")]
public class HKEY_CURRENT_CONFIG : Section { }

[Serializable]
[XmlRoot("Path")]
public class Brunch
{
    [XmlAttribute("Name")]
    public string Name { get; set; }

    [XmlElement(typeof(BinaryCortege)),
     XmlElement(typeof(TextCortege)),
     XmlElement(typeof(ExpandedTextCortege)),
     XmlElement(typeof(MultylineTextCortege)),
     XmlElement(typeof(IntCortege)),
     XmlElement(typeof(LongCortege))]
    public List<Cortege> Corteges { get; set; } 
}

[Serializable]
[XmlInclude(typeof(LongCortege))
,XmlInclude(typeof(IntCortege))
,XmlInclude(typeof(TextCortege))
,XmlInclude(typeof(MultylineTextCortege))
,XmlInclude(typeof(ExpandedTextCortege))
,XmlInclude(typeof(BinaryCortege))]
public class Cortege
{
    [XmlIgnore]
    public CortegeTypeEnum CortegeType { get; protected set; }

    [XmlAttribute("Name")]
    public string Name { get; set; }

    [XmlAttribute("Value")]
    public string Value { get; set; }
}

[Serializable]
[XmlRoot("DWord64")]
public class LongCortege : Cortege
{
    public LongCortege()
    {
        CortegeType = CortegeTypeEnum.LongType;
    }
}

[Serializable]
[XmlRoot("DWord32")]
public class IntCortege : Cortege
{
    public IntCortege()
    {
        CortegeType = CortegeTypeEnum.IntType;
    }
}

[Serializable]
[XmlRoot("MultylineText")]
public class MultylineTextCortege : Cortege
{
    public MultylineTextCortege()
    {
        CortegeType = CortegeTypeEnum.MultilineTextType;
    }
}

[Serializable]
[XmlRoot("ExpandText")]
public class ExpandedTextCortege : Cortege
{
    public ExpandedTextCortege()
    {
        CortegeType = CortegeTypeEnum.ExpandedTextType;
    }
}

[Serializable]
[XmlRoot("Text")]
public class TextCortege : Cortege
{
    public TextCortege()
    {
        CortegeType = CortegeTypeEnum.TextType;
    }
}

[Serializable]
[XmlRoot("Binary")]
public class BinaryCortege : Cortege
{
    public BinaryCortege()
    {
        CortegeType = CortegeTypeEnum.BinaryTextType;
    }
}

public enum CortegeTypeEnum
{
    IntType,
    LongType,
    TextType,
    ExpandedTextType,
    MultilineTextType,
    BinaryTextType
}

PS我曾尝试使用XSD.exe,但我不喜欢它的类。

PPS抱歉我的英文不好

1 个答案:

答案 0 :(得分:1)

明确指定ElementName似乎对我有用,即

[Serializable]
[XmlRoot("Path")]
public class Brunch
{
    [XmlAttribute("Name")]
    public string Name { get; set; }

    [XmlElement(typeof(BinaryCortege), ElementName="Binary"),
        XmlElement(typeof(TextCortege), ElementName = "Text"),
        XmlElement(typeof(ExpandedTextCortege), ElementName = "ExpandText"),
        XmlElement(typeof(MultylineTextCortege), ElementName = "MultylineText"),
        XmlElement(typeof(IntCortege), ElementName = "DWord32"),
        XmlElement(typeof(LongCortege), ElementName="DWord64")]
    public List<Cortege> Corteges { get; set; }
}