none
XML, Xsd, C# RRS feed

  • Question

  • hello every one,

    how can i deserelize an xsd generated class in order to use it?

    here is my class. I have to show the elements in a combo box.

    thanks for support :) 

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     Ce code a été généré par un outil.
    //     Version du runtime :4.0.30319.42000
    //
    //     Les modifications apportées à ce fichier peuvent provoquer un comportement incorrect et seront perdues si
    //     le code est régénéré.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    using System.Xml.Serialization;
    
    // 
    // This source code was auto-generated by xsd, Version=4.7.2046.0.
    // 
    
    namespace Xml_read.Model
    {
        /// <remarks/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.7.2046.0")]
    [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 voitures {
        
        private voituresVoiture[] itemsField;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("voiture", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public voituresVoiture[] Items {
            get {
                return this.itemsField;
            }
            set {
                this.itemsField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.7.2046.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class voituresVoiture {
        
        private string nb_voituresField;
        
        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string nb_voitures {
            get {
                return this.nb_voituresField;
            }
            set {
                this.nb_voituresField = value;
            }
        }
    }
    }

    Tuesday, April 9, 2019 7:36 AM

Answers

  • It depends a little bit on the form of XML, whether it is a file or a string or a stream..

    // From file.
    string fileName = @"C:\Path\YourFile.Xml";
    XmlSerializer serializer = new XmlSerializer(typeof(voitures));
    using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
    {
        voitures result = (voitures)serializer.Deserialize(fileStream);
    }
    
    // From string.
    string sampleString = @"<voitures>..</voitures>"; // Correct XML needed ;)
    XmlSerializer serializer = new XmlSerializer(typeof(voitures));
    using (TextReader reader = new StringReader(sampleString))
    {
        voitures result = (voitures )serializer.Deserialize(reader);
    }

    See Xml​Serializer.​Deserialize.

    • Marked as answer by Bil59650 Thursday, April 11, 2019 12:27 PM
    Tuesday, April 9, 2019 7:55 AM

All replies

  • It depends a little bit on the form of XML, whether it is a file or a string or a stream..

    // From file.
    string fileName = @"C:\Path\YourFile.Xml";
    XmlSerializer serializer = new XmlSerializer(typeof(voitures));
    using (FileStream fileStream = new FileStream(fileName, FileMode.Open))
    {
        voitures result = (voitures)serializer.Deserialize(fileStream);
    }
    
    // From string.
    string sampleString = @"<voitures>..</voitures>"; // Correct XML needed ;)
    XmlSerializer serializer = new XmlSerializer(typeof(voitures));
    using (TextReader reader = new StringReader(sampleString))
    {
        voitures result = (voitures )serializer.Deserialize(reader);
    }

    See Xml​Serializer.​Deserialize.

    • Marked as answer by Bil59650 Thursday, April 11, 2019 12:27 PM
    Tuesday, April 9, 2019 7:55 AM
  • hello sir, 

    it is a file.

    it actualy works with the first sollution. thank you! :) 

    Tuesday, April 9, 2019 8:56 AM