2016-05-15 39 views
0

這個問題可能是我沒有正確地問問題,但衝突的名稱和類使它很難。我想建立一個小工具來跟蹤我的代碼度量,因爲我重構了一些代碼。結果現在輸出到一個文件,我試圖反序列化。這是度量標準的一個缺點。 (對不起,這小我可以使它)反序列化Xml到不同的類型

<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\[email protected] 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更改爲MetricTypeNamespace類中時,我得到零結果。所以問題是我怎樣才能使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

serialiser將從類型和屬性名稱推斷元素和屬性名稱。如果您需要不同的名稱,則必須添加屬性以明確定義要使用的名稱。

你想這裏的屬性是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進行工作演示。

+0

我查看了一堆Xml___Attribute,但沒有看到'XmlArray'或'XmlArrayItem'非常感謝。我結束了作弊和使用LINQ和Select來解決這個問題,但我可能只是試圖關閉目的 –

相關問題