8

I have a question about getting Data from a SQL Database via ASP.NET and then passing the data over to Objective-C. Currently I am just using an SQL select statement to get data from the database via ASP.NET and ASP.NET returns the data like so:

<ArrayOfKeyValueOfstringPunchListCellModel84zsBx89 xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<KeyValueOfstringPunchListCellModel84zsBx89>
<Key>ORC0023</Key>
<Value xmlns:d3p1="http://schemas.datacontract.org/2004/07/LHS.Models">
</Value>
</KeyValueOfstringPunchListCellModel84zsBx89>
</ArrayOfKeyValueOfstringPunchListCellModel84zsBx89>

And then in Objective-C I am putting the data in a NSDictionary like so:

NSDictionary *punchList = [[NSDictionary alloc]initWithDictionary:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];

Everything is working as expected here.

What I am doing now is creating a stored procedure that returns XML and have ASP.NET return the XML (everything here is completed and working as expected) The XML came out like so:

<KeyValueOfstringPunchListCellModel84zsBx89>
<Key>ORC0023</Key>
<Value>
</Value>
</KeyValueOfstringPunchListCellModel84zsBx89>
</ArrayOfKeyValueOfstringPunchListCellModel84zsBx89>

Now for you Objective-C fans, you know you can’t have XML in NSDictionary unless you use a third party item/library.

Now my question is do I have redo my stored procedure to return JSON or this there another way to go about this?

My end goal is make the process as fast as possible and the SQL query is huge and returns alot of rows.

Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81
user979331
  • 11,039
  • 73
  • 223
  • 418
  • 1
    You don't really explain what your problem is. A size comparison between XML and JSON? Do you have compression enabled on the HTTP responses? – Wain May 05 '16 at 06:53
  • why do you need to prepare XML or JSON manually? ASP.Net API has ways to automate the process of returning response in the way we define – techspider May 06 '16 at 19:12

2 Answers2

2

You should

1) redo your stored proc and return the raw data.

2) Have asp.net handle the data formatting, since some clients might want JSON while others would prefer xml. so if you're going the JSON route, you can return JSON result from the MVC controller i.e

  public JsonResult GetData()
        {
            var temp = new {
                name = "hello world" 
            };
            return this.Json(temp)
        }

or create a web service using web api.

since you have a large result set, you should try using JSON which is less verbose than xml and thus will eat up less resources

dfdsfdsfsdf
  • 653
  • 3
  • 7
0

I'd like to offer an alternative solution but I have to go by some assumptions.

Now my question is do I have redo my stored procedure to return JSON or this there another way to go about this?

You don't have to return JSON if you can avoid that extra step unless you are running Microsoft SQL server 2016 which now supports the "FOR JSON" clause out of the box.

My end goal is make the process as fast as possible and the SQL query is huge and returns alot of rows.

Here "huge" + "fast" can play a role. In either case, you could also parse the XML right from Objective-C the way C# fan boys do it too, ala XmlReader.

There are thin wrappers of course, like IGXMLReader. And here is a preliminary example against your XML.

- (NSDictionary *)retrieveDataFromXml:(NSString *)xml {
  NSMutableDictionary *dictionary =
      [[NSMutableDictionary alloc] initWithCapacity:200];
  NSString *key;
  for (IGXMLReader *node in [[IGXMLReader alloc] initWithXMLString:xml]) {
    if ([node type] == IGXMLReaderNodeTypeElement &&
        [[node name] hasPrefix:@"Key"]) {
      key = [node text];
    } else if ([node type] == IGXMLReaderNodeTypeElement &&
               [[node name] hasPrefix:@"Value"] && [key length]) {
      [dictionary setObject:[node text] forKey:key];
      key = nil;
    }
  }
  return [dictionary copy];
}

I put together an example in github to illustrate the concept.

Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81