Deserializing XML Files-VBForums
Results 1 to 2 of 2

Thread: Deserializing XML Files

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2008
    Location
    Perth, Australia
    Posts
    99

    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="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Name>1:1</Name>
    <Numerator>1</Numerator>
    <Denominator>1</Denominator>
    <Units>mm</Units>

    <Name>1:2</Name>
    <Numerator>1</Numerator>
    <Denominator>2</Denominator>
    <Units>mm</Units>

    <Name>1:5</Name>
    <Numerator>1</Numerator>
    <Denominator>5</Denominator>
    <Units>mm</Units>

    <Name>1:10</Name>
    <Numerator>1</Numerator>
    <Denominator>10</Denominator>
    <Units>mm</Units>
    </Scale>

    This is the code I have used.
    Code:
    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);
            objStreamReader.Close();
    
            //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));
            Console.ReadLine();
            }
    
        public static bool SerializeFile<T>(object xmlType, string filename)
            {
            bool result;
            try
                {
                XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
                using (Stream stream = File.Open(filename, FileMode.Create))
                    {
                    xmlSerializer.Deserialize(stream);
                    result = true;
                    }
                }
            catch
                {
                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
    Posts
    144

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.