2

我有一个可能包含多个附件的信息路径表单:通过使用一组重复元素,用户可以单击“单击添加项目”选项,他将能够上传更多附件。在 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; }
        }
4

1 回答 1

0

您的问题似乎与您使用MoveToNext. 根据文档,此函数移动到下一个兄弟元素,并且不会导航到子元素。您的代码似乎会转到它找到的第一个元素(大概是my:myFields),查找第一个名为的子元素my:field2(它只拉出第一个,因为您正在使用SelectSingleNode,然后转到下一个兄弟my:myFields(而不是 的下一个兄弟my:field2)。一个解决此问题的方法可能是将当前do-while循环替换为SelectNodes对以下内容的调用,然后遍历nodeList.

XmlNodeList nodelist = ipFormNav.SelectNodes("//my:field2", nsManager);
于 2013-08-08T11:03:25.687 回答