4 Ocak 2012 Çarşamba

XmlSerializer

XmlSerializer kullanımı konusunu biraz daha açalım.
Bir datatable’i XmlSerializer ile serileştirirsek oluşacak çıktı şu şekilde olmaktadır:

<?xml version="1.0" encoding="utf-16"?>
<DataTable>
  <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="dtt" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="dtt">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Id" TYPE="xs:int" minOccurs="0" />
                <xs:element name="Ad" TYPE="xs:string" minOccurs="0" />
                <xs:element name="Soyad" TYPE="xs:string" minOccurs="0" />
                <xs:element name="Adres" TYPE="xs:string" minOccurs="0" />
                <xs:element name="Yas" TYPE="xs:int" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <DocumentElement>
      <dtt diffgr:id="dtt1" msdata:rowOrder="0">
        <Id>0</Id>
        <Ad>Ad0</Ad>
        <Soyad>Soyad0</Soyad>
        <Adres>Adres0</Adres>
        <Yas>0</Yas>
      </dtt>
      <dtt diffgr:id="dtt2" msdata:rowOrder="1">
        <Id>1</Id>
        <Ad>Ad1</Ad>
        <Soyad>Soyad1</Soyad>
        <Adres>Adres1</Adres>
        <Yas>1</Yas>
      </dtt>
      <dtt diffgr:id="dtt3" msdata:rowOrder="2">
        <Id>2</Id>
        <Ad>Ad2</Ad>
        <Soyad>Soyad2</Soyad>
        <Adres>Adres2</Adres>
        <Yas>2</Yas>
      </dtt>
    </DocumentElement>
  </diffgr:diffgram>
</DataTable>

Dikkat ederseniz metadata, gerçek data kadar yer kaplıyor.

DataTable’de bazı dataların değiştirilip AcceptChanges denmemesi durumunda ise serileştirmeye DiffGram da ekleniyor ve çıktı şu şekilde oluyor:

<?xml version="1.0" encoding="utf-16"?><DataSet>
  <xs:schema id="ds" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xs:element name="ds" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
        <xs:choice minOccurs="0" maxOccurs="unbounded">
          <xs:element name="dtt">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="Id" type="xs:int" minOccurs="0" />
                <xs:element name="Ad" type="xs:string" minOccurs="0" />
                <xs:element name="Soyad" type="xs:string" minOccurs="0" />
                <xs:element name="Adres" type="xs:string" minOccurs="0" />
                <xs:element name="Yas" type="xs:int" minOccurs="0" />
              </xs:sequence>
            </xs:complexType>
          </xs:element>
        </xs:choice>
      </xs:complexType>
    </xs:element>
  </xs:schema>
  <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    <ds>
      <dtt diffgr:id="dtt1" msdata:rowOrder="0" diffgr:hasChanges="modified">
        <Id>45</Id>
        <Ad>Ad0</Ad>
        <Soyad>Soyad0</Soyad>
        <Adres>Adres0</Adres>
        <Yas>0</Yas>
      </dtt>
      <dtt diffgr:id="dtt2" msdata:rowOrder="1" diffgr:hasChanges="modified">
        <Id>1</Id>
        <Ad>Addddddd</Ad>
        <Soyad>Soyad1</Soyad>
        <Adres>Adres1</Adres>
        <Yas>1</Yas>
      </dtt>
      <dtt diffgr:id="dtt3" msdata:rowOrder="2" diffgr:hasChanges="modified">
        <Id>2</Id>
        <Ad>Ad2</Ad>
        <Soyad>Soyad2</Soyad>
        <Adres>Adres2</Adres>
        <Yas>100</Yas>
      </dtt>
    </ds>
    <diffgr:before>
      <dtt diffgr:id="dtt1" msdata:rowOrder="0">
        <Id>0</Id>
        <Ad>Ad0</Ad>
        <Soyad>Soyad0</Soyad>
        <Adres>Adres0</Adres>
        <Yas>0</Yas>
      </dtt>
      <dtt diffgr:id="dtt2" msdata:rowOrder="1">
        <Id>1</Id>
        <Ad>Ad1</Ad>
        <Soyad>Soyad1</Soyad>
        <Adres>Adres1</Adres>
        <Yas>1</Yas>
      </dtt>
      <dtt diffgr:id="dtt3" msdata:rowOrder="2">
        <Id>2</Id>
        <Ad>Ad2</Ad>
        <Soyad>Soyad2</Soyad>
        <Adres>Adres2</Adres>
        <Yas>2</Yas>
      </dtt>
    </diffgr:before>
  </diffgr:diffgram>
</DataSet>

Aynı datatable’i WriteXml metodu ile serileştirirsek çıktımız oldukça sade hale gelmekte:

<DocumentElement>
  <dtt>
    <Id>0</Id>
    <Ad>Ad0</Ad>
    <Soyad>Soyad0</Soyad>
    <Adres>Adres0</Adres>
    <Yas>0</Yas>
  </dtt>
  <dtt>
    <Id>1</Id>
    <Ad>Ad1</Ad>
    <Soyad>Soyad1</Soyad>
    <Adres>Adres1</Adres>
    <Yas>1</Yas>
  </dtt>
  <dtt>
    <Id>2</Id>
    <Ad>Ad2</Ad>
    <Soyad>Soyad2</Soyad>
    <Adres>Adres2</Adres>
    <Yas>2</Yas>
  </dtt>
</DocumentElement>

Tarık

Hiç yorum yok:

Yorum Gönder