How to Deserialize XML file stream to a List<MyType>?

How to Deserialize XML file stream to a List<MyType>?

I have a MyType:

[DataContract]
[XmlRoot("MyTypeRoot")]
public class Mytype : IEquatable<Mytype>, INotifyPropertyChanged
{
    [DataMember]
    [XmlElement]
    public string Name (get;set;}

    [DataMember]
    [XmlElement]
    public string Location{get; set;}

    [DataMember]
    [XmlAttribute]
    public DateTime MyTypeCreatedOn{get; set;}
...................

}

Also:
public class MyTypeCollection : List<MyType>
{

...... Some customized methods ................
}


In the class MyRepository:
public class MyRepository
{
     public MyTypeCollection MyRead()
     {
          // Code to open the file and obtain the file stream fs
         ........

         XmlSerializer serializer = new XmlSerializer(typeof(MyTypeCollection));

         // The following code threw the exception for "{"There is an error in XML document (0, 0)."}"
         // InnerException:  {"Root element is missing."}      
        _MyTypeCollection = (MyTypeCollection)serializer.Deserialize(fs);
     }

}

XML file to read:

<?xml version="1.0" encoding="utf-8"?>
<MyTypeRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyType MyTypeCreatedOn="2013-08-26T17:17:34.8186123-04:00">
    <Name>Testing</Name>
    <Location>Florida</Location>
  </MyTypeRoot>
</MyTypeRoot>

This class used to be working. However, after we convert it from CollectionBase to List<> (as required), it no longer works.

BTW, here is the environment:  .NET 4.5,  VS2012


Anyone can help me on that?

Thanks a lot in advance.
LVL 1
chuang4630Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Your root element in the XML needs to be "Servers", not "MyTypeRoot"--unless you remove the XmlRoot attribute from your class.
0
chuang4630Author Commented:
Can you explain it in detail?
0
käµfm³d 👽Commented:
In order for your XML to be properly deserialized, it must match the data structure you created to represent it in code. Trivially, you can leave your class as-is, and have the XML mimic the class:

public class Mytype : IEquatable<Mytype>, INotifyPropertyChanged
{
    public string Name (get;set;}
    public string Location{get; set;}
    public DateTime MyTypeCreatedOn{get; set;}
}

Open in new window


<MyType>
    <Name>Alice</Name>
    <Location>Wonderland</Name>
    <MyTypeCreatedOn>01/01/2000</MyTypeCreatedOn>
</MyType>

Open in new window


You can modify this behavior by using the attributes found in the System.Xml namespace. This is what you did when you added the XmlRoot attribute. Now, instead of your XML having the root of MyType, you told the serializer to expect "Servers" when you used this:

[XmlRoot("Servers")]

Open in new window


Your XML doesn't match what the serializer expects, so you receive an error.

**edit

I see that you have modified your original post. My comment above applies to the original incarnation of the code.
0
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

chuang4630Author Commented:
The origianl file is about the server. I changed all the Server to MyType and Servers to MyTypeRoot for the sake of corporate confidentiality.

I did make sure that te XmlRoot matches the root element of the XML file. But it still does not work.
Error:Root element is missing

I do not know why
0
käµfm³d 👽Commented:
Try this modification:

fs.Position = 0;
 _MyTypeCollection = (MyTypeCollection)serializer.Deserialize(fs);

Open in new window


If that doesn't work, then you may need to show how you are initializing fs.
0
chuang4630Author Commented:
You are genius. I wish I could give you 5000 points.

But before I close this thread, I'd like to know how you reach this solution?
0
käµfm³d 👽Commented:
Frankly, experience--I ran into this on several occasions. The issue comes from the fact that your FileStream is reading in the file, and as it does, it is advancing the Position property of the stream for each byte read. By the time the whole file is read, the Position property is set to the length of the file (because that's how much data was read). When the serializer finally recieves the FileStream instance, it starts reading at Position--I'm not terribly sure why. Since you are at the end of the stream, there is no more data to read, and you get the error.

You'll run into the same thing when you use a MemoryStream, I've found.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
chuang4630Author Commented:
Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.