0

I updated the question @ xml content changed after serialization & deserialization want to pass in xml to map object

Any help are appreciated. Thanks a lot.

I am new to all of these: I am trying to pass in an xml file from a webapplication browse button to a service, call and then run it.

I searched and did the following:

string InputFilePath = FileUpload1.PostedFile.FileName;

MyServiceTypeClass _MyServiceTestObj = new MyServiceTypeClass ();
XmlSerializer SerializerObj = new XmlSerializer(typeof(MyServiceTypeClass ));
StreamWriter WriteFileStream = new StreamWriter(@InputFilePath);
SerializerObj.Serialize(WriteFileStream, _MyServiceTestObj );
WriteFileStream.Close();

FileStream XmlStream = new FileStream(@InputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
MyServiceTypeClass _ServiceTypeLoaded = (MyServiceTypeClass )SerializerObj.Deserialize(XmlStream);
XmlStream.Close();

After I run it and upload an xml file, I found that _ServiceTypeLoaded fields are almost all null for the fields. And the xml file content has been changed after running the above lines. _ServiceTypeLoaded only has 3 fields not null, and the xml file has changed to other format with lots of tags missing.

Some More Details:
MyServicetypeClass structure:
MyServiceTypeClass inherited from Class A and has some of its own members; Class A inherited form Class B and has some of its own members; Class B inherited from Class C and has some its own members etc. There are 5 layers in the relationship.

Community
  • 1
  • 1
user2751691
  • 401
  • 2
  • 10
  • 32
  • Could you provide some more code? Like for `MyServiceTypeClass`. – meilke Sep 18 '13 at 18:38
  • This is almost surely due to the incorrect use of serialization attributes on your class... try to simplify your situation to one level of inheritance with a few members... if you still have the problem, you can post your simple reproducible problem for others to help. You can also consider using DataContractSerializer as the generally preferred serialization class, in which case the attributes will differ, however you would have less control over the form of the XML. – TCC Sep 18 '13 at 19:49
  • Thank you very much. In my case, the passed in xmls are already preprocessed to fit the service type. I just want to pass in the xmls and give the vales stored in it to a object in C#. Do I need to do both serialization and deserialization in this case? or just one of them is enough? In any way, Is there an easy way for me to serialize it? As there are many serviceclass types and there will be too much work if i manually serialize for each one. Great thanks. – user2751691 Sep 19 '13 at 02:32
  • Sounds like you want to deserialize some provided xml into an object. Usually either the class is written first with serialization in mind, or the class is generated automatically from serialized sample data or a published XSD for the xml message format. If you are in neither of these cases, I'm not sure of any automatic methods to deserialize xml to your class. I don't think you can use .net serialization with your classes without at least adding serialization attributes, unless they already exist (you didn't post your class). Maybe some 3rd party tools would be of some use? I don't know – TCC Sep 19 '13 at 20:36
  • Hi hi, I updated it @ http://stackoverflow.com/questions/18899453/xml-content-changed-after-serialization-deserialization-want-to-pass-in-xml-to – user2751691 Sep 20 '13 at 18:16

1 Answers1

0

Used Xslt to convert the input xml to a desired xml format, then use it as input variable

user2751691
  • 401
  • 2
  • 10
  • 32