开发者

XML Help to C#, Please convert this to c#

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

Guys, please help me to convert this XML in to C# clas开发者_开发百科s.


If you run the xsd.exe utility (docs here) twice on your data file, you'll get:

//------------------------------------------------------------------------------
// <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;
        }
    }
}

The xsd.exe utility is part of the Microsoft Windows SDK currently at v7.1 which you can download for free from here: http://msdn.microsoft.com/en-us/windows/bb980924

Now with this class in hand, you should be able to write something like:

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


Have a look at XmlSerializer.Deserialize Method (hope will help you)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜