I have the following XML file:
<?xml version="1.0" encoding="utf-8" ?>
<scripts>
<ScriptName>
<name>
"My Name"
</ScriptName>
<ScriptBody>
"body contents"
</ScriptBody>
</script>
</scripts>
And the following object:
public class DbScript
{
#region Constructors
public DbScript()
: this(string.Empty, string.Empty)
{
}
public DbScript(string name, string body)
{
ScriptName = name;
ScriptBody = body;
}
#endregion
#region Properties
/// <summary>
/// Script name
/// </summary>
public string ScriptName { get; set; }
/// <summary>
/// Script body
/// </summary>
public string ScriptBody { get; set; }
#endregion
}
What is the quickest way to populate the collection of DBScript object from the contents of the XML file? Should I look into serializers?
Thanks!
Using .Net Serialization is absolutely my preference, in this case you would deserialize the file to an object. If you take your xml (which I edited a bit):
Then you create some classes the represents the xml:
And then when that is all set up correctly you can deserialize the file:
Xml serialization is really powerful, check it out in the docs: http://msdn.microsoft.com/en-us/library/ms950721.aspx.
robb