Deserializing XML Files-VBForums
Results 1 to 2 of 2

Thread: Deserializing XML Files

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2008
    Perth, Australia

    Deserializing XML Files

    I am trying to deserialize an XML file, but I only get the first entry.

    Here is the data from my xml file.

    <?xml version="1.0" encoding="utf-8"?>
    <Scale xmlns:xsi="" xmlns:xsd="">




    This is the code I have used.
    using System;
    using System.IO;
    using System.Xml.Serialization;
    public class Test
        public static void Main()
            StreamReader objStreamReader = new StreamReader(@"C:\Users\burgessb2\desktop\Scales.xml");
            Scale s2 = new Scale();
            XmlSerializer x = new XmlSerializer(typeof(Scale));
            s2 = (Scale)x.Deserialize(objStreamReader);
            //Display property values of the new product object.
            Console.WriteLine(string.Format("Name: {0}{1}\tNumerator: {2}\tDenominator: {3}",s2.Name, s2.Units,s2.Numerator,s2.Denominator));
        public static bool SerializeFile<T>(object xmlType, string filename)
            bool result;
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                using (Stream stream = File.Open(filename, FileMode.Create))
                    result = true;
                result = false;
            return result;
    public class Scale
    	private string _Name;
    	private double _Numerator;
        private double _Denominator;
        private string _Units;
    	public string Name
            get { return _Name; }
            set { _Name = value; }
    	public double Numerator
    		get { return _Numerator; }
    		set { _Numerator = value; }
    	[XmlElementAttribute(ElementName = "Denominator")]
    	public double Denominator
    		get { return _Denominator; }
    		set { _Denominator = value; }
        public string Units
            get { return _Units; }
            set { _Units = value; }
    The only result I get is
    Name: 1:1mm Numerator: 1 Denominator: 1

    Can anyone give me a clue as to where I am doing wrong?

  2. #2
    Addicted Member
    Join Date
    Sep 2008

    Re: Deserializing XML Files

    You're using single instance of object, not a collection.
    When you're displaying results you need to use for/foreach loop if you want to display all objects in collection.
    In function SerializeFile<T> you're using deserialize(stream) stream instead of serialize(stream).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts


Click Here to Expand Forum to Full Width

Survey posted by VBForums.