2016-05-09 13 views
0

次のXMLにはネストされたオブジェクトがあり、Stepオブジェクトの埋め込みリストでXMLをデシリアライズするときにエラーが発生します。ネストされた埋め込みオブジェクトを持つオブジェクトにXMLを変換する

[XmlRoot(ElementName="comp")] 
public class Comp 
{ 
    [XmlAttribute(AttributeName="ref")] 
    public string Ref { get; set; } 
    [XmlAttribute(AttributeName="dir")] 
    public string Dir { get; set; } 
} 

[XmlRoot(ElementName="step")] 
public class Step 
{ 
    [XmlElement(ElementName="comp")] 
    public Comp Comp { get; set; } 
    [XmlAttribute(AttributeName="name")] 
    public string Name { get; set; } 
    [XmlElement(ElementName="step")] 
    public Step Step { get; set; } 
} 

[XmlRoot(ElementName="steps")] 
public class Steps 
{ 
    [XmlElement(ElementName="step")] 
    public Step Step { get; set; } 
} 

マイXML:

<steps> 
    <step name="2.3 Upper"> 
    <step name="2.3.1 Upper"> 
     <comp ref="15.txt" dir="D:\test" /> 
     <comp ref="16.txt" dir="D:\test2" /> 
    </step> 
    <step name="2.3.2 Upper" > 
     <comp ref="19.txt" dir="D:\test" /> 
     <comp ref="29.txt" dir="D:\test2" /> 
    </step> 
    </step> 
</steps> 

更新:より多くのネストされたステップのための新しいXMLファイル。

<steps> 
    <step name="2.3 Upper"> 
    <step name="2.3.1 Upper"> 
     <step name="2.3.1.1 Upper"> 
     <comp ref="10.txt" dir="D:\test" /> 
     </step> 
     <comp ref="15.txt" dir="D:\test" /> 
     <comp ref="16.txt" dir="D:\test2" /> 
    </step> 
    <step name="2.3.2 Upper" > 
     <comp ref="19.txt" dir="D:\test" /> 
     <comp ref="29.txt" dir="D:\test2" /> 
    </step> 
    </step> 
</steps> 

私は次のエラーの乗り心地を得るためにどのようにあなたの助けを聞いても:

'Step': member names cannot be the same as their enclosing type

私はこのエラーにこだわっています。私は本当にあなたの助けに感謝します。

答えて

1

あなたStepクラスでからです:

[XmlRoot(ElementName="step")] 
public class Step 
{ 
    [XmlElement(ElementName="comp")] 
    public Comp Comp { get; set; } 
    [XmlAttribute(AttributeName="name")] 
    public string Name { get; set; } 
    [XmlElement(ElementName="step")] 
    public Step Step { get; set; } // <-- Here is your issue 
} 

あなたはStep(開始または何?!)と呼ばれるStepStepを持っています。

EDIT:

より良い提案:

完全Stepsクラスを削除する - 必要ありません。

ので、同じように、あなたのStepクラスを更新します。

[XmlRoot(ElementName="step")] 
public class Step 
{ 
    [XmlElement(ElementName="comp")] 
    public Comp Comp { get; set; } 
    [XmlAttribute(AttributeName="name")] 
    public string Name { get; set; } 
    [XmlElement(ElementName="steps")] 
    public List<Step> Steps { get; set; } 
} 

追加EDIT:

あなたのXMLは、このように出てくるでしょう:

<steps> 
    <step name="Step 1"> 
     <steps> 
      <!-- 3 substeps in Step 1 --> 
      <step name="Step 1 - 1st substep" ></step> 
      <step name="Step 1 - 2nd substep" ></step> 
      <step name="Step 1 - 3rd substep" > 
       <steps> 
        <!-- The 3rd one has its own substep --> 
        <step name="Step 1 - 3rd substep - 1st substep"/> 
       </steps> 
      </step> 
     </steps> 
    </step> 

    <step name="Step 2"> 
     <steps> 
      <!-- Step 2 has 1 substep --> 
      <step name="Step 2 - 1st substep"/> 
     </steps> 
    </step> 

    <step name="Step 3"> 
     <!-- No substeps in here --> 
    </step> 
</steps> 

明らかにあなたの他の要素/属性step要素内には、例のように独自の要素が含まれます。

主なポイントはStepクラスに変更されます唯一の事は、それがStepsまたはSubsteps(または任意の意味のある名前を与える)と呼ばStep Sの独自のリストを持つことができるということであるということです。

+0

しかし、この方法では、内側の手順を、彼らはより多くの '' Steps''に保存されていないだろうしているとき。右? –

+0

'Steps'オブジェクトを' List '型のプロパティに変更し、' Step'オブジェクトをこのリストに挿入することができます。必要であれば、 'Step'プロパティーを内部に持つこともできます。単一の' Step'プロパティーを持たずに、より多くのステップを含めることができます。あなたの階層は以下のようになります:ステップ>ステップ>ステップ>ツリーに深く入るステップ –

+0

良いアイデア。あなたのコードを更新していただけますか? –

0

私は次のように1は無制限の再帰を持っているList<step>を使用できることが判明:

[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 
public partial class steps 
{ 
    public script script { get; set; } 
    [System.Xml.Serialization.XmlElementAttribute("step")] 
    public List<step> step { get; set; } 
} 

[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 
public partial class step 
{ 
    [System.Xml.Serialization.XmlElementAttribute("step")] 
    public List<step> Step1 { get; set; } 
    [System.Xml.Serialization.XmlElementAttribute("comp")] 
    public List<comp> comp { get; set; } 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string name { get; set; } 
} 
[System.SerializableAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)] 
public partial class comp 
{ 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string @ref { get; set; } 
    [System.Xml.Serialization.XmlAttributeAttribute()] 
    public string dir { get; set; } 
} 
関連する問題