Guidance Needed : Deserilization of a XML String
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Guidance Needed : Deserilization of a XML String

  1. #1
    Join Date
    Aug 2011
    Posts
    3

    Guidance Needed : Deserilization of a XML String

    I am trying to deserialize a XML String and i need guidance on how to do it.

    A simple sample code would be great for me to quickly grasp what i should be doing.

    Here is a sample xml string that i am trying to deserialize. Thank you for you help and time

    <Employee xmlns:d2p1="http://schemas.microsoft.com/2003/10/serialization/arrays" xmlns ="http://schemas.datacontract.org/2004/07/test.tester.UserInfo">

    <d2p1:KeyValueOfstringstring>
    <d2p1:Key> Name </d2p1 :Key>
    <d2p1:Value> John Smith </d2p1:Value>
    </d2p1:KeyValueOfstringstring>


    <d2p1:KeyValueOfstringstring>
    <d2p1:Key> Age </d2p1:Key>
    <d2p1:Value> 21 </d2p1:Value>
    </d2p1:KeyValueOfstringstring>

    </Employee

  2. #2
    Join Date
    Aug 2011
    Posts
    3

    Re: Guidance Needed : Deserilization of a XML String

    Forgot to mention this. I am currently on .net 3.0/Visual Studio 2008.

    Quote Originally Posted by rzack View Post
    I am trying to deserialize a XML String and i need guidance on how to do it.

    A simple sample code would be great for me to quickly grasp what i should be doing.

    Here is a sample xml string that i am trying to deserialize. Thank you for you help and time

    <Employee xmlns:d2p1="http://schemas.microsoft.com/2003/10/serialization/arrays" xmlns ="http://schemas.datacontract.org/2004/07/test.tester.UserInfo">

    <d2p1:KeyValueOfstringstring>
    <d2p1:Key> Name </d2p1 :Key>
    <d2p1:Value> John Smith </d2p1:Value>
    </d2p1:KeyValueOfstringstring>


    <d2p1:KeyValueOfstringstring>
    <d2p1:Key> Age </d2p1:Key>
    <d2p1:Value> 21 </d2p1:Value>
    </d2p1:KeyValueOfstringstring>

    </Employee

  3. #3
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,416

    Re: Guidance Needed : Deserilization of a XML String

    Create some serialization classes:
    Code:
        using System.Xml.Serialization;
        using System.IO;
        using System.Collections.Generic;
            
        [XmlType(AnonymousType=true, Namespace="http://schemas.datacontract.org/2004/07/test.tester.UserInfo")]
        [XmlRoot(Namespace="http://schemas.datacontract.org/2004/07/test.tester.UserInfo", IsNullable=false)]
        public class Employee
        {
            public static Employee FromXml( string xml )
            {
                var serializer = new XmlSerializer( typeof( Employee ) );
    
                using ( var reader = new StringReader( xml ) )
                {
                    return ( Employee ) serializer.Deserialize( reader );
                }
            }
            
            /// <remarks/>
            [XmlElement("KeyValueOfstringstring", Namespace="http://schemas.microsoft.com/2003/10/serialization/arrays")]
            public KeyValue [ ] KeyValues
            {
                get
                {
                    if ( _keyValueList == null ) _keyValueList = new List<KeyValue>( );
                    return _keyValueList.ToArray( );
                }
                set
                {
                    if ( _keyValueList == null ) _keyValueList = new List<KeyValue>( );
    
                    if ( value != null ) _keyValueList.AddRange( value );
                }
            }
    
            private List<KeyValue> _keyValueList = new List< KeyValue >();
        }
        
        public partial class KeyValue
        {
    
            /// <remarks/>
            [XmlElement( "Key" )]
            public string Key { get; set; }
            
            /// <remarks/>
            [XmlElement( "Value" )]
            public string Value { get; set; }
        }


    Load the xml up using the serialization classes. Note: I'm using an XmlDocument to read the xml from file. This just makes it easier to test - the FromXml method takes raw xml string.
    Code:
    // use an xml document to load the xml snippet for convenience
    var xmlDoc = new XmlDocument();
    xmlDoc.Load( "Employee.xml" );
    
    // Deserialize the xml
    var employee = CG.Employee.FromXml(xmlDoc.InnerXml);
    
    // Cycle through the key value pairs
    foreach( var kv in employee.KeyValues )
    {
      Console.WriteLine( String.Format( "Key: {0}", kv.Key ) );
      Console.WriteLine( String.Format( "Value: {0}\n", kv.Value ) );
    }
    If you have control over the xml schema, you might want to revise it into something a bit more compact (i.e use attributes instead of elements and shorter names).

  4. #4
    Join Date
    Aug 2011
    Posts
    3

    Talking Re: Guidance Needed : Deserilization of a XML String

    wonderful, this worked like a charm...thank you Arjay...

    Quote Originally Posted by Arjay View Post
    Create some serialization classes:
    Code:
        using System.Xml.Serialization;
        using System.IO;
        using System.Collections.Generic;
            
        [XmlType(AnonymousType=true, Namespace="http://schemas.datacontract.org/2004/07/test.tester.UserInfo")]
        [XmlRoot(Namespace="http://schemas.datacontract.org/2004/07/test.tester.UserInfo", IsNullable=false)]
        public class Employee
        {
            public static Employee FromXml( string xml )
            {
                var serializer = new XmlSerializer( typeof( Employee ) );
    
                using ( var reader = new StringReader( xml ) )
                {
                    return ( Employee ) serializer.Deserialize( reader );
                }
            }
            
            /// <remarks/>
            [XmlElement("KeyValueOfstringstring", Namespace="http://schemas.microsoft.com/2003/10/serialization/arrays")]
            public KeyValue [ ] KeyValues
            {
                get
                {
                    if ( _keyValueList == null ) _keyValueList = new List<KeyValue>( );
                    return _keyValueList.ToArray( );
                }
                set
                {
                    if ( _keyValueList == null ) _keyValueList = new List<KeyValue>( );
    
                    if ( value != null ) _keyValueList.AddRange( value );
                }
            }
    
            private List<KeyValue> _keyValueList = new List< KeyValue >();
        }
        
        public partial class KeyValue
        {
    
            /// <remarks/>
            [XmlElement( "Key" )]
            public string Key { get; set; }
            
            /// <remarks/>
            [XmlElement( "Value" )]
            public string Value { get; set; }
        }


    Load the xml up using the serialization classes. Note: I'm using an XmlDocument to read the xml from file. This just makes it easier to test - the FromXml method takes raw xml string.
    Code:
    // use an xml document to load the xml snippet for convenience
    var xmlDoc = new XmlDocument();
    xmlDoc.Load( "Employee.xml" );
    
    // Deserialize the xml
    var employee = CG.Employee.FromXml(xmlDoc.InnerXml);
    
    // Cycle through the key value pairs
    foreach( var kv in employee.KeyValues )
    {
      Console.WriteLine( String.Format( "Key: {0}", kv.Key ) );
      Console.WriteLine( String.Format( "Value: {0}\n", kv.Value ) );
    }
    If you have control over the xml schema, you might want to revise it into something a bit more compact (i.e use attributes instead of elements and shorter names).

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center