Deserialize and Serialize InfoPath data?

  • Thread starter Jeff Richardson
  • Start date
J

Jeff Richardson

Hi,
I am writing a SharePoint application that works with InfoPath forms. When
a user submits a completed InfoPath form to a forms library my code
processes the posted xml file by reading the data into a C# class object
that was generated by XSD.EXE. The class was generated from the
myschema.xsd file that is contained in the InfoPath solution with a command
line like:

xsd.exe myschema.xsd /c

With the generated class I can read (Deserialize) InfoPath's xml file into a
strongly typed C# object. Manipulate the data using the C# object, and then
write (Serialize) the C# object back to an xml file with code like this:

// InfoPathData class generated by XSD.EXE
private InfoPathData formData;

// Fill InfoPathData object with data from the InfoPath form
formData = (InfoPathData)DeserializeFile(myFile, typeof(InfoPathData));

// Modify the data
formData.Field1 = "New Data";

//update the InfoPath form
myFile.SaveBinary(SerializeObject(projInfo, typeof(InfoPathData)));

protected object DeserializeFile(SPFile myFile, Type myType)
{
MemoryStream fileStream = new MemoryStream(myFile.OpenBinary());
XmlReader reader = XmlReader.Create(fileStream);
XmlSerializer serializer = new XmlSerializer(myType);
return serializer.Deserialize(reader);
}

protected byte[] SerializeObject(object myObject, Type myType)
{
MemoryStream fileStream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(fileStream);
XmlSerializer serializer = new XmlSerializer(myType);
serializer.Serialize(writer, myObject);
return fileStream.ToArray();
}

The problem that I am having is that the serialized xml file does not have
the preamble that identifies the xml file as an InfoPath document as well as
it is missing a namespace declaration.

Are there Serialization attributes that I can add to the generated C# class
that would cause the XmlSerializer to output the correct InfoPath specific
xml?

Are there changes to the XSD.EXE command like that would cause the generated
C# class to have the correct Serialization attributes?

Is there a better way to Deserialize and Serialize InfoPath documents?

Thanks in advance,
Jeff.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top