Generate XML based on XSD

0

I've got some XSD files here to use in an application I've developed ... I'd like to know how I can generate some sample XML in Visual Studio so I can put myself in the data export. When I try to do via Visual Studio in Generated XML example, it gives a "... namespace was not resolved" error message.

Following is an example of XSD.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:cadastroDomiciliar="http://esus.ufsc.br/v2000/cadastrodomiciliar" xmlns:condicaoMoradia="http://esus.ufsc.br/v2000/condicaomoradia" xmlns:enderecoLocalPermanencia="http://esus.ufsc.br/v2000/enderecolocalpermanencia" xmlns:familiaRow="http://esus.ufsc.br/v2000/familiarow" xmlns:headerCdsCadastro="http://esus.ufsc.br/v2000/headercdscadastro" targetNamespace="http://esus.ufsc.br/v2000/cadastrodomiciliar" version="1.0">

  <xs:import namespace="http://esus.ufsc.br/v2000/headercdscadastro" schemaLocation="v2000headercdscadastro.xsd"/>

  <xs:import namespace="http://esus.ufsc.br/v2000/enderecolocalpermanencia" schemaLocation="v2000enderecolocalpermanencia.xsd"/>

  <xs:import namespace="http://esus.ufsc.br/v2000/condicaomoradia" schemaLocation="v2000condicaomoradia.xsd"/>

  <xs:import namespace="http://esus.ufsc.br/v2000/familiarow" schemaLocation="v2000familiarow.xsd"/>

  <xs:element name="cadastroDomiciliarTransport" type="cadastroDomiciliar:cadastroDomiciliarTransport"/>

  <xs:complexType name="cadastroDomiciliarTransport">
    <xs:sequence>
      <xs:element maxOccurs="1" minOccurs="0" name="dadosGerais" type="headerCdsCadastro:headerCdsCadastroTransport"/>
      <xs:element maxOccurs="1" minOccurs="0" name="enderecoLocalPermanencia" type="enderecoLocalPermanencia:enderecoLocalPermanenciaTransport"/>
      <xs:element maxOccurs="1" minOccurs="0" name="condicaoMoradia" type="condicaoMoradia:condicaoMoradiaTransport"/>
      <xs:element maxOccurs="1" minOccurs="0" name="stAnimaisNoDomicilio" type="xs:boolean"/>
      <xs:element maxOccurs="unbounded" minOccurs="0" name="animaisNoDomicilio" type="xs:long"/>
      <xs:element maxOccurs="1" minOccurs="0" name="quantosAnimaisNoDomicilio" type="xs:string"/>
      <xs:element maxOccurs="unbounded" minOccurs="0" name="familias" type="familiaRow:familiaRowTransport"/>
      <xs:element maxOccurs="1" minOccurs="0" name="tpCdsOrigem" type="xs:int"/>
      <xs:element maxOccurs="1" name="uuid" type="xs:string"/>
      <xs:element maxOccurs="1" name="uuidFichaOriginadora" type="xs:string"/>
      <xs:element maxOccurs="1" minOccurs="0" name="fichaAtualizada" type="xs:boolean"/>
      <xs:element maxOccurs="1" minOccurs="0" name="statusTermoRecusaCadastroDomiciliarAtencaoBasica" type="xs:boolean"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
    
asked by anonymous 02.09.2015 / 14:16

2 answers

2

First step, put all your xsd in the same place, here we call:

Arquivo01.xsd // Não possui referencias
Arquivo02.xsd // Possui referencia a um tipo declarado no arquivo 01
Arquivo03.xsd // Possui referencia a um tipo declarado no arquivo 01
Arquivo04.xsd // Possui referencia a um tipo declarado no arquivo 02

Then open Visual Studio Command Propmt and run the following command.

xsd.exe Arquivo01.xsd Arquivo02.xsd Arquivo03.xsd Arquivo04.xsd /class

Note that the order you declare *.XSD is important, for example, if you put Arquivo02.xsd before Arquivo01.xsd , since Arquivo02.xsd has Arquivo01.xsd command would fire an exception.

But if you reverse the order of Arquivo02.xsd with Arquivo03.xsd , you will not have problems, after all, they will not depend on each other.

Now based on your file without some properties (after all I do not know what the structure of your imports is).

.xsd file

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:cadastroDomiciliar="http://esus.ufsc.br/v2000/cadastrodomiciliar" xmlns:condicaoMoradia="http://esus.ufsc.br/v2000/condicaomoradia" xmlns:enderecoLocalPermanencia="http://esus.ufsc.br/v2000/enderecolocalpermanencia" xmlns:familiaRow="http://esus.ufsc.br/v2000/familiarow" xmlns:headerCdsCadastro="http://esus.ufsc.br/v2000/headercdscadastro" targetNamespace="http://esus.ufsc.br/v2000/cadastrodomiciliar" version="1.0">      
  <xs:element name="cadastroDomiciliarTransport" type="cadastroDomiciliar:cadastroDomiciliarTransport"/>
  <xs:complexType name="cadastroDomiciliarTransport">
    <xs:sequence>
      <xs:element maxOccurs="1" minOccurs="0" name="stAnimaisNoDomicilio" type="xs:boolean"/>
      <xs:element maxOccurs="unbounded" minOccurs="0" name="animaisNoDomicilio" type="xs:long"/>
      <xs:element maxOccurs="1" minOccurs="0" name="quantosAnimaisNoDomicilio" type="xs:string"/>
      <xs:element maxOccurs="1" minOccurs="0" name="tpCdsOrigem" type="xs:int"/>
      <xs:element maxOccurs="1" name="uuid" type="xs:string"/>
      <xs:element maxOccurs="1" name="uuidFichaOriginadora" type="xs:string"/>
      <xs:element maxOccurs="1" minOccurs="0" name="fichaAtualizada" type="xs:boolean"/>
      <xs:element maxOccurs="1" minOccurs="0" name="statusTermoRecusaCadastroDomiciliarAtencaoBasica" type="xs:boolean"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

then by running the following command:

xsd.exe arquivo.xsd /class

the following file will be generated:

file.cs

//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

using System.Xml.Serialization;

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1.
// 


/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://esus.ufsc.br/v2000/cadastrodomiciliar")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://esus.ufsc.br/v2000/cadastrodomiciliar", IsNullable=false)]
public partial class cadastroDomiciliarTransport {

    private bool stAnimaisNoDomicilioField;

    private bool stAnimaisNoDomicilioFieldSpecified;

    private long[] animaisNoDomicilioField;

    private string quantosAnimaisNoDomicilioField;

    private int tpCdsOrigemField;

    private bool tpCdsOrigemFieldSpecified;

    private string uuidField;

    private string uuidFichaOriginadoraField;

    private bool fichaAtualizadaField;

    private bool fichaAtualizadaFieldSpecified;

    private bool statusTermoRecusaCadastroDomiciliarAtencaoBasicaField;

    private bool statusTermoRecusaCadastroDomiciliarAtencaoBasicaFieldSpecified;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public bool stAnimaisNoDomicilio {
        get {
            return this.stAnimaisNoDomicilioField;
        }
        set {
            this.stAnimaisNoDomicilioField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool stAnimaisNoDomicilioSpecified {
        get {
            return this.stAnimaisNoDomicilioFieldSpecified;
        }
        set {
            this.stAnimaisNoDomicilioFieldSpecified = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("animaisNoDomicilio", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public long[] animaisNoDomicilio {
        get {
            return this.animaisNoDomicilioField;
        }
        set {
            this.animaisNoDomicilioField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string quantosAnimaisNoDomicilio {
        get {
            return this.quantosAnimaisNoDomicilioField;
        }
        set {
            this.quantosAnimaisNoDomicilioField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public int tpCdsOrigem {
        get {
            return this.tpCdsOrigemField;
        }
        set {
            this.tpCdsOrigemField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool tpCdsOrigemSpecified {
        get {
            return this.tpCdsOrigemFieldSpecified;
        }
        set {
            this.tpCdsOrigemFieldSpecified = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string uuid {
        get {
            return this.uuidField;
        }
        set {
            this.uuidField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public string uuidFichaOriginadora {
        get {
            return this.uuidFichaOriginadoraField;
        }
        set {
            this.uuidFichaOriginadoraField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public bool fichaAtualizada {
        get {
            return this.fichaAtualizadaField;
        }
        set {
            this.fichaAtualizadaField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool fichaAtualizadaSpecified {
        get {
            return this.fichaAtualizadaFieldSpecified;
        }
        set {
            this.fichaAtualizadaFieldSpecified = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public bool statusTermoRecusaCadastroDomiciliarAtencaoBasica {
        get {
            return this.statusTermoRecusaCadastroDomiciliarAtencaoBasicaField;
        }
        set {
            this.statusTermoRecusaCadastroDomiciliarAtencaoBasicaField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool statusTermoRecusaCadastroDomiciliarAtencaoBasicaSpecified {
        get {
            return this.statusTermoRecusaCadastroDomiciliarAtencaoBasicaFieldSpecified;
        }
        set {
            this.statusTermoRecusaCadastroDomiciliarAtencaoBasicaFieldSpecified = value;
        }
    }
}

Now to create your xml file, use the following code:

var cadastro = new cadastroDomiciliarTransport();
/* preencha o seu objeto */

var xmlSerializer = new XmlSerializer(typeof(cadastroDomiciliarTransport));
using(var stringWriter = new StringWriter())
{
    using(var xmlWriter = XmlWriter.Create(stringWriter))
    {
        xmlSerializer.Serialize(xmlWriter, cadastro);
        var xml = stringWriter.ToString(); // Your XML
    }
}

In the example above I used a StringWriter , if you want to write a file, use FileStream and StreamWriter .

    
02.06.2016 / 14:03
-1

If xsd is valid, just open it in Visual Studio. An Xml Schema Explorer window will appear, then just right-click on one of the root elements and select the Generate Sample XML option.

    
02.09.2015 / 15:01