2016-05-15 5 views
0

この質問は正しく質問していないかもしれませんが、矛盾する名前とクラスが難しくなっている可能性があります。私はコードをリファクタリングしながらコードメトリクスを追跡する小さなツールを作りたいと思っています。結果は、逆シリアル化しようとしているファイルに出力されます。ここに指標のスニペットがあります。私は私が私のTypeに入れLinqPadに依存できることを知っていたTypeに着いたとき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であることを望んでいませんが、私はむしろMetricTypeになります。をNamespaceMetricTypeに変更すると、結果はゼロに戻ります。だから問題はどうすれば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

シリアライザは、タイプ名とプロパティ名から要素名と属性名を推測します。別の名前が必要な場合は、使用する名前を明示的に定義するために属性を追加する必要があります。

あなたがここにする属性がリスト内のアイテムの名前を指定し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を使用してこれを回避しようとしましたが、閉鎖の目的でこれを試すことができます –

関連する問題