Click here to Skip to main content
11,706,979 members (54,371 online)
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 5:04am
Edited 11-May-13 5: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 Sergey Alexandrovich Kryukov 145
1 OriginalGriff 141
2 chainerlt 110
3 Maciej Los 80
4 ppolymorphe 79
0 OriginalGriff 9,178
1 Sergey Alexandrovich Kryukov 8,512
2 CPallini 5,189
3 Maciej Los 4,766
4 Mika Wendelius 3,696


Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 16 May 2013
Copyright © CodeProject, 1999-2015
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