作者:windwhisper 日期:2008-03-05
曾经有一段时间,我都是按照这种教科书式的步骤来取得Infopath表单中的数据:
- 将xsn文件另存为原文件。
- 使用Visual Studio的命令行工具将xsd命令将myschema.xsd文件转换成*.cs文件。
- 在工作流中将表单反序列化成cs文件中的类。然后再使用。
如果表单中的域(Field)变化了,整个步骤还要重来一次。现在不怕了。因为workflow中得到的表单数据均为标准的XML文档。可以使用一种近乎偷懒的方法得到--在将数据读取到内存中后,使用正则表达式直捣黄龙。
下面我们来看具体的实现。
C#代码
- /// <summary>
- ///
- /// </summary>
- /// <param name="path">
- /// Infopath表单中元素存储的位置。
- /// 使用“\”作为路径分隔符.比如:NODE1\NODE2\...\NODEN.
- /// 注意,请不要将根节点写入路径中。</param>
- /// <param name="content">内容</param>
- /// <returns></returns>
- private ArrayList GetFormInfoByUsingRegex(string path, string content)
- {
- string[] nodes = default(string[]);
- ArrayList result = new ArrayList();
- string tempCts = content;
- try
- {
- nodes = path.Split('\\');
- }
- catch (Exception ee)
- {
- }
- Regex reg;
- MatchCollection mc;
- for (int i = 0; i < nodes.Length; i++)
- {
- reg = new Regex(string.Format(@"<my:{0}>([\s\S]*?)</my:{0}>", nodes[i]));
- mc = reg.Matches(tempCts);
- if (i < nodes.Length - 1)
- {
- tempCts = mc[0].Result("$1");
- }
- else
- {
- foreach (Match m in mc)
- {
- result.Add(m.Result("$1"));
- }
- }
- }
- return result;
- }
整段程序很简单,要点也都写在了标题注释里。以后如果我们想读取以下路径中的数据,只要这么来调用就行
了:GetFormInfoByUsingRegex(@"评审计划\基本信息\产品名称",formstrings)。
问题:如果返回值是类似于Contract Selector的多值的情况呢?没有问题,看到返回值是一个ArrayList了吧?foreach一下吧。

浙公网安备 33010602011771号