How to create data serializer/deserializer according to XSD-description with Visual Studio?

Step 1

Create XSD-definition of classes containing data.

Example

<xs:complexType name="ParagraphExtractorType">
  <xs:complexContent>
    <xs:extension base="htmlCleanup:TextProcessorType">
      <xs:sequence>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

Step 2

Create XSD-definition of container class representing entire data.

Example

<xs:element name="HTMLCleanupConfig">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="TextProcessorConfig" 
        type="htmlCleanup:TextProcessorType">
      </xs:element>
      <xs:element name="ParagraphExtractorConfig" 
        type="htmlCleanup:ParagraphExtractorType">
      </xs:element>
      <xs:element name="SpecialHTMLRemoverConfig" 
        type="htmlCleanup:SpecialHTMLRemoverType">
      </xs:element>
      <xs:element name="InnerTagRemoverConfig" 
        type="htmlCleanup:InnerTagRemoverType">
      </xs:element>
      <xs:element name="TagWithTextRemoverConfig" 
        type="htmlCleanup:TagWithTextRemoverType">
      </xs:element>
      <xs:element name="URLFormatterConfig" 
        type="htmlCleanup:URLFormatterType">
      </xs:element>
      <xs:element name="TextFormatterConfig" 
        type="htmlCleanup:TextFormatterType">
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

Step 3

Generate classes from XSD-schema.

How to create classes from XSD-schema with Visual Studio?

Example

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, 
  Namespace="urn:xmlns:HTMLCleanupConfig")]
[System.Xml.Serialization.XmlRootAttribute(
  Namespace="urn:xmlns:HTMLCleanupConfig", IsNullable=false)]
public partial class HTMLCleanupConfig {
  private TextProcessorType textProcessorConfigField;
  private ParagraphExtractorType paragraphExtractorConfigField;
  private SpecialHTMLRemoverType specialHTMLRemoverConfigField;
  private InnerTagRemoverType innerTagRemoverConfigField;
  private TagWithTextRemoverType tagWithTextRemoverConfigField;
  private URLFormatterType uRLFormatterConfigField;
  private TextFormatterType textFormatterConfigField;

/// <remarks/>
public TextProcessorType TextProcessorConfig {
  get {
    return this.textProcessorConfigField;
  }
  set {
    this.textProcessorConfigField = value;
  }
}

/// <remarks/>
public ParagraphExtractorType ParagraphExtractorConfig {
  get {
    return this.paragraphExtractorConfigField;
  }
  set {
    this.paragraphExtractorConfigField = value;
  }
}

/// <remarks/>
public SpecialHTMLRemoverType SpecialHTMLRemoverConfig {
  get {
    return this.specialHTMLRemoverConfigField;
  }
  set {
    this.specialHTMLRemoverConfigField = value;
  }
}

/// <remarks/>
public InnerTagRemoverType InnerTagRemoverConfig {
  get {
    return this.innerTagRemoverConfigField;
  }
  set {
    this.innerTagRemoverConfigField = value;
  }
}

/// <remarks/>
public TagWithTextRemoverType TagWithTextRemoverConfig {
  get {
    return this.tagWithTextRemoverConfigField;
  }
  set {
    this.tagWithTextRemoverConfigField = value;
  }
}

/// <remarks/>
public URLFormatterType URLFormatterConfig {
  get {
    return this.uRLFormatterConfigField;
  }
  set {
    this.uRLFormatterConfigField = value;
  }
}

/// <remarks/>
public TextFormatterType TextFormatterConfig {
  get {
    return this.textFormatterConfigField;
  }
  set {
    this.textFormatterConfigField = value;
  }
}
}

Step 4

Create class implementing methods for serialization / deserialization of container object.

Example

public void Deserialize(string fileName)
{
  var config = new HTMLCleanupConfig();
  using (var reader = new StreamReader(fileName))
  {
    var serializer = new XmlSerializer(typeof(HTMLCleanupConfig));
    config = (HTMLCleanupConfig)serializer.Deserialize(reader);
  }
}

public void Serialize(string fileName)
{
  var config = new HTMLCleanupConfig();
  using (var writer = new StreamWriter(fileName))
  {
    var serializer = new XmlSerializer(typeof(HTMLCleanupConfig));
    serializer.Serialize(writer, config);
  }
}

Examples are taken from pdf service .

 

Advertisements

How to define inheritance in XSD-schema?

Define base class

<xs:complexType name="TextProcessorType">
  <xs:sequence>
    <xs:element name="Skipped" type="xs:boolean" />
  </xs:sequence>
</xs:complexType>

Define inherited class

<xs:complexType name="ParagraphExtractorType">
  <xs:complexContent>
    <xs:extension base="htmlCleanup:TextProcessorType">
      <xs:sequence>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

Full example (taken from pdf service)

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:htmlCleanup="urn:xmlns:HTMLCleanupConfig" 
  elementFormDefault="qualified" 
  targetNamespace="urn:xmlns:HTMLCleanupConfig" 
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="HTMLCleanupConfig">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="TextProcessorConfig" 
        type="htmlCleanup:TextProcessorType">
      </xs:element>
      <xs:element name="ParagraphExtractorConfig" 
        type="htmlCleanup:ParagraphExtractorType">
      </xs:element>
      <xs:element name="SpecialHTMLRemoverConfig"
        type="htmlCleanup:SpecialHTMLRemoverType">
      </xs:element>
      <xs:element name="InnerTagRemoverConfig"
        type="htmlCleanup:InnerTagRemoverType">
      </xs:element>
      <xs:element name="TagWithTextRemoverConfig"
        type="htmlCleanup:TagWithTextRemoverType">
      </xs:element>
      <xs:element name="URLFormatterConfig"
        type="htmlCleanup:URLFormatterType">
      </xs:element>
      <xs:element name="TextFormatterConfig"
        type="htmlCleanup:TextFormatterType">
      </xs:element>
    </xs:sequence>
  </xs:complexType>
</xs:element>

<xs:complexType name="TextProcessorType">
  <xs:sequence>
    <xs:element name="Skipped" type="xs:boolean" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="ParagraphExtractorType">
  <xs:complexContent>
    <xs:extension base="htmlCleanup:TextProcessorType">
      <xs:sequence>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="SpecialHTMLRemoverType">
  <xs:complexContent>
    <xs:extension base="htmlCleanup:TextProcessorType">
      <xs:sequence>
        <xs:element name="SpecialHTML" 
          type="htmlCleanup:SpecialHTMLSymbolListType" />
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="SpecialHTMLSymbolListType">
  <xs:sequence>
    <xs:element maxOccurs="unbounded" name="SpecialHTMLSymbol"
      type="htmlCleanup:SpecialHTMLSymbolType" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="SpecialHTMLSymbolType">
  <xs:sequence>
    <xs:element name="SpecialHTML" type="xs:string" />
    <xs:element name="Replacement" type="xs:string" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="InnerTagRemoverType">
  <xs:complexContent>
    <xs:extension base="htmlCleanup:TextProcessorType">
      <xs:sequence>
        <xs:element name="Tags" type="htmlCleanup:TagToRemoveListType" />
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="TagToRemoveListType">
  <xs:sequence>
    <xs:element maxOccurs="unbounded" 
      name="Tag" type="htmlCleanup:TagToRemoveType" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="TagToRemoveType">
  <xs:sequence>
    <xs:element name="StartTagWithoutBracket" type="xs:string" />
    <xs:element name="EndTag" type="xs:string" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="TagWithTextRemoverType">
  <xs:complexContent>
    <xs:extension base="htmlCleanup:TextProcessorType">
      <xs:sequence>
        <xs:element name="Tags" type="htmlCleanup:TagToRemoveListType" />
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="URLFormatterType">
  <xs:complexContent>
    <xs:extension base="htmlCleanup:TextProcessorType">
      <xs:sequence>
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="TextFormatterType">
  <xs:complexContent>
    <xs:extension base="htmlCleanup:TextProcessorType">
      <xs:sequence>
        <xs:element name="Delimiters" 
          type="htmlCleanup:DelimiterListType" />
      </xs:sequence>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>

<xs:complexType name="DelimiterListType">
  <xs:sequence>
    <xs:element maxOccurs="unbounded" 
      name="DelimiterSymbol" type="htmlCleanup:DelimiterSymbolType" />
  </xs:sequence>
</xs:complexType>

<xs:complexType name="DelimiterSymbolType">
  <xs:sequence>
    <xs:element name="SymbolCode" type="xs:unsignedByte" />
  </xs:sequence>
</xs:complexType>
</xs:schema>

Read more

How to create classes from XSD-schema with Visual Studio?

Open Developer Command Prompt for Visual Studio and execute

> xsd xsd_file_name /classes /namespace:resulting_namespace

For example

> xsd config.xsd /classes /namespace:HTMLCleanup.Config

As result you will get config.cs containing set of classes having structure according to XSD-description.

XML Schema Definition Tool (Xsd.exe)

Comparison of XSD Code Generators

Xsd2Code