将Xml反序列化为不同的类型

时间:2016-05-15 11:20:07

标签: c# xml xml-deserialization

这个问题可能是我没有正确地提出这个问题,但是相互冲突的名称和类别使其变得困难。我想构建一个跟踪代码指标的小工具,因为我重构了一些代码。结果现在输出到我试图反序列化的文件。以下是指标的信息。 (对不起,这是我能做到的那么小)

<CodeMetricsReport Version="11">
<Targets>
    <Target Name="C:\Git\coab\GoldBox.Logging\bin\Debug\GoldBox.Logging.dll">
    <Modules>
        <Module Name="GoldBox.Logging.dll" AssemblyVersion="1.0.5978.28510">
        <Metrics>
            <Metric Name="MaintainabilityIndex" Value="88" />
            <Metric Name="CyclomaticComplexity" Value="30" />
            <Metric Name="ClassCoupling" Value="13" />
            <Metric Name="DepthOfInheritance" Value="1" />
            <Metric Name="LinesOfCode" Value="51" />
        </Metrics>
        <Namespaces>
            <Namespace Name="GoldBox.Logging">
            <Metrics>
                <Metric Name="MaintainabilityIndex" Value="88" />
                <Metric Name="CyclomaticComplexity" Value="30" />
                <Metric Name="ClassCoupling" Value="13" />
                <Metric Name="DepthOfInheritance" Value="1" />
                <Metric Name="LinesOfCode" Value="51" />
            </Metrics>
            <Types>
                <Type Name="Config">
                <Metrics>
                    <Metric Name="MaintainabilityIndex" Value="89" />
                    <Metric Name="CyclomaticComplexity" Value="9" />
                    <Metric Name="ClassCoupling" Value="5" />
                    <Metric Name="DepthOfInheritance" Value="1" />
                    <Metric Name="LinesOfCode" Value="15" />
                </Metrics>
                <Members>
                    <Member Name="BasePath.get() : string" File="C:\Git\coab\GoldBox.Logging\Config.cs" Line="8">
                    <Metrics>
                        <Metric Name="MaintainabilityIndex" Value="98" />
                        <Metric Name="CyclomaticComplexity" Value="1" />
                        <Metric Name="ClassCoupling" Value="0" />
                        <Metric Name="LinesOfCode" Value="1" />
                    </Metrics>
                    </Member>
                    <Member Name="BasePath.set(string) : void" File="C:\Git\coab\GoldBox.Logging\Config.cs" Line="8">
                    <Metrics>
                        <Metric Name="MaintainabilityIndex" Value="95" />
                        <Metric Name="CyclomaticComplexity" Value="1" />
                        <Metric Name="ClassCoupling" Value="0" />
                        <Metric Name="LinesOfCode" Value="1" />
                    </Metrics>
                    </Member>
                    <Member Name="LogPath.get() : string" File="C:\Git\coab\GoldBox.Logging\Config.cs" Line="9">
                    <Metrics>
                        <Metric Name="MaintainabilityIndex" Value="98" />
                        <Metric Name="CyclomaticComplexity" Value="1" />
                        <Metric Name="ClassCoupling" Value="0" />
                        <Metric Name="LinesOfCode" Value="1" />
                    </Metrics>
                    </Member>
                    <Member Name="LogPath.set(string) : void" File="C:\Git\coab\GoldBox.Logging\Config.cs" Line="9">
                    <Metrics>
                        <Metric Name="MaintainabilityIndex" Value="95" />
                        <Metric Name="CyclomaticComplexity" Value="1" />
                        <Metric Name="ClassCoupling" Value="0" />
                        <Metric Name="LinesOfCode" Value="1" />
                    </Metrics>
                    </Member>
                    <Member Name="SavePath.get() : string" File="C:\Git\coab\GoldBox.Logging\Config.cs" Line="10">
                    <Metrics>
                        <Metric Name="MaintainabilityIndex" Value="98" />
                        <Metric Name="CyclomaticComplexity" Value="1" />
                        <Metric Name="ClassCoupling" Value="0" />
                        <Metric Name="LinesOfCode" Value="1" />
                    </Metrics>
                    </Member>
                    <Member Name="SavePath.set(string) : void" File="C:\Git\coab\GoldBox.Logging\Config.cs" Line="10">
                    <Metrics>
                        <Metric Name="MaintainabilityIndex" Value="95" />
                        <Metric Name="CyclomaticComplexity" Value="1" />
                        <Metric Name="ClassCoupling" Value="0" />
                        <Metric Name="LinesOfCode" Value="1" />
                    </Metrics>
                    </Member>
                    <Member Name="Setup() : void" File="C:\Git\coab\GoldBox.Logging\Config.cs" Line="13">
                    <Metrics>
                        <Metric Name="MaintainabilityIndex" Value="67" />
                        <Metric Name="CyclomaticComplexity" Value="1" />
                        <Metric Name="ClassCoupling" Value="3" />
                        <Metric Name="LinesOfCode" Value="7" />
                    </Metrics>
                    </Member>
                    <Member Name="CreateIfNeeded(string) : void" File="C:\Git\coab\GoldBox.Logging\Config.cs" Line="25">
                    <Metrics>
                        <Metric Name="MaintainabilityIndex" Value="84" />
                        <Metric Name="CyclomaticComplexity" Value="2" />
                        <Metric Name="ClassCoupling" Value="1" />
                        <Metric Name="LinesOfCode" Value="2" />
                    </Metrics>
                    </Member>
                </Members>
                </Type>
            </Types>
            </Namespace>
        </Namespaces>
        </Module>
    </Modules>
    </Target>
</Targets>
</CodeMetricsReport>

我正在努力工作

void Main()
{
    var xml = XElement.Load(@"C:\Git\coab\MetricResults\immer@DESKTOP-2KSR2T6 2016-05-15 05_27_14.mrx");
    XmlSerializer serializer = new XmlSerializer(typeof(Target));
    xml.Dump();
    xml.Descendants("Target")
        .Select(e=>(Target)serializer.Deserialize(e.CreateReader()))
        .Dump();
}
public class Target 
{
    [XmlAttribute]
    public string Name {get;set;}
    public List<Module> Modules {get;set;}
}
public class Module
{
    [XmlAttribute]
    public string Name {get;set;}

    [XmlAttribute]
    public string AssemblyVersion {get;set;}

    public List<Metric> Metrics {get;set;}
    public List<Namespace> Namespaces {get;set;}
}
public class Namespace
{
    [XmlAttribute]
    public string Name {get;set;}
    public List<Metric> Metrics {get;set;}
    public List<Type> Types {get;set;}
}

public class Type
{
    [XmlAttribute]
    public string Name {get;set;}
    public List<Metric> Metrics {get;set;}

}
public class Metric
{
    [XmlAttribute]
    public string Name {get;set;}

    [XmlAttribute]
    public string Value {get;set;}
}

当我到Type时,我知道我可以依赖LinqPad将我的Type放在它自己独特的命名空间中,代码就可以了。但是对于我想写的代码,我不希望它是Type,我宁愿它是MetricType但是当我将Type更改为MetricType时班级Namespace我得到零结果。所以问题是如何让Namespace看起来像这样呢?

public class Namespace
{
    [XmlAttribute]
    public string Name {get;set;}
    public List<Metric> Metrics {get;set;}
    public List<MetricType> Types {get;set;}
}
public class MetricType
{
    [XmlAttribute]
    public string Name {get;set;}
    public List<Metric> Metrics {get;set;}
}

1 个答案:

答案 0 :(得分:1)

序列化器将根据类型和属性名称推断元素和属性名称。如果需要不同的名称,则必须添加属性以显式定义要使用的名称。

您想要的属性是XmlArrayItem,它指定列表中项目的名称:

public class Namespace
{
    [XmlAttribute]
    public string Name { get; set; }

    public List<Metric> Metrics { get; set; }

    [XmlArrayItem("Type")]
    public List<MetricType> Types { get; set; }
}

只是为了给你更多的味道,如果你想明确这个类中的所有名字,你需要这些属性:

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

    [XmlArray("Metrics")]
    [XmlArrayItem("Metric")]
    public List<Metric> Metrics { get; set; }

    [XmlArray("Types")]
    [XmlArrayItem("Type")]
    public List<MetricType> Types { get; set; }
}

有关正常工作的演示,请参阅this fiddle