Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
How can I deserialize following XML file using C# classes. But I don't want to use XMLReader or same thing. I need to know how it can be done by using C# classes and its properties
 
<Visibilities>
    <Visibilities AppName="Test2">
      <Visibility DisplayName="Full Name" ListOrder="2">
        <FullName>
          <FirstName PropertyName="FullName.FirstName" DisplayName="First Name"></FirstName>
          <MiddleName PropertyName="FullName.MiddleName" DisplayName="Middle Name"></MiddleName>
          <LastName PropertyName="FullName.LastName" DisplayName="Last Name"></LastName>
        </FullName>
      </Visibility>
    </Visibilities>
  </Visibilities>
 

I want to use following function
 
 public static T LoadXml<T>(string fileName) where T : new()
        {
            StreamReader reader = null;
            try
            {
                // Deserialize
                XmlSerializer ser = new XmlSerializer(typeof(T));
                reader = new StreamReader(fileName);
                T t = (T)ser.Deserialize(reader);
                if (t == null) throw new NullReferenceException("Invalid xml format");
                return t;
            }
            catch (Exception ex)
            {
                //TODO: Error
            }
            finally
            {
                if (reader != null) reader.Close();
            }
 
            return new T();
        }
 
Thanks in Advance.
Posted 11-May-13 6:04am
Edited 11-May-13 6:17am
v3
Comments
Dave Kreskowiak at 11-May-13 12:33pm
   
Why would you want to re-invent "the wheel"??
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

What you really need is Data Contract: http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].
 
Please see my past answers advocating this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].
 
This is much better then the solution referenced in Solution 1. The difference is performance. Data Contract serializer does the reflection only once per type. It creates a serialization assembly on the fly, using System.Reflection.Emit. If you wanted to develop similar solution by yourself, you would face with the need to know CIL very well, and Emit code is quite difficult to debug.
 
—SA
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 280
1 Jochen Arndt 150
2 Kornfeld Eliyahu Peter 120
3 PIEBALDconsult 110
4 Richard MacCutchan 105
0 OriginalGriff 6,055
1 DamithSL 4,621
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,500
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 16 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100