zoukankan      html  css  js  c++  java
  • Auto generating Entity classes with xsd.exe for XML Serialization and De-Serialization

    More info here: http://blogs.msdn.com/b/yojoshi/archive/2011/05/14/xml-serialization-and-deserialization-entity-classes-with-xsd-exe.aspx

    Introduction

    Many time we come across the need of parsing XML document and creating entities. These entities are later passed between multiple methods, stored as configuration or used to perform some manipulation. Hand wrting code for parsing these entities from XML document is tedious. This article presents and easy way to parse XML documents and generating deserializers.

    Hands on!

    Lets create a sample XML document named Students.xml following is the sample content.

    <xml version="1.0" encoding="utf-8" ?>
    <Students>
      <Student>
        <RollNo>1</RollNo>
        <Name>Student 1</Name>
        <Address>Xyz Street</Address>
      </Student>
      <Student>
        <RollNo>2</RollNo>
        <Name>Student 2</Name>
        <Address>Xyz Street</Address>
      </Student>
      <Student>
        <RollNo>3</RollNo>
        <Name>Student 3</Name>
        <Address>Xyz Street</Address>
      </Student>
      <Student>
        <RollNo>4</RollNo>
        <Name>Student 4</Name>
        <Address>Xyz Street</Address>
      </Student>
      <Student>
        <RollNo>5</RollNo>
        <Name>Student 5</Name>
        <Address>Xyz Street</Address>
      </Student>
    </Students>

    Here, as we can see, there is one root node named <Students> and it contains <Student> nodes as children. Now, what if we need to create a list of students in our program and bind it to some grid? We have to write manual parsing code for achieving same. But, you can easily achieve this task using xsd.exe to generate de-serializer and get the student collection. Following are the steps

    1. Start Visual Studio and open Student.xml 
    2. Click on "XML" -> "Create Schema" on toolbar



    3.  It will generate XML Schema. Following is the sample schema generated for Students.xml file

      <?xml version="1.0" encoding="utf-8"?>
      <xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:element name="Students">
          <xs:complexType>
            <xs:sequence>
              <xs:element maxOccurs="unbounded" name="Student">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="RollNo" type="xs:unsignedByte" />
                    <xs:element name="Name" type="xs:string" />
                    <xs:element name="Address" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:schema>



    4. Here we can observe the datatypes are automatically set as per the data available in Student.xml As per your need you can edit the schema and set the appropriate data types.
    5. Save the Students.xml and Students.xsd files inside some folder say "C:SampleXML"
    6. Now, we have to start "Visual Studio Command Prompt"
    7. On VS Command Prompt, cd to "C:SampleXML" directory and type "xsd Students.xsd /c"

    8. This will generate "Students.cs" file that we can use to de-serialize Student.xml file
    9. Now create a "Console Application" named "XmlDeSerializer" we will use this to test the de-serialization
    10. Add "Students.cs" file that we have created using xsd.exe file

    11. Copy and paste following code in Program.cs file and run the program.
      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Xml;
      using System.IO;
      using System.Xml.Serialization;
      
      namespace XmlDeSerializer
      {
          class Program
          {
              static void Main(string[] args)
              {
                  using (FileStream xmlStream = new FileStream("C:\Sample\XML\Students.xml", FileMode.Open))
                  {
                      using (XmlReader xmlReader = XmlReader.Create(xmlStream))
                      {
                          XmlSerializer serializer = new XmlSerializer(typeof(Students));
                          Students deserializedStudents = serializer.Deserialize(xmlReader) as Students;
                          foreach (var student in deserializedStudents.Student)
                          {
                              Console.WriteLine("Roll No : {0}", student.RollNo);
                              Console.WriteLine("Name : {0}", student.Name);
                              Console.WriteLine("Address : {0}", student.Address);
                              Console.WriteLine("");
                          }
                      }
                  }
              }
          }
      }


    12. You will see following output

    In this way we have successfully de-serialized Students.xml file and created entities. You May also serialize existing entites back in the xml format, we can use "Serialize" method of XmlSerializer.

  • 相关阅读:
    博弈论专题(持续更新)
    数论专题(持续更新)
    树的遍历专题(持续更新)
    直线折线分割平面问题
    字典树模板 HDU1251
    差分数组——面对大数据的处理
    最短路——dijkstra算法
    并查集与最小生成树
    KMP的初步认识及题目分析
    Codeforces Round#636(Div.3) D题 差分数组
  • 原文地址:https://www.cnblogs.com/vincentDr/p/3380479.html
Copyright © 2011-2022 走看看