Scambio dati in formato XML

L’XML è ormai da diverso tempo il formato che conviene utilizzare se in un’applicazione c’è la necessità di scambiare dati per esportazione o importazione.

Nel caso occorra gestire uno scambio dati custom la cosa più veloce è utilizzare un Dataset con le DataTable necessarie da riempire con i dati necessari.

Nel caso che il record sia composto da valori nulli si noti che per default i relativi attributi non saranno riportati nell’XML:

image

ID

Description

ValueInteger

ValueString

1

First

10

Test

2

Second

 

 

Di seguito ad esempio il codice per riempire il La DataTable del DataSet con i valori di esempio mostrati in tabella

Dim newRow = Me.dstExport.SampleData.NewSampleDataRow()
newRow.ID = 1
newRow.Description = “First”
newRow.ValueInteger = 10
newRow.ValueString = “Test”
Me.dstExport.SampleData.AddSampleDataRow(newRow)

newRow = Me.dstExport.SampleData.NewSampleDataRow()
newRow.ID = 2
newRow.Description = “Second”
Me.dstExport.SampleData.AddSampleDataRow(newRow)

Me.dstExport.WriteXml(“C:\temp\Export.xml”)

Come detto precedentemente il secondo record non avrà gli attributi ValueInteger e ValueString in quanto sono nulli:

<?xml version=”1.0″ standalone=”yes”?>
<ExportDataset xmlns=”http://tempuri.org/ExportDataset.xsd”>
  <SampleData>
    <ID>1</ID>
    <Description>First</Description>
    <ValueInteger>10</ValueInteger>
    <ValueString>Test</ValueString>
  </SampleData>
  <SampleData>
    <ID>2</ID>
    <Description>Second</Description>
  </SampleData>
</ExportDataset>

Il comportamento è by design, ma si può ovviare includendo nel file xml la definizione dello schema, in questo modo chi dovrà trattare il file avrà la descrizione precisa dei campi anche in termini di tipo di dato ed eventualmente di valori ammessi a null o meno:

Me.dstExport.WriteXml(“C:\temp\Export.xml”, XmlWriteMode.WriteSchema)

<?xml version=”1.0″ standalone=”yes”?>
<ExportDataset xmlns=”http://tempuri.org/ExportDataset.xsd”>
  <xs:schema id=”ExportDataset” targetNamespace=”http://tempuri.org/ExportDataset.xsd” xmlns:mstns=”http://tempuri.org/ExportDataset.xsd” xmlns=”http://tempuri.org/ExportDataset.xsd” xmlns:xs=”http://www.w3.org/2001/XMLSchema” xmlns:msdata=”urn:schemas-microsoft-com:xml-msdata” attributeFormDefault=”qualified” elementFormDefault=”qualified”>
    <xs:element name=”ExportDataset” msdata:IsDataSet=”true” msdata:UseCurrentLocale=”true”>
      <xs:complexType>
        <xs:choice minOccurs=”0″ maxOccurs=”unbounded”>
          <xs:element name=”SampleData”>
            <xs:complexType>
              <xs:sequence>
                <xs:element name=”ID” type=”xs:int” minOccurs=”0″ />
                <xs:element name=”Description” type=”xs:string” minOccurs=”0″ />
                <xs:element name=”ValueInteger” type=”xs:int” minOccurs=”0″ />
                <xs:element name=”ValueString” type=”xs:string” minOccurs=”0″ />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <SampleData>
    <ID>1</ID>
    <Description>First</Description>
    <ValueInteger>10</ValueInteger>
    <ValueString>Test</ValueString>
  </SampleData>
  <SampleData>
    <ID>2</ID>
    <Description>Second</Description>
  </SampleData>
</ExportDataset>

Per ulteriori informazioni si vedano le seguenti KB che sebbene siano un po’ datate sono ancora valide: