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

  • AI for GitHub Collaboration? Maybe Not So Much

    No doubt GitHub Copilot has been a boon for developers, but AI might not be the best tool for collaboration, according to developers weighing in on a recent social media post from the GitHub team.

  • Visual Studio 2022 Getting VS Code 'Command Palette' Equivalent

    As any Visual Studio Code user knows, the editor's command palette is a powerful tool for getting things done quickly, without having to navigate through menus and dialogs. Now, we learn how an equivalent is coming for Microsoft's flagship Visual Studio IDE, invoked by the same familiar Ctrl+Shift+P keyboard shortcut.

  • .NET 9 Preview 3: 'I've Been Waiting 9 Years for This API!'

    Microsoft's third preview of .NET 9 sees a lot of minor tweaks and fixes with no earth-shaking new functionality, but little things can be important to individual developers.

  • Data Anomaly Detection Using a Neural Autoencoder with C#

    Dr. James McCaffrey of Microsoft Research tackles the process of examining a set of source data to find data items that are different in some way from the majority of the source items.

  • What's New for Python, Java in Visual Studio Code

    Microsoft announced March 2024 updates to its Python and Java extensions for Visual Studio Code, the open source-based, cross-platform code editor that has repeatedly been named the No. 1 tool in major development surveys.

Subscribe on YouTube