Reading the XML from an InfoPath Form instance in a Form Library


Table of Contents

There are many scenarios where you would want to post an InfoPath form to a Form Library but then read the information out that is captured and do something else with it.

InfoPath Form XML

You can find out what the submitted InfoPath Form XML looks like by downloading the instance out of the Form Library to your local drive. It will be an xml file that you can open in NotePad or Visual Studio etc.

<my:TimesheetWorkFields xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:dfs="http://schemas.microsoft.com/office/infopath/2003/dataFormSolution" xmlns:s0="http://microsoft.com/webservices/SharePointPortalServer/UserProfileService" xmlns:s1="http://microsoft.com/wsdl/types/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/" xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-01-23T00:43:17" xmlns:xd="http://schemas.microsoft.com/office/infopath/2003">
	<my:Name>Jeremy Thake</my:Name>
	<my:WeekEnding>19-FEB-2009</my:WeekEnding>
	<my:ProjectHours>
		<my:ProjectName>Project X</my:ProjectName>
		<my:ProjectDescription>Wasn't there monkey's involved in this?</my:ProjectDescription>
		<my:ProjectComments>Need more budget</my:ProjectComments>
	</my:ProjectHours>
</my:TimesheetWorkFields>

Code to read the InfoPath Form XML

The example below is where an event receiver is fired on an InfoPath Form being submitted to a Form Library. The important thing to highlight is the addition of the Namespace based on that in the InfoPath XML e.g. namespace xmlns:my="http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-01-23T00:43:17". If you want to retrieve info from the above elements with the namespace prefix you have to add this namespace to the XmlDocument and pass it in when you read nodes.

public override void ItemAdded(SPItemEventProperties properties)
{
  SPListItem currentListItem = properties.ListItem;
  using (SPWeb web = properties.OpenWeb())
  {
    MemoryStream myInStream = new MemoryStream(currentListItem.File.OpenBinary());
    XmlDocument doc = new XmlDocument();
    doc.Load(myInStream);
    try
    {
      XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager(doc.NameTable);
      nameSpaceManager.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-01-23T00:43:17");

      XmlNodeList nodeList;
      XmlElement root = doc.DocumentElement;

      DateTime dateFriday = DateTime.Parse(root.SelectSingleNode("my:WeekEnding", nameSpaceManager).InnerXml);
      string personName = root.SelectSingleNode("my:Name", nameSpaceManager).InnerXml;

      nodeList = root.SelectNodes("//my:ProjectHours", nameSpaceManager);
      foreach (XmlNode node in nodeList)
      {
        string projectName = node.SelectSingleNode("my:ProjectName", nameSpaceManager).InnerText;
        string projectDescription =  node.SelectSingleNode("my:ProjectDescription", nameSpaceManager).InnerText;
        string projectComments = node.SelectSingleNode("my:ProjectComments", nameSpaceManager).InnerText;
        DoSomething(projectName, projectDescription, projectComments);
      }
    }
  }
}

Labels

infopath infopath Delete
xml xml Delete
xmldocument xmldocument Delete
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.



Creative Commons License
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 Unported License. Hosted generously by CustomWare