Customizing XML Serialization with IXmlSerializable

Last post 11-08-2009 5:38 AM by Martin.Lukas. 0 replies.

Sort Posts:

  • Customizing XML Serialization with IXmlSerializable

    11-08-2009, 5:38 AM
    • Member
      4 point Member
    • Martin.Lukas
    • Member since 09-13-2009, 11:45 AM
    • Posts 34

    Hi, I am begginer and I have one problem. I read tutorial about  Customizing XML Serialization with IXmlSerializable from

    http://www.c-sharpcorner.com/UploadFile/prvn_131971/WebServiceStandardsandExtensions02072008073635AM/WebServiceStandardsandExtensions.aspx

    Class which is serialize :

     

    public class EmployeeDetailsCustom : IXmlSerializable
    
    {
    
        public int ID;
    
        public string FirstName;
    
        public string LastName;
    
        const string ns = "http://www.apress.com/ProASP.NET/CustomEmployeeDetails";
    
        void IXmlSerializable.WriteXml(XmlWriter w)
    
        {
    
            w.WriteStartElement("Employee", ns);
    
            w.WriteStartElement("Name", ns);
    
            w.WriteElementString("First", ns, FirstName);
    
            w.WriteElementString("Last", ns, LastName);
    
            w.WriteEndElement();
    
            w.WriteElementString("ID", ns, ID.ToString());
    
            w.WriteEndElement();
    
        }
    
        void IXmlSerializable.ReadXml(XmlReader r)
    
        {
    
            r.MoveToContent();
    
            r.ReadStartElement("Employee");
    
            r.ReadStartElement("Name");
    
            FirstName = r.ReadElementString("First", ns);
    
            LastName = r.ReadElementString("Last", ns);
    
            r.ReadEndElement();
    
            r.MoveToContent();
    
            ID = Int32.Parse(r.ReadElementString("ID", ns));
    
            reader.ReadEndElement();
    
        }
    
        System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
    
        {
    
            return null;
    
        }
    
        // (Constructors omitted.)
    
    }


     

    Method which return XML scheme as XmlQualifiedName object .

    public static XmlQualifiedName GetSchemaDocument(XmlSchemaSet xs)
    
    {
    
       // Get the path to the schema file.
    
       string schemaPath = HttpContext.Current.Server.MapPath("EmployeeDetails.xsd");
    
       // Retrieve the schema from the file.
    
       XmlSerializer schemaSerializer = new XmlSerializer(typeof(XmlSchema));
    
       XmlSchema s = (XmlSchema)schemaSerializer.Deserialize(new XmlTextReader(schemaPath), null);
    
       xs.XmlResolver = new XmlUrlResolver();
    
       xs.Add(s);
    
       return new XmlQualifiedName("EmployeeDetails", ns);
    
    }


     

    Implementation on web method.

     

    [XmlSchemaProvider("GetSchemaDocument")]
    
    public class EmployeeDetailsCustom : IXmlSerializable
    
    { ... }


    The method  public static XmlQualifiedName GetSchemaDocument load xml scheme from file. My question is how can I get xml scheme from my own object. It must be wrote be me , or can be generated with some utility or object from .NET ?

    What is the simple way how get xml scheme from my own object. Thank..

    Filed under:
Page 1 of 1 (1 items)