all groups > dotnet xml > february 2006 >
You're in the

dotnet xml

group:

Deserialize and Serialize InfoPath data?


Deserialize and Serialize InfoPath data? Jeff Richardson
2/6/2006 12:09:58 PM
dotnet xml:
This is a repost from the InfoPath news group.

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.


RE: Deserialize and Serialize InfoPath data? v-kevy NO[at]SPAM online.microsoft.com
2/8/2006 3:34:58 AM
Hi Jeff,

As far as I know, when doing serialize, we cannot make the serializer add
such information which makes the xml dedicated for InfoPath. If you need to
add a tag in the serialized xml file, you can try to do an xsl
transformation on it to add that tag. HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
Re: Deserialize and Serialize InfoPath data? Jeff Richardson
2/8/2006 11:19:53 AM
Hi Kevin,

Thank you for responding.

Do you have any examples of an xsl that can add the reqired Processing
Instrucitons and Namespace information?

I have not authored an xsl before and an example would be helpful.

Thanks
Jeff.

[quoted text, click to view]

Re: Deserialize and Serialize InfoPath data? v-kevy NO[at]SPAM online.microsoft.com
2/9/2006 12:00:00 AM
Hi Jeff,

I don't have this example at hand now, but here is a good XSLT tutorial you
can reference.

http://www.w3schools.com/xsl/

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
AddThis Social Bookmark Button