C# 遍历和infoPath表单

C# 遍历和infoPath表单,c#,sharepoint-2007,infopath,C#,Sharepoint 2007,Infopath,我有一个可能包含多个附件的infopath表单:通过使用一组重复元素,用户可以单击“单击添加项目”选项,他将能够上载更多附件。 在Sharepoint中,我使用工作流提取附件并将其放在单独的列表中。到目前为止,我只提取了第一个,工作流成功完成 我可以放一个循环或什么东西在表单中迭代吗 我附上以下代码: public sealed partial class FileCopyFeature : SharePointSequentialWorkflowActivity {

我有一个可能包含多个附件的infopath表单:通过使用一组重复元素,用户可以单击“单击添加项目”选项,他将能够上载更多附件。 在Sharepoint中,我使用工作流提取附件并将其放在单独的列表中。到目前为止,我只提取了第一个,工作流成功完成

我可以放一个循环或什么东西在表单中迭代吗

我附上以下代码:

    public sealed partial class FileCopyFeature : SharePointSequentialWorkflowActivity
    {
        public FileCopyFeature()
        {
            InitializeComponent();
        }

        public Guid workflowId = default(System.Guid);
        public Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties workflowProperties = new Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties();

        private void CopyFile(object sender, EventArgs e)
        {

            // Retrieve the file associated with the item
            // on which the workflow has been instantiated
            SPFile file = workflowProperties.Item.File;

            if (file == null)
                return;

            // Get the binary data of the file
            byte[] xmlFormData = null;
            xmlFormData = file.OpenBinary();

            // Load the data into an XPathDocument object
            XPathDocument ipForm = null;

            if (xmlFormData != null)
            {
                using (MemoryStream ms = new MemoryStream(xmlFormData))
                {
                    ipForm = new XPathDocument(ms);
                    ms.Close();
                }
            }

            if (ipForm == null)
                return;

            // Create an XPathNavigator object to navigate the XML
            XPathNavigator ipFormNav = ipForm.CreateNavigator();

            ipFormNav.MoveToFollowing(XPathNodeType.Element);
            XmlNamespaceManager nsManager =
            new XmlNamespaceManager(new NameTable());

            foreach (KeyValuePair<string, string> ns
            in ipFormNav.GetNamespacesInScope(XmlNamespaceScope.All))
            {
                if (ns.Key == String.Empty)
                {
                    nsManager.AddNamespace("def", ns.Value);
                }
                else
                {
                    nsManager.AddNamespace(ns.Key, ns.Value);
                }
            }

           do
            {


                XPathNavigator nodeNav = ipFormNav.SelectSingleNode("//my:field2", nsManager);


                // Retrieve the value of the attachment in the InfoPath form
                //XPathNavigator nodeNav = ipFormNav.SelectSingleNode(
                //"//my:field2", nsManager);

                string ipFieldValue = string.Empty;
                if (nodeNav != null)
                {
                    ipFieldValue = nodeNav.Value;

                    // Decode the InfoPath file attachment
                    InfoPathAttachmentDecoder dec =
                    new InfoPathAttachmentDecoder(ipFieldValue);
                    string fileName = dec.Filename;
                    byte[] data = dec.DecodedAttachment;

                    // Add the file to a document library
                    using (SPWeb web = workflowProperties.Web)
                    {
                        SPFolder docLib = web.Folders["Doc"];
                        docLib.Files.Add(fileName, data);
                        docLib.Update();
                        // workflowProperties.Item.CopyTo(data + "/Doc/" + fileName);
                    }

                }

            }
            while (ipFormNav.MoveToNext());

        }
   }
    /// <summary>
    /// Decodes a file attachment and saves it to a specified path.
    /// </summary>
    public class InfoPathAttachmentDecoder
    {
        private const int SP1Header_Size = 20;
        private const int FIXED_HEADER = 16;

        private int fileSize;
        private int attachmentNameLength;
        private string attachmentName;
        private byte[] decodedAttachment;

        /// <summary>
        /// Accepts the Base64 encoded string
        /// that is the attachment.
        /// </summary>
        public InfoPathAttachmentDecoder(string theBase64EncodedString)
        {
            byte[] theData = Convert.FromBase64String(theBase64EncodedString);
            using (MemoryStream ms = new MemoryStream(theData))
            {
                BinaryReader theReader = new BinaryReader(ms);
                DecodeAttachment(theReader);
            }
        }

        private void DecodeAttachment(BinaryReader theReader)
        {
            //Position the reader to get the file size.
            byte[] headerData = new byte[FIXED_HEADER];
            headerData = theReader.ReadBytes(headerData.Length);

            fileSize = (int)theReader.ReadUInt32();
            attachmentNameLength = (int)theReader.ReadUInt32() * 2;

            byte[] fileNameBytes = theReader.ReadBytes(attachmentNameLength);
            //InfoPath uses UTF8 encoding.
            Encoding enc = Encoding.Unicode;
            attachmentName = enc.GetString(fileNameBytes, 0, attachmentNameLength - 2);
            decodedAttachment = theReader.ReadBytes(fileSize);
        }

        public void SaveAttachment(string saveLocation)
        {
            string fullFileName = saveLocation;
            if (!fullFileName.EndsWith(Path.DirectorySeparatorChar.ToString()))
            {
                fullFileName += Path.DirectorySeparatorChar;
            }

            fullFileName += attachmentName;

            if (File.Exists(fullFileName))
                File.Delete(fullFileName);

            FileStream fs = new FileStream(fullFileName, FileMode.CreateNew);
            BinaryWriter bw = new BinaryWriter(fs);
            bw.Write(decodedAttachment);

            bw.Close();
            fs.Close();
        }

        public string Filename
        {
            get { return attachmentName; }
        }

        public byte[] DecodedAttachment
        {
            get { return decodedAttachment; }
        }
公共密封部分类FileCopyFeature:SharePointSequentialWorkflowActivity
{
PublicFileCopyFeature()
{
初始化组件();
}
公共Guid workflowId=默认值(System.Guid);
public Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties workflowProperties=新的Microsoft.SharePoint.Workflow.SPWorkflowActivationProperties();
私有void CopyFile(对象发送方,事件参数e)
{
//检索与该项关联的文件
//已在其上实例化工作流
SPFile file=workflowProperties.Item.file;
if(file==null)
返回;
//获取文件的二进制数据
字节[]xmlFormData=null;
xmlFormData=file.OpenBinary();
//将数据加载到XPathDocument对象中
XPathDocument ipForm=null;
如果(xmlFormData!=null)
{
使用(MemoryStream ms=新的MemoryStream(xmlFormData))
{
ipForm=新的XPathDocument(ms);
Close女士();
}
}
if(ipForm==null)
返回;
//创建XPathNavigator对象以导航XML
XPathNavigator ipFormNav=ipForm.CreateNavigator();
ipFormNav.MoveToFollowing(XPathNodeType.Element);
XmlNamespaceManager nsManager=
新的XmlNamespaceManager(新的NameTable());
foreach(键值对)
在ipFormNav.GetNamespacesInScope(XmlNamespaceScope.All)中
{
if(ns.Key==String.Empty)
{
nsManager.AddNamespace(“def”,ns.Value);
}
其他的
{
nsManager.AddNamespace(ns.Key,ns.Value);
}
}
做
{
XPathNavigator nodeNav=ipFormNav.SelectSingleNode(“//my:field2”,nsManager);
//在InfoPath表单中检索附件的值
//XPathNavigator nodeNav=ipFormNav.SelectSingleNode(
//“//my:field2”,nsManager);
字符串ipFieldValue=string.Empty;
if(nodeNav!=null)
{
ipFieldValue=nodeNav.Value;
//解码InfoPath文件附件
InfoPathAttachmentDec=
新的InfoPathAttachmentDecoder(ipFieldValue);
字符串文件名=十二月文件名;
字节[]数据=dec.decodedatatchment;
//将文件添加到文档库
使用(SPWeb=workflowProperties.web)
{
SPFolder docLib=web.Folders[“Doc”];
添加(文件名、数据);
Update();
//workflowProperties.Item.CopyTo(数据+“/Doc/”+文件名);
}
}
}
while(ipFormNav.MoveToNext());
}
}
/// 
///解码文件附件并将其保存到指定路径。
/// 
公共类InfoPathAttachmentDecoder
{
私有常量int SP1Header_Size=20;
私有const int FIXED_头=16;
私有int文件大小;
私有int attachmentNameLength;
私有字符串attachmentName;
专用字节[]解码数据连接;
/// 
///接受Base64编码的字符串
///这就是附件。
/// 
公共InfoPathAttachmentDecoder(字符串base64编码字符串)
{
字节[]数据=Convert.FromBase64String(Base64EncodedString);
使用(MemoryStream ms=新的MemoryStream(theData))
{
BinaryReader theReader=新的BinaryReader(毫秒);
解码附件(阅读器);
}
}
私有无效解码附件(二进制读卡器)
{
//定位读卡器以获取文件大小。
字节[]头数据=新字节[固定头];
headerData=theReader.ReadBytes(headerData.Length);
fileSize=(int)theReader.ReadUInt32();
attachmentNameLength=(int)theReader.ReadUInt32()*2;
byte[]fileNameBytes=theReader.ReadBytes(attachmentNameLength);
//InfoPath使用UTF8编码。
Encoding enc=Encoding.Unicode;
attachmentName=enc.GetString(fileNameBytes,0,attachmentNameLength-2);
decodedAttachment=theReader.ReadBytes(文件大小);
}
公共void SaveAttachment(字符串saveLocation)
{
字符串fullFileName=保存位置;
如果(!fullFileName.EndsWith(Path.directoryseportorchar.ToString()))
{
fullFileName+=Path.directoryseportorchar;
}
fullFileName+=附件名称;
if(File.Exists(fullFileName))
Delete(fullFileName);
FileStream fs=newfilestream(fullFileName,FileMode.CreateNew);
BinaryWriter bw=新的BinaryWriter(fs);
写入(解码数据附件);
bw.Close();
fs.Close();
}
公共字符串文件名
{
获取{return attachmentName;}
}
XmlNodeList nodelist = ipFormNav.SelectNodes("//my:field2", nsManager);