locked
Exclude schema when sending typed dataset through web services RRS feed

  • Question

  • Hi.

     

    The problem with sending typed dataset through web services is that it also sends the schema def. of the dataset, that can be very large if it reflects a big database.

    So i have tried to to not sending the schema with;

    myDataSet1.SchemaSerializationMode = SchemaSerializationMode.ExcludeSchema;

    But this aint working, it makes no difference to the XML being sent. The schema is there.

    And i have also tried:

    myDataSet1.RemotingFormat = SerializationFormat.Binary;

    with no luck.

    So anyone know how to turn off sending the schema def. when doing web service calls?.

     

    ;

     

     

     

    Tuesday, January 24, 2006 7:40 PM

Answers

  • I had to write code that does this about 1 hour ago :)

    If you don't need interop with other platforms you could do it like this:

    [WebMethod]

    public void UpdateDataSet(byte[] data)

    {

    try

    {

    MemoryStream ms = new MemoryStream(data);

    GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);

    DataSet dataSet = ... // get dataset from somewhere

    dataSet.ReadXml(zip, System.Data.XmlReadMode.Auto);

    zip.Close();

    dal.UpdateDataSet();

    ms.Close();

    } catch (Exception ex)

    {

    }

    }

    [WebMethod]

    public byte[] GetDataSet()

    {

    DataSet dataSet = ... // get dataset from somewhere

    MemoryStream ms = new MemoryStream();

    GZipStream zip = new GZipStream(ms, CompressionMode.Compress);

    dataSet.WriteXml(zip, System.Data.XmlWriteMode.IgnoreSchema);

    zip.Close();

    ms.Close();

    return ms.GetBuffer();

    }

    My client calls GetDataSet to get the DataSet from my web service, and UpdateDataSet to send changes back to the web service. I transfer the dataset as byte[] because I compress it to save network bandwidth. The disadvantage is that this is probably more difficult to use for a non-.NET client.

    Wednesday, January 25, 2006 12:13 AM
  • Hello,

    I think what you missed from Mathias' post is how he has code to serialize the dataset. There are a number of ways to serialize a dataset into XML representation. Take a look at the section titled "Serializing DataSet Objects to XML" of the following link

    You should consider having your web method also return a byte[] array or as a string, and then within your web method call the WriteXML method of the DataSet object and passing in the System.Data.XmlWriteMode.IgnoreSchema enumerator. Take a look at Mathias' code sample again or reference
    DataSet.WriteXml Method on MSDN.

    hope this helps
    -Todd Foust

    Friday, January 27, 2006 2:07 AM
    Moderator

All replies

  • I had to write code that does this about 1 hour ago :)

    If you don't need interop with other platforms you could do it like this:

    [WebMethod]

    public void UpdateDataSet(byte[] data)

    {

    try

    {

    MemoryStream ms = new MemoryStream(data);

    GZipStream zip = new GZipStream(ms, CompressionMode.Decompress);

    DataSet dataSet = ... // get dataset from somewhere

    dataSet.ReadXml(zip, System.Data.XmlReadMode.Auto);

    zip.Close();

    dal.UpdateDataSet();

    ms.Close();

    } catch (Exception ex)

    {

    }

    }

    [WebMethod]

    public byte[] GetDataSet()

    {

    DataSet dataSet = ... // get dataset from somewhere

    MemoryStream ms = new MemoryStream();

    GZipStream zip = new GZipStream(ms, CompressionMode.Compress);

    dataSet.WriteXml(zip, System.Data.XmlWriteMode.IgnoreSchema);

    zip.Close();

    ms.Close();

    return ms.GetBuffer();

    }

    My client calls GetDataSet to get the DataSet from my web service, and UpdateDataSet to send changes back to the web service. I transfer the dataset as byte[] because I compress it to save network bandwidth. The disadvantage is that this is probably more difficult to use for a non-.NET client.

    Wednesday, January 25, 2006 12:13 AM
  • Hi, thanks for your answer, but i just want to exclude the schema and just send the values i have in the dataset.
    Thursday, January 26, 2006 6:22 PM
  • That's what my sample does. Just remove the compression if you don't need it.
    Thursday, January 26, 2006 6:45 PM
  •  

     

     

    Thanks for your sample but i want the XML to look like this:

     

      <?xml version="1.0" encoding="utf-8" ?>
    - <ShopDataSet xmlns="http://tempuri.org/">
    - <xs:schema id="ShopDataSet" targetNamespace="http://tempuri.org/ShopDataSet.xsd" xmlns:mstns="http://tempuri.org/ShopDataSet.xsd" xmlns="http://tempuri.org/ShopDataSet.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
    -  <customersID>1f295ad2-c622-4d59-a8d5-6e84a588f5a7</customersID>
      <customersname>Mehmet</customersname>
      </customers>
    - <customers diffgr:id="customers2" msdata:rowOrder="1">
      <customersID>c100d5fb-e96c-4e83-a328-7d8ad8817be1</customersID>
      <customersname>Peter2</customersname>
      </customers>
    - <customers diffgr:id="customers3" msdata:rowOrder="2">
      <customersID>8ff0014d-06c2-4c86-95cd-7e647c3534a7</customersID>
      <customersname>Dan2</customersname>
      </customers>
    - <products diffgr:id="products1" msdata:rowOrder="0">
      <productsID>20fb34a5-573f-4de4-8c11-15df3acca2ca</productsID>
      <productname>Apples</productname>
      </products>
    - <products diffgr:id="products2" msdata:rowOrder="1">
      <productsID>b1df617f-cc28-4c7c-a746-2a85e3b26d94</productsID>
      <productname>Strawberrys</productname>
      </products>
    - <products diffgr:id="products3" msdata:rowOrder="2">
      <productsID>6826a6d3-8ffd-4677-b378-8215ec4915dc</productsID>
      <productname>Blue berrys</productname>
      </products>
    - <products diffgr:id="products4" msdata:rowOrder="3">
      <productsID>ae5f421c-31fa-4edc-a6e0-ad7bd1fc3cfd</productsID>
      <productname>Banana</productname>
      </products>
    - <orderdetails diffgr:id="orderdetails1" msdata:rowOrder="0">
      <orderdetailsID>f0c8dfa2-cc29-40b5-a46e-18931b9b06eb</orderdetailsID>
      <ordersID>aac88cce-6ac9-4e1d-9306-3ee18cf719ae</ordersID>
      <productsID>6826a6d3-8ffd-4677-b378-8215ec4915dc</productsID>
      </orderdetails>
    - <orderdetails diffgr:id="orderdetails2" msdata:rowOrder="1">
      <orderdetailsID>ae607169-7243-47ec-969b-585178bd97e1</orderdetailsID>
      <ordersID>aac88cce-6ac9-4e1d-9306-3ee18cf719ae</ordersID>
      <productsID>b1df617f-cc28-4c7c-a746-2a85e3b26d94</productsID>
      </orderdetails>
    - <orderdetails diffgr:id="orderdetails3" msdata:rowOrder="2">
      <orderdetailsID>3fe606dd-40cb-4094-a919-58fa7d178441</orderdetailsID>
      <ordersID>aac88cce-6ac9-4e1d-9306-3ee18cf719ae</ordersID>
      <productsID>ae5f421c-31fa-4edc-a6e0-ad7bd1fc3cfd</productsID>
      </orderdetails>
    - <orderdetails diffgr:id="orderdetails4" msdata:rowOrder="3">
      <orderdetailsID>dd2678f8-a0b7-4a6e-8554-ad515cbb6469</orderdetailsID>
      <ordersID>b88293d4-c46c-4c2b-bf79-21645d5b205b</ordersID>
      <productsID>20fb34a5-573f-4de4-8c11-15df3acca2ca</productsID>
      </orderdetails>
    - <orderdetails diffgr:id="orderdetails5" msdata:rowOrder="4">
      <orderdetailsID>08285e41-e4c0-4ba2-8e64-b0bdbaa07511</orderdetailsID>
      <ordersID>661956c8-d842-4e44-8917-076db0b3a24f</ordersID>
      <productsID>ae5f421c-31fa-4edc-a6e0-ad7bd1fc3cfd</productsID>
      </orderdetails>
    - <orderdetails diffgr:id="orderdetails6" msdata:rowOrder="5">
      <orderdetailsID>f9390934-725a-4f91-a0fa-c5a4b212d6be</orderdetailsID>
      <ordersID>661956c8-d842-4e44-8917-076db0b3a24f</ordersID>
      <productsID>20fb34a5-573f-4de4-8c11-15df3acca2ca</productsID>
      </orderdetails>
    - <orders diffgr:id="orders1" msdata:rowOrder="0">
      <ordersID>daf6c0bb-2ed7-4c6b-9036-039fef3465a2</ordersID>
      <customersID>c100d5fb-e96c-4e83-a328-7d8ad8817be1</customersID>
      <ordername>Peters order2</ordername>
      <orderdate>2005-09-03T02:35:00+02:00</orderdate>
      </orders>
    - <orders diffgr:id="orders2" msdata:rowOrder="1">
      <ordersID>661956c8-d842-4e44-8917-076db0b3a24f</ordersID>
      <customersID>8ff0014d-06c2-4c86-95cd-7e647c3534a7</customersID>
      <ordername>Dans order</ordername>
      <orderdate>2005-09-03T01:50:00+02:00</orderdate>
      </orders>
    - <orders diffgr:id="orders3" msdata:rowOrder="2">
      <ordersID>b88293d4-c46c-4c2b-bf79-21645d5b205b</ordersID>
      <customersID>1f295ad2-c622-4d59-a8d5-6e84a588f5a7</customersID>
      <ordername>Mehmets order</ordername>
      <orderdate>2005-09-03T01:50:00+02:00</orderdate>
      </orders>
    - <orders diffgr:id="orders4" msdata:rowOrder="3">
      <ordersID>aac88cce-6ac9-4e1d-9306-3ee18cf719ae</ordersID>
      <customersID>c100d5fb-e96c-4e83-a328-7d8ad8817be1</customersID>
      <ordername>Peters order</ordername>
      <orderdate>2005-09-03T01:50:00+02:00</orderdate>
      </orders>
      </ShopDataSet>
      </diffgr:diffgram>
      </ShopDataSet>
     
     
     
    Instead of this:
     
     
      <?xml version="1.0" encoding="utf-8" ?>
    - <ShopDataSet xmlns="http://tempuri.org/">
    - <xs:schema id="ShopDataSet" targetNamespace="http://tempuri.org/ShopDataSet.xsd" xmlns:mstns="http://tempuri.org/ShopDataSet.xsd" xmlns="http://tempuri.org/ShopDataSet.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
    - <xs:element name="ShopDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    - <xs:complexType>
    - <xs:choice minOccurs="0" maxOccurs="unbounded">
    - <xs:element name="customers">
    - <xs:complexType>
    - <xs:sequence>
      <xs:element name="customersID" msdata:DataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string" />
    - <xs:element name="customersname">
    - <xs:simpleType>
    - <xs:restriction base="xs:string">
      <xs:maxLength value="255" />
      </xs:restriction>
      </xs:simpleType>
      </xs:element>
      </xs:sequence>
      </xs:complexType>
      </xs:element>
    - <xs:element name="products">
    - <xs:complexType>
    - <xs:sequence>
      <xs:element name="productsID" msdata:DataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string" />
    - <xs:element name="productname">
    - <xs:simpleType>
    - <xs:restriction base="xs:string">
      <xs:maxLength value="50" />
      </xs:restriction>
      </xs:simpleType>
      </xs:element>
      </xs:sequence>
      </xs:complexType>
      </xs:element>
    - <xs:element name="orderdetails">
    - <xs:complexType>
    - <xs:sequence>
      <xs:element name="orderdetailsID" msdata:DataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string" />
      <xs:element name="ordersID" msdata:DataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string" />
      <xs:element name="productsID" msdata:DataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string" />
      </xs:sequence>
      </xs:complexType>
      </xs:element>
    - <xs:element name="orders">
    - <xs:complexType>
    - <xs:sequence>
      <xs:element name="ordersID" msdata:DataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string" />
      <xs:element name="customersID" msdata:DataType="System.Guid, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" type="xs:string" />
    - <xs:element name="ordername">
    - <xs:simpleType>
    - <xs:restriction base="xs:string">
      <xs:maxLength value="50" />
      </xs:restriction>
      </xs:simpleType>
      </xs:element>
      <xs:element name="orderdate" type="xs:dateTime" />
      </xs:sequence>
      </xs:complexType>
      </xs:element>
      </xs:choice>
      </xs:complexType>
    - <xs:unique name="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//mstns:customers" />
      <xs:field xpath="mstns:customersID" />
      </xs:unique>
    - <xs:unique name="products_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//mstns:products" />
      <xs:field xpath="mstns:productsID" />
      </xs:unique>
    - <xs:unique name="orderdetails_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//mstns:orderdetails" />
      <xs:field xpath="mstns:orderdetailsID" />
      </xs:unique>
    - <xs:unique name="orders_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
      <xs:selector xpath=".//mstns:orders" />
      <xs:field xpath="mstns:ordersID" />
      </xs:unique>
      </xs:element>
    - <xs:annotation>
    - <xs:appinfo>
      <msdata:Relationship name="FK__orderdeta__order__060DEAE8" msdata:parent="orders" msdata:child="orderdetails" msdata:parentkey="ordersID" msdata:childkey="ordersID" />
      <msdata:Relationship name="FK__orderdeta__produ__07020F21" msdata:parent="products" msdata:child="orderdetails" msdata:parentkey="productsID" msdata:childkey="productsID" />
      <msdata:Relationship name="FK__orders__customer__014935CB" msdata:parent="customers" msdata:child="orders" msdata:parentkey="customersID" msdata:childkey="customersID" />
      </xs:appinfo>
      </xs:annotation>
      </xs:schema>
    - <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    - <ShopDataSet xmlns="http://tempuri.org/ShopDataSet.xsd">
    - <customers diffgr:id="customers1" msdata:rowOrder="0">
      <customersID>1f295ad2-c622-4d59-a8d5-6e84a588f5a7</customersID>
      <customersname>Mehmet</customersname>
      </customers>
    - <customers diffgr:id="customers2" msdata:rowOrder="1">
      <customersID>c100d5fb-e96c-4e83-a328-7d8ad8817be1</customersID>
      <customersname>Peter2</customersname>
      </customers>
    - <customers diffgr:id="customers3" msdata:rowOrder="2">
      <customersID>8ff0014d-06c2-4c86-95cd-7e647c3534a7</customersID>
      <customersname>Dan2</customersname>
      </customers>
    - <products diffgr:id="products1" msdata:rowOrder="0">
      <productsID>20fb34a5-573f-4de4-8c11-15df3acca2ca</productsID>
      <productname>Apples</productname>
      </products>
    - <products diffgr:id="products2" msdata:rowOrder="1">
      <productsID>b1df617f-cc28-4c7c-a746-2a85e3b26d94</productsID>
      <productname>Strawberrys</productname>
      </products>
    - <products diffgr:id="products3" msdata:rowOrder="2">
      <productsID>6826a6d3-8ffd-4677-b378-8215ec4915dc</productsID>
      <productname>Blue berrys</productname>
      </products>
    - <products diffgr:id="products4" msdata:rowOrder="3">
      <productsID>ae5f421c-31fa-4edc-a6e0-ad7bd1fc3cfd</productsID>
      <productname>Banana</productname>
      </products>
    - <orderdetails diffgr:id="orderdetails1" msdata:rowOrder="0">
      <orderdetailsID>f0c8dfa2-cc29-40b5-a46e-18931b9b06eb</orderdetailsID>
      <ordersID>aac88cce-6ac9-4e1d-9306-3ee18cf719ae</ordersID>
      <productsID>6826a6d3-8ffd-4677-b378-8215ec4915dc</productsID>
      </orderdetails>
    - <orderdetails diffgr:id="orderdetails2" msdata:rowOrder="1">
      <orderdetailsID>ae607169-7243-47ec-969b-585178bd97e1</orderdetailsID>
      <ordersID>aac88cce-6ac9-4e1d-9306-3ee18cf719ae</ordersID>
      <productsID>b1df617f-cc28-4c7c-a746-2a85e3b26d94</productsID>
      </orderdetails>
    - <orderdetails diffgr:id="orderdetails3" msdata:rowOrder="2">
      <orderdetailsID>3fe606dd-40cb-4094-a919-58fa7d178441</orderdetailsID>
      <ordersID>aac88cce-6ac9-4e1d-9306-3ee18cf719ae</ordersID>
      <productsID>ae5f421c-31fa-4edc-a6e0-ad7bd1fc3cfd</productsID>
      </orderdetails>
    - <orderdetails diffgr:id="orderdetails4" msdata:rowOrder="3">
      <orderdetailsID>dd2678f8-a0b7-4a6e-8554-ad515cbb6469</orderdetailsID>
      <ordersID>b88293d4-c46c-4c2b-bf79-21645d5b205b</ordersID>
      <productsID>20fb34a5-573f-4de4-8c11-15df3acca2ca</productsID>
      </orderdetails>
    - <orderdetails diffgr:id="orderdetails5" msdata:rowOrder="4">
      <orderdetailsID>08285e41-e4c0-4ba2-8e64-b0bdbaa07511</orderdetailsID>
      <ordersID>661956c8-d842-4e44-8917-076db0b3a24f</ordersID>
      <productsID>ae5f421c-31fa-4edc-a6e0-ad7bd1fc3cfd</productsID>
      </orderdetails>
    - <orderdetails diffgr:id="orderdetails6" msdata:rowOrder="5">
      <orderdetailsID>f9390934-725a-4f91-a0fa-c5a4b212d6be</orderdetailsID>
      <ordersID>661956c8-d842-4e44-8917-076db0b3a24f</ordersID>
      <productsID>20fb34a5-573f-4de4-8c11-15df3acca2ca</productsID>
      </orderdetails>
    - <orders diffgr:id="orders1" msdata:rowOrder="0">
      <ordersID>daf6c0bb-2ed7-4c6b-9036-039fef3465a2</ordersID>
      <customersID>c100d5fb-e96c-4e83-a328-7d8ad8817be1</customersID>
      <ordername>Peters order2</ordername>
      <orderdate>2005-09-03T02:35:00+02:00</orderdate>
      </orders>
    - <orders diffgr:id="orders2" msdata:rowOrder="1">
      <ordersID>661956c8-d842-4e44-8917-076db0b3a24f</ordersID>
      <customersID>8ff0014d-06c2-4c86-95cd-7e647c3534a7</customersID>
      <ordername>Dans order</ordername>
      <orderdate>2005-09-03T01:50:00+02:00</orderdate>
      </orders>
    - <orders diffgr:id="orders3" msdata:rowOrder="2">
      <ordersID>b88293d4-c46c-4c2b-bf79-21645d5b205b</ordersID>
      <customersID>1f295ad2-c622-4d59-a8d5-6e84a588f5a7</customersID>
      <ordername>Mehmets order</ordername>
      <orderdate>2005-09-03T01:50:00+02:00</orderdate>
      </orders>
    - <orders diffgr:id="orders4" msdata:rowOrder="3">
      <ordersID>aac88cce-6ac9-4e1d-9306-3ee18cf719ae</ordersID>
      <customersID>c100d5fb-e96c-4e83-a328-7d8ad8817be1</customersID>
      <ordername>Peters order</ordername>
      <orderdate>2005-09-03T01:50:00+02:00</orderdate>
      </orders>
      </ShopDataSet>
      </diffgr:diffgram>
      </ShopDataSet>
     
     
     
     
    Your sample is sending the whole dataset in a binary form, very good code, but it isnt really what im looking for.
     
     
    Thursday, January 26, 2006 7:47 PM
  • Hello,

    I think what you missed from Mathias' post is how he has code to serialize the dataset. There are a number of ways to serialize a dataset into XML representation. Take a look at the section titled "Serializing DataSet Objects to XML" of the following link

    You should consider having your web method also return a byte[] array or as a string, and then within your web method call the WriteXML method of the DataSet object and passing in the System.Data.XmlWriteMode.IgnoreSchema enumerator. Take a look at Mathias' code sample again or reference
    DataSet.WriteXml Method on MSDN.

    hope this helps
    -Todd Foust

    Friday, January 27, 2006 2:07 AM
    Moderator