Tuesday, March 06, 2007

Generating Infopath Taskpane Help

Using a nice technique outlined on InfoPath Dev, I have been extensively using the infopath task pane to display context-aware form field help information. After deciding that manually generating help xml for my next form would be a big chore, I decided to develop a console app to extract all of the Leaf nodes of a form template and put them into a "InfopathDev" friendly xml file format.

Here is the relevent code to get you started:



public void WriteHelpText(System.IO.Stream outStream, string strTemplateFile)
{
XmlDocument doc = new XmlDocument();
try
{
doc.Load(strTemplateFile);
}
catch (System.IO.IOException ex)
{
throw new Exception("error reading file:" + strTemplateFile, ex);
}
XmlNodeList ndList = doc.SelectNodes("descendant::*[not(child::*)]");

XmlTextWriter writer = new XmlTextWriter(
outStream, System.Text.Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.Indentation = 5;

writer.WriteStartDocument(false);

writer.WriteStartElement("Help");

foreach (XmlNode nd in ndList)
{
writer.WriteStartElement("Field");

//generate xpath attribute
StringBuilder sb = new StringBuilder(nd.LocalName);

//walk node path back to document source
XmlNode parentNode = nd.ParentNode;

do
{
sb.Insert(0, parentNode.LocalName + "/");
parentNode = parentNode.ParentNode;
} while (parentNode != (XmlNode)doc);

writer.WriteAttributeString("xpath", sb.ToString());
writer.WriteRaw(strDefaultHelpText);

writer.WriteEndElement();//end Field Element
}

writer.WriteEndElement();//end Help Element
writer.WriteEndDocument();//end document
writer.Flush();
}

1 comments:

Elizabeth said...

Hey, nice code. Is it possible to read xml data from an xml column in a SQL Server 2005 database?

I'm going down the path of:

Dim SurveyBinary() As Byte = Encoding.Default.GetBytes(dsSurveyStaging.form_xml)
Response.Headers.Add("Translate", "F")
Response.BinaryWrite(SurveyBinary)

Found the Translate thing at the bottom of http://msdn2.microsoft.com/en-us/library/ms772417.aspx but I'm not even sure Returning a content stream is what I'm looking for.

Thanks!