473,385 Members | 1,356 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,385 software developers and data experts.

Issues with XML Deserialization of List<CustomObject>

have an object (InputFile) that I am able to successfully serialize to XML and deserialize back into the object through an IXmlSerializable interface.

Now I'm trying to serialize a List<InputFile> to XML. I'm using the following code:


Expand|Select|Wrap|Line Numbers
  1. XmlSerializer s = new XmlSerializer(typeof(List<Inputs.InputFile>));
  2. TextWriter w = new StreamWriter("c:\\out.xml");
  3. XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
  4. ns.Add("", "");
  5. w.Serialize(w, Global.Pool.InputFiles, ns);
  6. w.Close();
This produces the following XML:


Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ArrayOfInputFile>
  3.   <InputFile FileName="file1.hdr" GUID="XYZ" TimeShift="0">
  4.   //objects from a list in the InputFile list
  5.   </InputFile>
  6.   <InputFile FileName="file2.hdr" GUID="ABC" TimeShift="0">
  7.   //objects from a list in the InputFile list
  8.   </InputFile>
  9. </ArrayOfInputFile>
This looks well formed and good to me.

I use the following to deserialize:


Expand|Select|Wrap|Line Numbers
  1. XmlSerializer s = new XmlSerializer(typeof(Inputs.InputFile));
  2. TextReader r = new StreamReader("c:\\out.xml");
  3. List<Inputs.InputFile> a = (List<Inputs.InputFile>)s.Deserialize(r);
  4. r.Close();
I get an exception on the Deserialize line:
InvalidOperationException: {"There is an error in XML document (2, 2)."}
{"<ArrayOfInputFile xmlns=''> was not expected."}

I've had no issues Serializing/Deserializing List<>s before.

I've tried variations on providing a fake-namespace or removing the namespace portion altogether. Either way I get an InvalidOperationException: {"There is an error in XML document (2, 2)."} (2,2 cooresponds to the A in ArrayOfInputFile): {"<ArrayOfInputFile xmlns=''> was not expected."}


Any ideas where I went wrong?

Thanks
Oct 2 '09 #1
3 8451
GaryTexmo
1,501 Expert 1GB
I'm... at a loss! I've done this fairly recently and it went off without a hitch. The only issue I ran into was the code putting a namespace in, which I solved with the exact same approach you used (I wonder if we found the same article :D).

I can't help but wonder why you're getting that error though... clearly your XML has no namespace in it, so I'm not sure why it's saying it does in the exception. Maybe verify 100% that you're looking at the file you think you are. I've done that a few times where I mix things up and realize I'm looking at the wrong file, especially when I switch between build modes in Visual Studio.

Another random thought... is there anything about namespaces in your class code (those items in square brackets) where you define your XML elements/attributes?
Oct 2 '09 #2
You'll notice in my deserialization I used Inputs.InputFile instead of List<Inputs.InputFile>. That simple change makes everything work!
Oct 2 '09 #3
GaryTexmo
1,501 Expert 1GB
Ahhh in the typeof! I missed it too :D Glad you got it working!
Oct 2 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: dave v | last post by:
I thought I read in an earlier post that there are known issues with deserialization in the System.Data.OracleClient.dll. Can anyone expand on that or give a workaround? I'm calling a custom...
0
by: Christopher Pragash | last post by:
Hello All, I'm trying to use XML Serialization to a class that inherits from another class and also has ArrayLists exposed as properties. When I serialize the class after populating the methods...
0
by: pak76 | last post by:
Class SignedXml is used to produce/verify signature over XML document. One of its methods, function GetIdElement, is used to select Xml elements for signature and verification and consist following...
2
by: Snowman | last post by:
Suppose I have a RootObject which holds a collection of other objects. The other objects have a property (Parent) which refers back to the "parent" collection (b.t.w. my collection is based on...
4
by: Mike Sarbu | last post by:
Hello all, I have an XML file like this: <?xml version="1.0" encoding="utf-8"?> <SomeObject xmlns="http://www.abcinc.com/objectdefinition"...
3
by: parrot toes | last post by:
Summary: I have been trying to make requests of a web service provided by Axis using a dotnet client with code generated by wsdl.exe and have been getting exceptions when trying to process the...
1
by: parrot toes | last post by:
I tried to post this question before, but there was an error when posting. I case it did get posted and in order to avoid duplication, I'll just repost a summary. I have written a dotnet client...
8
by: ashoksrini | last post by:
Hi All, I have the below requirement and would like to get some feeback from the group on the best way to implement: 1. I have WSDL defined exposing few web services. 2. We dont have a...
2
by: Joe | last post by:
I'm getting this strange error: The object with ID 203 was referenced in a fixup but does not exist. I get different numbers depending on the file I'm trying to deserialize. Any idea what it...
6
by: Bradley Plett | last post by:
I have run into this problem occasionally, and have crude ways of getting around it, but I'm wondering if anyone else has a better, more elegant solution. I have a web service and a client...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.