Updated solution (after clarification by OP):
If you want to read in the attachment from an Infopath document, you can use this method:
http://support.microsoft.com/kb/2517906. The code contains both an Encoder and Decoder.
The Decoder gives you the actual byte[] for the attachment. Then byte[] -> MemoryStream -> Unzip (using DeflateStream or ZipArchive -> then you get your XML files out of your archive.
Start by getting the attachment data from the infopath form as byte array:
XPathDocument document = new XPathDocument(@"infopathform.xml");
XPathNavigator navigator = document.CreateNavigator();
navigator.MoveToFollowing(XPathNodeType.Element);
string myNamespace = navigator.GetNamespacesInScope(XmlNamespaceScope.All)["my"];
XmlNamespaceManager ns = new XmlNamespaceManager(new NameTable());
ns.AddNamespace("my", myNamespace);
XPathNavigator xnAttNode = navigator.SelectSingleNode("//my:Attachments", ns);
string encodedAttachment = xnAttNode.InnerXml;
byte[] theData = Convert.FromBase64String(encodedAttachment);
Next, get the actual attachment data, because the raw data includes filename etc.:
using (MemoryStream ms = new MemoryStream(theData))
{
BinaryReader theReader = new BinaryReader(ms);
byte[] headerData = new byte[16];
headerData = theReader.ReadBytes(headerData.Length);
int fileSize = (int)theReader.ReadUInt32();
int attachmentNameLength = (int)theReader.ReadUInt32() * 2;
byte[] fileNameBytes = theReader.ReadBytes(attachmentNameLength);
Encoding enc = Encoding.Unicode;
string attachmentName = enc.GetString(fileNameBytes, 0, attachmentNameLength - 2);
byte[] decodedAttachment = theReader.ReadBytes(fileSize);
}
Once you get your byte[], put it into a stream, and you can use any of the following methods to extract your XML files.
.NET 4.5 - reference System.IO.Packaging.
using (MemoryStream memoryStream = new MemoryStream(decodedAttachment))
{
ZipArchive zip = new ZipArchive(memoryStream, ZipArchiveMode.Read);
foreach (var entry in zip.Entries)
{
var sw = new StreamReader(entry.Open());
string xmlString = sw.ReadToEnd();
}
}
Or pre-.NET 4.5 - reference WindowsBase:
using (MemoryStream memoryStream = new MemoryStream(decodedAttachment))
{
ZipPackage p = (ZipPackage) ZipPackage.Open(memoryStream);
foreach (var part in p.GetParts())
{
var s = new StreamReader(part.GetStream());
string xmlString = s.ReadToEnd();
}
}
Or using the OpenXML SDK:
using (MemoryStream memoryStream = new MemoryStream(decodedAttachment))
{
SpreadsheetDocument document = SpreadsheetDocument.Open(memoryStream, false);
foreach (var worksheetPart in document.WorkbookPart.WorksheetParts)
{
}
}