Test-Drive SQL Server Data Services: C# 3.0, LINQ to XML: Autogenerate REST XML Entity Templates: Listing 1

A SqlDataReader instance and LINQ to XML functional construction combine to enable autogenerating the XML payload for the help file's sample CreateEntity() function from table metadata. Much of the code in this snippet, which is excerpted from the sample app's CreateEntitySet( ) method for the REST protocol, is devoted to replacing .NET data type designations with SSDS's more limited XML Schema type repertoire.

string setXml = null;
SqlConnection connNwind = new SqlConnection(strNwind)
SqlCommand cmdNwind = new SqlCommand("SELECT * 
   FROM [" + tableName + "]", connNwind)
connNwind.Open();
SqlDataReader sdrNwind = cmdNwind.ExecuteReader(
   CommandBehavior.SequentialAccess);
while (sdrNwind.Read())
{
   // Create an XElement with XAttributes for each column
   var entElement = new XElement(new XElement(entityType,
         new XAttribute(XNamespace.Xmlns + "s", 
         "http://schemas.microsoft.com/sitka/2008/03/"),
         new XAttribute(XNamespace.Xmlns + "xsi", 
         "http://www.w3.org/2001/XMLSchema-instance"),
         new XAttribute(XNamespace.Xmlns + "x", 
         "http://www.w3.org/2001/XMLSchema"),
      from col in Enumerable.Range(0, sdrNwind.FieldCount)
      select new XElement(sdrNwind.GetName(col),
         new XAttribute("xsi:type", "x:" + 
         sdrNwind.GetFieldType(col).ToString().ToLower()),
         sdrNwind.GetValue(col))));
   setElement.Add(entElement);
   sdrNwind.Close();
   setXml = setElement.ToString();
   // String identifier change
   setXml = setXml.Replace('\"', '\'');
   // Type fixups with "x:system." prefix for specificity
   setXml = setXml.Replace("x:system.uint64", 
      "x:decimal");
   setXml = setXml.Replace("x:system.uint32", 
      "x:decimal");
   setXml = setXml.Replace("x:system.uint16", 
      "x:decimal");
   setXml = setXml.Replace("x:system.int64", 
      "x:decimal");
   setXml = setXml.Replace("x:system.int32", 
      "x:decimal");
   setXml = setXml.Replace("x:system.int16", 
      "x:decimal");
   setXml = setXml.Replace("x:system.byte[]",
      "x:base64Binary");
   setXml = setXml.Replace("x:system.byte", 
      "x:decimal");
   setXml = setXml.Replace("x:system.sbyte", 
      "x:decimal");
   setXml = setXml.Replace("x:system.single", 
      "x:decimal");
   setXml = setXml.Replace("x:system.double", 
      "x:decimal");
   setXml = setXml.Replace("x:system.decimal", 
      "x:decimal");
   setXml = setXml.Replace("x:system.datetime", 
      "x:dateTime");
   setXml = setXml.Replace("x:system.boolean",
      "x:boolean");
   setXml = setXml.Replace("x:system.char", "x:string");
   setXml = setXml.Replace("x:system.guid", "x:string");
   setXml = setXml.Replace("x:system.string", 
      "x:string");
   // Special case: Temporarily remove elements 
   // with nil values
   setXml = setXml.Replace("<ShippedDate 
      xsi:type='x:decimal'></ShippedDate>", "");
}
// Code to remove empty elements for value types goes here
// ...
// POST the entity using sample code from SSDS help file

CreateEntity(containerUri, setXml);
comments powered by Disqus

Featured

  • Mastering AI Development and Building AI Apps with GitHub Copilot

    Two Microsoft experts explain how GitHub Copilot is evolving from a coding assistant into a broader platform for building, customizing and testing AI-powered developer workflows.

  • VS Code 1.123 Adds Agent Session Sync, 1M Context Windows

    Microsoft released Visual Studio Code 1.123 on June 3, adding agent-focused features, larger model context support, integrated browser updates and a new delay for some automatic extension updates.

  • Copilot Billing Shock Hits Developers

    Developer complaints about GitHub Copilot's new usage-based billing model have centered on unexpectedly rapid AI credit consumption, and neither GitHub nor Microsoft has responded directly to the backlash, though they have previously published guidance to lessen model usage costs.

  • Hands On with GitHub Copilot App Technical Preview: Turning a Blazor Issue into a PR

    GitHub's brand-new Copilot desktop app, in technical preview, handled a small Blazor issue from planning through pull request creation, but the hands-on test also showed why developers still need to verify agent work in the running app before merging.

Subscribe on YouTube