Get data from InfoPath form using Object Model(C#)

The InfoPath forms (.xml) files stored in the following Path
Open the site inthe SharePoint desinger, click on All Files folder and list -> Open any xml file

  <my:group7>
    <my:group8>
     <my:ListOfAttendee>Ravi</my:ListOfAttendee>
     <my:Organization>Financial Services</my:Organization>
    </my:group8>
    <my:group8>
     <my:ListOfAttendee>Kumar</my:ListOfAttendee>
     <my:Organization>IT Department</my:Organization>
    </my:group8><my:group8>
     <my:ListOfAttendee>Mohan</my:ListOfAttendee>
     <my:Organization>HR</my:Organization>
    </my:group8>
   </my:group7>


use the below code:

SPFile file = properties.ListItem.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);
               }
           }
           // Retrieve the value of the field in the InfoPath form
           //XPathNavigator nodeNav = ipFormNav.SelectSingleNode("//my:DisplayName", nsManager);
           XPathNodeIterator rows = ipFormNav.Select("//my:group7/my:group8", nsManager);
           string _Attendees = "";
           string _Organization = "";
           DataTable table = new DataTable();
           table.Columns.Add("List of Attendees", typeof(string));
           table.Columns.Add("Organization", typeof(string));
           while (rows.MoveNext())
           {
               _Attendees=rows.Current.SelectSingleNode("my:ListOfAttendee", nsManager).Value;
               _Organization = rows.Current.SelectSingleNode("my:Organization", nsManager).Value;
               table.Rows.Add(_Attendees, _Organization);
            }
          
        SPListItem list = AttendeesDetailsList.Items.Add();
                   list["Title"] = properties.ListItemId;
                   list["ListOfAttendee"] = _Attendees;
                   list.Update();

Comments

Popular posts from this blog

SharePoint 2016 and 2019 Ports

PsConfig step by step /Configuration Wizard. “Upgrade Available” vs “Upgrade required”

Unlock the SharePoint site using Powershell command