如何使用XSD生成的Class

时间:2013-06-05 15:04:22

标签: c# xml class partial xsd.exe

我有这个xml文件:

<?xml version="1.0" encoding="us-ascii"?>
<body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <header><user>BOBBY</user></header>
    <in>
        <customer>0123456789</customer>
    </in>
    <out>
        <cmd>
            <productid></productid>
            <price></price>
            <date></date>
            <state></state>
            <type></type>
        </cmd>
        <cmd>
            <productid></productid>
            <price></price>
            <date></date>
            <state></state>
            <type></type>
        </cmd>
    </out>
    <state>
        <code></code>
        <desc></desc>
    </state>
</body>

并且Xsd.Exe生成这个类:

namespace System.autoGenerated {
    using System.Xml.Serialization;


    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.17929")]
    [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 body : object, System.ComponentModel.INotifyPropertyChanged {

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("header", typeof(bodyHeader), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        [System.Xml.Serialization.XmlElementAttribute("in", typeof(bodyIN), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        [System.Xml.Serialization.XmlElementAttribute("out", typeof(bodyOut), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        [System.Xml.Serialization.XmlElementAttribute("state", typeof(bodyState), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public object[] Items;

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

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

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string user;

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

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

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string customer;

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

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

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("cmd", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public bodyOutCmd[] cmd;

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

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

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string productid;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string price;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string date;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string state;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string type;

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

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

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string code;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string desc;

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName) {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null)) {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

我如何使用此文件?我不明白如何在extend类中使用它来使用这个partial类。

我的目标是序列化和反序列化并反对。 ---------------------------------- UPDATE --------------- -------------

我已经创建了这个测试代码:         使用系统;     使用DAL.XML.PDD.PDDORDM001;     使用System.Xml.Serialization;     使用System.IO;

namespace Tests
{
    class Serialize
    {
        public static void Main()
        {
            Console.WriteLine("Go...");

            XmlSerializer serializer = new XmlSerializer(typeof(body));
            TextWriter writer = new StreamWriter(@"c:\file.xml");
            body test = null;
            serializer.Serialize(writer, test);
            writer.Close();

        }
    }
}

但它给了我这个结果:

<?xml version="1.0" encoding="utf-8"?>
<body xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />

3 个答案:

答案 0 :(得分:1)

您需要使用XmlSerializer来序列化对象并反序列化xml:

using (var stream = new FileStream(xmlFilePath))
{
     var serializer = new XmlSerializer(typeof(body));
     var body = (body) serializer.Deserialize(stream);
 }

答案 1 :(得分:1)

解决方案:我修改了Xml类:

    [System.Xml.Serialization.XmlElementAttribute("Header", typeof(BodyHeader), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("In", typeof(BodyIn), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("State", typeof(BodyState), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlElementAttribute("Out", typeof(BodyOut), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public object[] Items;

替换为

    [System.Xml.Serialization.XmlElementAttribute("Header", typeof(BodyHeader), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public BodyHeader header;

    [System.Xml.Serialization.XmlElementAttribute("In", typeof(BodyIn), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public In in;

    [System.Xml.Serialization.XmlElementAttribute("State", typeof(BodyState), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public BodyState state;

    [System.Xml.Serialization.XmlElementAttribute("Out", typeof(BodyOut), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public BodyOut out;

我的测试代码:

using System;
using DAL.XML.PDD.PDDORDM001;
using System.Xml.Serialization;
using System.IO;

namespace Tests
{
    class Serialize
    {
        public static void Main()
        {
            Console.WriteLine("Serialisation...");


            XmlSerializer serializer = new XmlSerializer(typeof(DAL.XML.PDD.PDDORDM001.Body));
            TextWriter writer = new StreamWriter(@"c:\file.xml");
            DAL.XML.PDD.PDDORDM001.Bodytest = new DAL.XML.PDD.PDDORDM001.Body();

            test.header= new BodyHeader();
            test.header.action = "LEC";
            test.header.user = "BOBBY";

            test.in= new BodyIn();
            test.in.customer= "07251005502";

            serializer.Serialize(writer, test);
            writer.Close();

        }
    }
}

答案 2 :(得分:0)

您可能想要使用DataContractSerializer类。以下是其文档中的示例,稍作修改以适合您的使用:

public static body ReadObject(string fileName)
{
    Console.WriteLine("Deserializing an instance of the object.");
    FileStream fs = new FileStream(fileName,
    FileMode.Open);
    XmlDictionaryReader reader =
       XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
    DataContractSerializer ser = new DataContractSerializer(typeof(body));

    // Deserialize the data and read it from the instance.
    body deserializedBody = (body)ser.ReadObject(reader, true);
    reader.Close();
    fs.Close();
    return deserializedBody;
}

另一个选项是XmlSerializer类。