locked
serialize large objects to xml RRS feed

  • Question

  • User767034699 posted

    Hi there guys,

    i have a problem here. Im trying to serialize large objects into xml and then return the xml from soap service. The objects size is up to 200mb.

    The xml structure looks like this below

    <?xml version="1.0"?>
    <ArrayOfSport4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <Sport4>
        <ExtensionData />
        <Groups>
          <Group3>
            <ExtensionData />
            <Events>
              <Event3>
                <ExtensionData />
                <Event>Cup</Event>
                <EventID>637022</EventID>
                <EventTypeID>1</EventTypeID>
                <Order>100</Order>
                <Status>1</Status>
                <SubEvents>
                  <SubEvent3>
                    <ExtensionData />
                    <BetRadarMatchID>15200765</BetRadarMatchID>
                    <Odds>
                      <Odd3>
                        <ExtensionData />
                        <Combinability>1</Combinability>
                        <EndDate>2018-08-06T18:00:00</EndDate>
                        <HND>0</HND>
                        <Odd>2.31</Odd>
                        <OddClass>Home/Draw/Away (FT)</OddClass>
                        <OddID>8215892760</OddID>
                        <OddType>Home</OddType>
                        <OddTypeID>1</OddTypeID>
                        <StartDate>2018-08-06T14:25:00</StartDate>
                        <Status>1</Status>
                      </Odd3>
                      <Odd3>

    However when i serialize i get outofmemory exception if i return tostring().

    Code that im using to serialize is as follows

     public static string Serialize<T>(this T value)
            {
                if (value == null) return string.Empty;
    
                var xmlSerializer = new XmlSerializer(typeof(T));
    
                using (var stringWriter = new StringWriter())
                {
                    using (var xmlWriter = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true, OmitXmlDeclaration = true }))
                    {
                        //var xmlns = new XmlSerializerNamespaces();
                        //xmlns.Add(string.Empty, string.Empty);
                        var emptyNamepsaces = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
                        xmlSerializer.Serialize(xmlWriter, value, emptyNamepsaces);
                        return stringWriter.ToString();
                    }
                }
            }

    so i would like to return the xml as string from webmethod. is there a way i could return large xml on soap service?

    kind regards

    Tony

    Monday, August 6, 2018 2:32 PM

All replies

  • User753101303 posted

    Hi,

    Which kind of service are you trying to expose ? ASP.NET should be able to handle for you. I would try first with "standard" code (it may write directly the result to the browser output ?)

    You could also perhaps consider implementing paging on your service.

    Monday, August 6, 2018 2:58 PM
  • User475983607 posted

    I'm pretty sure we have given you this same advice in the past... Do not return a serialized strings from a web method.  Return the actual type. 

    You are essentially serialize the response twice and forcing the client to fetch and deserialize a string.  

    i have a problem here. Im trying to serialize large objects into xml and then return the xml from soap service. The objects size is up to 200mb.

    You're state there is a problem but not what the problem is... 200Mb is large but not that large.  Can you clarify the problem you are facing?

    Monday, August 6, 2018 3:05 PM
  • User767034699 posted

    Hi there mgebhard

    thanks for the response,

    I'm pretty sure we have given you this same advice in the past... Do not return a serialized strings from a web method.  Return the actual type. 

    Here is my Webmethod

        [WebMethod]
            public string GetFixtures(string Sport)
            {
               // isCheckData.EventsSoapClient client = new EventsSoapClient();
              //  XmlElement ds = client.GetFixtures("");
    
    //Here i connect to external api and get my objects
           EventFixtures.Fixtures.EventsProgramFiltered((Int16)sBookmakerID);
    
    
              
               
               
               // string xml = EventFixtures.Fixtures.Serialize(Ts);
                XmlDocument newDooc = new XmlDocument();
                string xml = EventFixtures.Fixtures.Serialize(Ts);
    
                return xml;
            }
    

    if i return xml. The data doesnt show at all. 

    kind regards

    Tony

    Wednesday, August 8, 2018 6:42 AM
  • User767034699 posted

    Hi there PatriceSc,

    Which kind of service are you trying to expose ? ASP.NET should be able to handle for you. I would try first with "standard" code (it may write directly the result to the browser output ?)

    its a soap service. The data doesnt show when trying to consume it.

    kind regards

    Tony

    Wednesday, August 8, 2018 6:44 AM
  • User753101303 posted

    Seems the "page method" approach rather than really a SOAP service.

    The basic idea is that ASP.NET takes care of the plumbing for you. A service should accept and return .NET objects. ASP.NET deserialize incoming data before calling your code and deserialize what you return.

    You have control on both sides ? Try to create a simple Sport4 class and return an array. If consumed from a browser you could use JSON rather than XML. Once the basic stuff is ok you'll be able to extend your Sport4 class (not sure from where comes your XML sample, you should have sites to create a C# class from an XML sample).

    Wednesday, August 8, 2018 8:06 AM