I'm not quite sure what you've meant. But you cannot convert ANY xml into ANY .Net object.
First your app have to know what types should be xml's attributes/elements.
You should have xsd schema for this purpose.
Let's suppose you have the following xml:
<BillsOutList>
<BillOut Account="5"
Destination="RUFBESTQWY"
Created="2010-12-14T11:34:48.297"
Modified="2010-12-14T11:34:48.297"
Amount="10.0000"
PageNumber="0" />
<BillOut Account="5"
Destination="RUFBESTQWY"
Created="2010-11-22T17:41:25.330"
Modified="2010-11-22T17:41:25.330"
Amount="10.0000" />
</BillsOutList>
The xsd should be:
<xs:schema
id="IncomeList" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="BillsOutList"
msdata:Locale="en-US">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="1">
<xs:element name="BillOut">
<xs:complexType>
<xs:attribute name="Account" type="xs:string" />
<xs:attribute name="Destination" type="xs:string" />
<xs:attribute name="Created" type="xs:string" />
<xs:attribute name="Modified" type="xs:string" />
<xs:attribute name="Amount" type="xs:decimal" />
<xs:attribute name="PageNumber" type="xs:int" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
And the corresponding class:
public class BillOut : IEntity
{
private string msAccount = String.Empty;
private decimal mdAmount;
private DateTime moModified;
private DateTime moCreated;
private string msDestination = String.Empty;
private int miPage;
[XmlAttribute]
public string Destination
{
get { return msDestination; }
set { msDestination = value; }
}
[XmlAttribute]
public DateTime Created
{
get { return moCreated; }
set { moCreated = value; }
}
[XmlAttribute]
public DateTime Modified
{
get { return moModified; }
set { moModified = value; }
}
[XmlAttribute]
public decimal Amount
{
get { return mdAmount; }
set { mdAmount = value; }
}
[XmlAttribute]
public string Account
{
get { return msAccount; }
set { msAccount = value; }
}
[XmlAttribute]
public int PageNumber
{
get { return miPage; }
set { miPage = value; }
}
public string GetXSD()
{
return "BillsOutList.xsd";
}
}
Here is the IEntity:
public interface IEntity
{
string GetXSD();
}
And to convert it:
Type TType = typeof(T);
if (TType.GetInterface("IEntity") == null)
{
RowCount = 0;
throw new CPException(TType.ToString() + " must be iherited from IEntity!");
}
string lsXSDFile = String.Empty;
try
{
T t = Activator.CreateInstance<T>();
lsXSDFile = (string)TType.InvokeMember("GetXSD", BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.Instance, null, t, null);
}
catch (Exception)
{
RowCount = 0;
throw new CPNullException(TType.ToString() + " no XSD!");
}
DataSet loDataSet = new DataSet();
Stream schema = Assembly.GetExecutingAssembly().GetManifestResourceStream("yourappnamespaces.Schemas." + lsXSDFile);
if (schema == null)
{
RowCount = 0;
throw new CPNullException(lsXSDFile + " No schema in resources!");
}
loDataSet.ReadXmlSchema(schema);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
MemoryStream loResultStream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(loResultStream, settings);
yourxmlelement.WriteTo(writer);
writer.Close();
loResultStream.Position = 0;
loDataSet.ReadXml(loResultStream, XmlReadMode.IgnoreSchema);
loResultStream.Close();
But if you can use .Net 4 you can use CodeDOM/Reflection.Emit to create an object dynamically but still you need xsd to know types of properties.