-
August 3rd, 2011, 06:45 AM
#1
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 xmlns2p1="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
-
August 3rd, 2011, 06:50 AM
#2
Re: Guidance Needed : Deserilization of a XML String
Forgot to mention this. I am currently on .net 3.0/Visual Studio 2008.
Originally Posted by rzack
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 2p1="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
-
August 3rd, 2011, 10:49 PM
#3
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).
-
August 6th, 2011, 10:55 AM
#4
Re: Guidance Needed : Deserilization of a XML String
wonderful, this worked like a charm...thank you Arjay...
Originally Posted by Arjay
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|