对C#的XML帮助,请将其转换为c#

时间:2011-01-07 07:52:24

标签: xml-serialization

<data1>
    <ClosedDates>
      <ClosedDate>2011-01-09</ClosedDate> 
      <ClosedDate>2011-01-10</ClosedDate> 
      <ClosedDate>2011-01-16</ClosedDate> 
    </ClosedDates> 
</data1>

伙计们,请帮我把这个XML转换成C#类。

2 个答案:

答案 0 :(得分:2)

如果您在数据文件上运行xsd.exe实用程序(文档here)两次,您将获得:

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.4952
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=2.0.50727.3038.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class data1 {

    private data1ClosedDates[] itemsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("ClosedDates", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public data1ClosedDates[] Items {
        get {
            return this.itemsField;
        }
        set {
            this.itemsField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class data1ClosedDates {

    private data1ClosedDatesClosedDate[] closedDateField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("ClosedDate", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    public data1ClosedDatesClosedDate[] ClosedDate {
        get {
            return this.closedDateField;
        }
        set {
            this.closedDateField = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class data1ClosedDatesClosedDate {

    private string valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value {
        get {
            return this.valueField;
        }
        set {
            this.valueField = value;
        }
    }
}

xsd.exe实用程序是当前版本为v7.1的Microsoft Windows SDK的一部分,您可以从此处免费下载:http://msdn.microsoft.com/en-us/windows/bb980924

现在有了这个课程,你应该可以写下这样的内容:

XmlSerializer ser = new XmlSerializer(typeof(data1));
var result = ser.Deserialize(@"C:\test.xml");  

答案 1 :(得分:0)

看看XmlSerializer.Deserialize Method(希望能帮到你)