473,386 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Deserialize and Serialize InfoPath data?

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.

Feb 6 '06 #1
3 4768
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."

Feb 8 '06 #2
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.

"Kevin Yu [MSFT]" <v-****@online.microsoft.com> wrote in message
news:%2***************@TK2MSFTNGXA01.phx.gbl...
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."

Feb 8 '06 #3
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."

Feb 9 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Carl Mercier | last post by:
Hi! I have 2 different applications/assembly. The first one creates an object and serializes it to a textfile on disk. The second one has the the exact same class (copied/pasted). It reads...
7
by: Ian Tompsett | last post by:
H I was wondering if it possible for an object to serialize/deserialize itself from XML. I'd be guessing that it would need to use the XmlSerializer class, but that seems to want to create a...
2
by: Greg | last post by:
I'm writing a class in C# .... I have a collection calls Reports made up of Report objects. I'm trying to deserialize an XML file that looks like : <Reports> <Report> <Title>some title</Title>...
1
by: Bsiang Tan | last post by:
I try to serialize my object into xml it work, but I can't deserialize it back. I get an exception.. :- An unhandled exception of type 'System.Reflection.TargetInvocationException' occurred...
2
by: PCH | last post by:
I have 2 functions, one to serialize an object, and one to deserialize it. I can serialize just fine, the problem is when I try to deserialize it later... I get an error: {"Invalid...
4
by: George Addison | last post by:
I understand this might not be the optimal method of deserialization, but how can I deserialize a class to itself? Something like: Public Sub New(Optional ByVal filename as string = Nothing)...
0
by: John Manion via .NET 247 | last post by:
Long Post, thanks for your patience... I have and XML file that looks something like this: <?xml version="1.0" encoding="utf-8" ?> <Settings> <Location> <X>30</X> <Y>40</Y> </Location>...
17
by: Peter | last post by:
How would would you deserialize this example below? Imports System Imports System.Collections Imports System.IO Imports System.Xml.Serialization Public Class App1 Shared Sub Main()
6
by: Peter Franks | last post by:
Is it possible to deserialize a class that has a public property w/ a setter, but no getter? I'm not finding anything that would allow this -- Presuming that is is NOT possible, what are the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.