c# - 如何使用XSD生成的类

标签 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));
            }
        }
    }
}

我如何使用这个文件?我不明白如何在扩展类中使用它来使用这个部分类。

我的目标是序列化、反序列化和对象。 - - - - - - - - - - - - - - - - - 更新 - - - - - - - - -------------

我已经创建了这个测试代码: 使用系统; 使用 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" />

最佳答案

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

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

关于c# - 如何使用XSD生成的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16943352/

相关文章:

c# - Windows 7 计算器破坏 WPF 多点触控?

c# - 如何在 MassTransit 3.0 中使用分散/聚集模式实现传奇

c# - EF Core 3.1 更新实体

c++ - 在 C++ 中创建 Xml 字符串

PHP类没有反应

java - 对象内存管理 Python 与 Java

c++ - 类虚拟成员函数中的线程

c# - 需要 SearchBar 给出相同的结果,无论其字母是否大写

xml - 如何在 PowerShell 中编写 Xml.linq?

java - 从java读取xml文件