Generate Class for Smart Form

  • Updated

Create a C# or VB.NET class to programmatically access Smart Form data. Smart Form data is XML and may be processed using XSLT, an XML parser (XmlDocument), Linq/XQuery or even as a string. It may also be deserialized into a .NET object given a C# or VB.NETclass.

This article describes how to generate a class from a Smart Form design.

Microsoft provides a command-line tool, xsd.exe, that converts an XML Schema to a C# or VB.NET class.

1. Save Schema XSD File.

A. Save the Smart Form schema to an .xsd file. With CMS400 8.0.1  or later, you may open the Smart Form schema in Visual Studio via a URL and then save it as an .xsd file.

    http:// {SITE-NAME} /workarea/webservices/rest.svc/xmlconfigs/ {SMART-FORM-ID} .xsd

For example: http://localhost/cms400developer/workarea/webservices/rest.svc/xmlconfigs/10.xsd

If this isn't working. We have a sample of how to generate this using your own webservice here .

With CMS400 8.0.0 and older ...

B. Open the Smart Form design and click the XSD button. Copy and paste the schema to a file on your server's file system.

Schema

2. Generate Class from XSD

A. Run the xsd.exe tool to create the .cs or .vb file. Store the file in your web site's App_Code folder (i.e., App_Code/CSCode or App_Code/VBCode).

     xsd file.xsd /classes [/language:language] [/namespace:namespace] [/outputdir:directory]

B. Open a Visual Studio command prompt or a command prompt with xsd.exe in its path. xsd.exe is provided with Visual Studio (C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\xsd.exe) or may be downloaded with the .Net Framework 2.0 SDK from Microsoft's site. For documentation on the options, see XML Schema Definition Tool (Xsd.exe) .

Examples

    xsd bookstore.xsd /classes /language:CS /namespace:SmartForm.Bookstore /outputdir:c:\inetpub\wwwroot\App_Code\CSCode

    xsd bookstore.xsd /classes /language:VB /namespace:Bookstore

The xsd.exe creates a file with the same name as the schema, for example, bookstore.cs or bookstore.vb.

Example C# class snippet,

//------------------------------------------------------------------------------
//
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.3603
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
//
//------------------------------------------------------------------------------

//
// This source code was auto-generated by xsd, Version=2.0.50727.42.
//
namespace SmartForm.BookStore
{
    using System.Xml.Serialization;
    
    
    ///
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
    public partial class root {
        
        private rootBook[] bookField;
        
        private decimal bookcountField;
        
        private decimal avepriceField;
        
        ///
        [System.Xml.Serialization.XmlElementAttribute("book")]
        public rootBook[] book {
            get {
                return this.bookField;
            }
            set {
                this.bookField = value;
            }
        }
    ...

3. Deserialize XML into an Object

 To use the class, deserialize Smart Form data into an object.

C# Example,

    string xml = ContentBlock1.EkItem.Html;
    SmartForm.BookStore.root book = (SmartForm.BookStore.root)Ektron.Cms.EkXml.Deserialize(typeof(SmartForm.BookStore.root), xml);

VB.NET Example,

    Dim xml As String = ContentBlock1.EkItem.Html
    Dim book As SmartForm.BookStore.root = CType(Ektron.Cms.EkXml.Deserialize(GetType(SmartForm.BookStore.root), xml), SmartForm.BookStore.root)

Additional Information

Sample methods to retrieve rich area field text:
public static string GetRichString(XmlNode[] nodeArray)
    {
        return (nodeArray != null && nodeArray.Length > 0) ? nodeArray.Aggregate(new System.Text.StringBuilder(), (sb, node) => sb.AppendLine(node.OuterXml), sb => sb.ToString()) : string.Empty;
    }
    public static string GetTextString(XmlNode[] nodeArray)
    {
        return (nodeArray != null && nodeArray.Length > 0) ? nodeArray.Aggregate(new System.Text.StringBuilder(), (sb, node) => sb.Append(node.InnerText), sb => sb.ToString()) : string.Empty;
    }

http://ektron.com/Resources/Webinars/Ektron-Content-Types/  - Learn more about creating and using Content Types with the Framework API to retrieve, filter, and sort structured content.
Microsoft's  XML Schema Definition Tool (Xsd.exe)