如何在c#中使用xsd?

我是xml的新手。 我使用xsd.exe从xml创建.xsd文件和.cs文件。 但我不知道现在用什么或如何访问它? 我见过一些例子,但发现它们令人困惑。 是否有一个简单的教程或某个人可以给我的例子?

//------------------------------------------------------------------------------ //  // This code was generated by a tool. // Runtime Version:4.0.30319.237 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. //  //------------------------------------------------------------------------------ using System.Xml.Serialization; // // This source code was auto-generated by xsd, Version=4.0.30319.1. // ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [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 file { private fileClass[] itemsField; ///  [System.Xml.Serialization.XmlElementAttribute("class", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public fileClass[] Items { get { return this.itemsField; } set { this.itemsField = value; } } } ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class fileClass { private string search_keywordsField; private fileClassPart[] partField; private string idField; ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string search_keywords { get { return this.search_keywordsField; } set { this.search_keywordsField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute("part", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public fileClassPart[] part { get { return this.partField; } set { this.partField = value; } } ///  [System.Xml.Serialization.XmlAttributeAttribute()] public string id { get { return this.idField; } set { this.idField = value; } } } ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class fileClassPart { private string tcpnField; private string last_change_dateField; private string primary_part_nbrField; private string te_part_detail_urlField; private string part_statusField; private string product_codeField; private string gpl_codeField; private string gpl_descField; private string lf_comp_descField; private string lf_process_descField; private string part_descField; private fileClassPartImage_urls[] image_urlsField; private fileClassPartPart_aliasesAlias[][] part_aliasesField; private fileClassPartAttributesAttr_cat[][] attributesField; private fileClassPartDocInfoList[] docInfoListField; private string idField; ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string tcpn { get { return this.tcpnField; } set { this.tcpnField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string last_change_date { get { return this.last_change_dateField; } set { this.last_change_dateField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string primary_part_nbr { get { return this.primary_part_nbrField; } set { this.primary_part_nbrField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string te_part_detail_url { get { return this.te_part_detail_urlField; } set { this.te_part_detail_urlField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string part_status { get { return this.part_statusField; } set { this.part_statusField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string product_code { get { return this.product_codeField; } set { this.product_codeField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string gpl_code { get { return this.gpl_codeField; } set { this.gpl_codeField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string gpl_desc { get { return this.gpl_descField; } set { this.gpl_descField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string lf_comp_desc { get { return this.lf_comp_descField; } set { this.lf_comp_descField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string lf_process_desc { get { return this.lf_process_descField; } set { this.lf_process_descField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string part_desc { get { return this.part_descField; } set { this.part_descField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute("image_urls", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public fileClassPartImage_urls[] image_urls { get { return this.image_urlsField; } set { this.image_urlsField = value; } } ///  [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] [System.Xml.Serialization.XmlArrayItemAttribute("alias", typeof(fileClassPartPart_aliasesAlias), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public fileClassPartPart_aliasesAlias[][] part_aliases { get { return this.part_aliasesField; } set { this.part_aliasesField = value; } } ///  [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] [System.Xml.Serialization.XmlArrayItemAttribute("attr_cat", typeof(fileClassPartAttributesAttr_cat), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)] public fileClassPartAttributesAttr_cat[][] attributes { get { return this.attributesField; } set { this.attributesField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute("docInfoList", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public fileClassPartDocInfoList[] docInfoList { get { return this.docInfoListField; } set { this.docInfoListField = value; } } ///  [System.Xml.Serialization.XmlAttributeAttribute()] public string id { get { return this.idField; } set { this.idField = value; } } } ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class fileClassPartImage_urls { private string te_primary_image_urlField; ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string te_primary_image_url { get { return this.te_primary_image_urlField; } set { this.te_primary_image_urlField = value; } } } ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class fileClassPartPart_aliasesAlias { private string valueField; ///  [System.Xml.Serialization.XmlTextAttribute()] public string Value { get { return this.valueField; } set { this.valueField = value; } } } ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class fileClassPartAttributesAttr_cat { private fileClassPartAttributesAttr_catAttr_type[] attr_typeField; private string nameField; ///  [System.Xml.Serialization.XmlElementAttribute("attr_type", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public fileClassPartAttributesAttr_catAttr_type[] attr_type { get { return this.attr_typeField; } set { this.attr_typeField = value; } } ///  [System.Xml.Serialization.XmlAttributeAttribute()] public string name { get { return this.nameField; } set { this.nameField = value; } } } ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class fileClassPartAttributesAttr_catAttr_type { private fileClassPartAttributesAttr_catAttr_typeAttr_value[] attr_valueField; private fileClassPartAttributesAttr_catAttr_typeAlt_uom_attr_value[] alt_uom_attr_valueField; private string idField; private string nameField; private string uomField; ///  [System.Xml.Serialization.XmlElementAttribute("attr_value", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] public fileClassPartAttributesAttr_catAttr_typeAttr_value[] attr_value { get { return this.attr_valueField; } set { this.attr_valueField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute("alt_uom_attr_value", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)] public fileClassPartAttributesAttr_catAttr_typeAlt_uom_attr_value[] alt_uom_attr_value { get { return this.alt_uom_attr_valueField; } set { this.alt_uom_attr_valueField = value; } } ///  [System.Xml.Serialization.XmlAttributeAttribute()] public string id { get { return this.idField; } set { this.idField = value; } } ///  [System.Xml.Serialization.XmlAttributeAttribute()] public string name { get { return this.nameField; } set { this.nameField = value; } } ///  [System.Xml.Serialization.XmlAttributeAttribute()] public string uom { get { return this.uomField; } set { this.uomField = value; } } } ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class fileClassPartAttributesAttr_catAttr_typeAttr_value { private string valueField; ///  [System.Xml.Serialization.XmlTextAttribute()] public string Value { get { return this.valueField; } set { this.valueField = value; } } } ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class fileClassPartAttributesAttr_catAttr_typeAlt_uom_attr_value { private string idField; private string nameField; private string uomField; private string valueField; ///  [System.Xml.Serialization.XmlAttributeAttribute()] public string id { get { return this.idField; } set { this.idField = value; } } ///  [System.Xml.Serialization.XmlAttributeAttribute()] public string name { get { return this.nameField; } set { this.nameField = value; } } ///  [System.Xml.Serialization.XmlAttributeAttribute()] public string uom { get { return this.uomField; } set { this.uomField = value; } } ///  [System.Xml.Serialization.XmlTextAttribute()] public string Value { get { return this.valueField; } set { this.valueField = value; } } } ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class fileClassPartDocInfoList { private string isDocCADFilesAvailableField; private fileClassPartDocInfoListDocInfo[] docInfoField; ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string isDocCADFilesAvailable { get { return this.isDocCADFilesAvailableField; } set { this.isDocCADFilesAvailableField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute("docInfo", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public fileClassPartDocInfoListDocInfo[] docInfo { get { return this.docInfoField; } set { this.docInfoField = value; } } } ///  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)] public partial class fileClassPartDocInfoListDocInfo { private string docTypeField; private string docIdField; private string docRevisionField; private string docTitleField; private string docSubTypeField; private string docFileNameField; private string docFormatField; private string docSizeField; private string docLangField; private string docUrlField; ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string docType { get { return this.docTypeField; } set { this.docTypeField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string docId { get { return this.docIdField; } set { this.docIdField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string docRevision { get { return this.docRevisionField; } set { this.docRevisionField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string docTitle { get { return this.docTitleField; } set { this.docTitleField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string docSubType { get { return this.docSubTypeField; } set { this.docSubTypeField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string docFileName { get { return this.docFileNameField; } set { this.docFileNameField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string docFormat { get { return this.docFormatField; } set { this.docFormatField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string docSize { get { return this.docSizeField; } set { this.docSizeField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string docLang { get { return this.docLangField; } set { this.docLangField = value; } } ///  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] public string docUrl { get { return this.docUrlField; } set { this.docUrlField = value; } } } 

如果要创建XML可序列化类并控制xml的格式,则可以使用xsd.exe生成要传递到XmlSerializer实例的类型。

听起来您希望接收XML,然后将其反序列化为类实例,以便您可以使用它们来填充网站上的各种控件。

所以你可以这样做:

 // Create an instance of the XmlSerializer. XmlSerializer serializer = new XmlSerializer(typeof(MyType)); // Read the xml. Stream reader= new FileStream(@".\MyXmlFile.xml", FileMode.Open); // Call the Deserialize method to restore the object's state. MyType myType = (MyType) serializer.Deserialize(reader); 

在你的情况下,你会做类似的事情:

 file f = (file) serializer.Deserialize(reader); // Then you can use your "file" instance: foreach (fileClass fc in f.itemsField) { //.. Do something here }