将XML转换为具有嵌套嵌入对象的对象

时间:2016-05-09 12:33:17

标签: c# .net xml winforms visual-studio

我有以下具有嵌套对象的XML,当我想将XML反序列化为带有嵌入的Step对象列表的Steps类时,我有一个错误。

[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':成员名称不能与其封闭类型相同

我坚持这个错误。我非常感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

这是因为在你的 [DllImport("shell32.dll")] public static extern IntPtr ExtractIcon(IntPtr hInst, string file, int nIconIndex); [DllImport("user32.dll", SetLastError = true)] static extern bool DestroyIcon(IntPtr hIcon); /// <summary> /// Gets application icon from main .exe. /// </summary> /// <param name="setToObject">object to which to set up icon</param> /// <param name="bAsImageSource">true if get it as "ImageSource" (xaml technology), false if get it as "Icon" (winforms technology)</param> /// <returns>true if successful.</returns> public bool GetIcon(object setToObject, bool bAsImageSource) { String path = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); path = Path.Combine(path, "yourmainexecutableName.exe"); int iIconIndex = 0; // If your application contains multiple icons, then // you could change iIconIndex here. object o2set = null; IntPtr hIcon = ExtractIcon(IntPtr.Zero, path, iIconIndex); if (hIcon == IntPtr.Zero) return false; Icon icon = (Icon)Icon.FromHandle(hIcon); if (bAsImageSource) { o2set = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( icon.ToBitmap().GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions()); } else { icon = (Icon)icon.Clone(); } DestroyIcon(hIcon); setToObject.GetType().GetProperty("Icon").SetValue(setToObject, o2set); return true; } //GetIcon 课程中:

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内有Step Step(开始或什么?!)。

修改

更好的建议:

完全删除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; }
}

其他编辑:

您的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类中唯一改变的是它可以拥有自己的Step列表Steps或{{1 (或者你提供的任何有意义的名字)。

答案 1 :(得分:0)

我发现可以使用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; }
}