Generate XML based on XSD

Asked

Viewed 2,328 times

0

I received here some XSD files to use in an application I developed... I would like to know how to generate an example XML in Visual Studio to be able to locate me in the data export. When I try to do via Visual Studio in Generated XML example, it gives an error message "...namespace was not resolved".

The 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>

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 the Visual Studio Command Propmt and execute the following command.

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

Note that the order you declare the *.XSD it is important, for example, if you put up the Arquivo02.xsd before the Arquivo01.xsd, as the file Arquivo02.xsd has a department with Arquivo01.xsd, the command would fire an exception.

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

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

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 when executing the following command:

xsd.exe arquivo.xsd /class

the following file will be generated:

Cs file.

//------------------------------------------------------------------------------
// <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 the FileStream and a StreamWriter.

  • I have to look at the date of the question before publishing an answer,

-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 Generate Sample XML option.

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.