How to retrieve Smart Form fields programmatically

  • Updated

This KB article shows how to retrieve a particular Smart Form field programmatically.

The following documentation shows information about the methods. The code samples work for various described fields.

XmlDocument Class

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument%28v=vs.110%29.aspx

XmlDocument.LoadXml Method

http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.loadxml%28v=vs.110%29.aspx

Load the xml and use the next method: SelectSingleNode(String) which selects the first XmlNode that matches the XPath expression. (Inherited from XmlNode.)

Examples for various Smart Form fields:

For a text field:

Xmldoc.SelectSingleNode("/root/textfield").InnerXml // this selects the text and includes HTML markup
Xmldoc.SelectSingleNode("/root/textfield").InnerText // this selects the text and strips out HTMl markup

For a checkbox list:

for (int i=0; i < Xmldoc.SelectNodes( "root/checklist").Count -1; i++)
{
  string s = Xmldoc.SelectNodes("root/checklist").Item(i).InnerText; // gets the value
}

For a repeating node

if (xmldoc.SelectNodes("root/Repeating") != null)
{
  foreach(XmlNode xn in xmldoc.SelectNodes("root/Repeating"))
  {
  // gets the text values
    string s = xn.FirstChild.InnerText;
    string s2 = xn.LastChild.InnerText;
    string s3 = xn.ChildNodes[3].InnerText;
  }
}

For a link field:

xmldoc.SelectSingleNode("root/LinkNode/a/@href").InnerText; // gets the URL being linked to

For an image field:

If xmldoc.SelectSingleNode("root/backgroundImage") IsNot Nothing Then
  ' get the background image's src
  Dim src As String = xmldoc.SelectSingleNode("root/backgroundImage/img/@src").InnerText.Trim()
  Response.Write(src)
End If