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.

  • 相关阅读:
    数据库被注入daxia123原因及解决办法
    Alipay数字证书管理员权限问题
    关闭数据库的xp_cmdshell命令以防止黑客攻击
    如何使用JavaScript来写ASP程序
    VBscript操作DOM
    如何做好性能压测丨压测环境设计和搭建
    10倍性能提升!DLA SQL推出基于Alluxio的数据湖分析加速功能
    高德地图驾车导航内存优化原理与实战
    「直播实录」中英数据库专家谈:数据库的过去、未来和现在
    Flink 助力美团数仓增量生产
  • 原文地址:https://www.cnblogs.com/vincentDr/p/3380479.html
Copyright © 2011-2022 走看看