曾经有一段时间,我都是按照这种教科书式的步骤来取得Infopath表单中的数据:

  1. 将xsn文件另存为原文件。
  2. 使用Visual Studio的命令行工具将xsd命令将myschema.xsd文件转换成*.cs文件。
  3. 在工作流中将表单反序列化成cs文件中的类。然后再使用。

如果表单中的域(Field)变化了,整个步骤还要重来一次。现在不怕了。因为workflow中得到的表单数据均为标准的XML文档。可以使用一种近乎偷懒的方法得到--在将数据读取到内存中后,使用正则表达式直捣黄龙。

下面我们来看具体的实现。

C#代码
  1. /// <summary>      
  2. ///       
  3. /// </summary>      
  4. /// <param name="path">      
  5. /// Infopath表单中元素存储的位置。      
  6. /// 使用“\”作为路径分隔符.比如:NODE1\NODE2\...\NODEN.      
  7. /// 注意,请不要将根节点写入路径中。</param>      
  8. /// <param name="content">内容</param>      
  9. /// <returns></returns>      
  10. private ArrayList GetFormInfoByUsingRegex(string path, string content)      
  11. {      
  12.     string[] nodes = default(string[]);      
  13.     ArrayList result = new ArrayList();      
  14.     string tempCts = content;      
  15.     try     
  16.     {      
  17.         nodes = path.Split('\\');      
  18.     }      
  19.     catch (Exception ee)      
  20.     {      
  21.     }      
  22.     Regex reg;      
  23.     MatchCollection mc;      
  24.     for (int i = 0; i < nodes.Length; i++)      
  25.     {      
  26.      
  27.         reg = new Regex(string.Format(@"<my:{0}>([\s\S]*?)</my:{0}>", nodes[i]));      
  28.         mc = reg.Matches(tempCts);      
  29.         if (i < nodes.Length - 1)      
  30.         {      
  31.             tempCts = mc[0].Result("$1");      
  32.         }      
  33.         else     
  34.         {      
  35.             foreach (Match m in mc)      
  36.             {      
  37.                 result.Add(m.Result("$1"));      
  38.             }      
  39.         }      
  40.     }      
  41.     return result;      
  42. }  

整段程序很简单,要点也都写在了标题注释里。以后如果我们想读取以下路径中的数据,只要这么来调用就行
了:GetFormInfoByUsingRegex(@"评审计划\基本信息\产品名称",formstrings)。


问题:如果返回值是类似于Contract Selector的多值的情况呢?没有问题,看到返回值是一个ArrayList了吧?foreach一下吧。

 



posted on 2008-06-04 11:31  晃晃悠悠  阅读(278)  评论(1)    收藏  举报