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

Deserialize Xml to different type

这个问题可能是我问的问题不对,但是名称和 classes 的冲突使问题变得困难。我想构建一个小工具来跟踪我重构某些代码时的代码指标。结果现在输出到我试图反序列化的文件。这是指标的片段。 (对不起,我能做的这么小)

<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 时class 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;}
}

序列化程序将从类型和 属性 名称中推断出元素和属性名称。如果你想要不同的名字,你必须添加属性来明确定义你想使用的名字。

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

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

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

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

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

[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