Wow that is alot of work
Why arent you using the SMO option. I have other options but I would suggest that you research what you need.
ScriptingOptions scropt = new ScriptingOptions();
scropt.ScriptSchema = true;
scropt.AllowSystemObjects = false;
SchemaCollection schemacoll;
schemacoll = db.Schemas;
foreach (Schema schemas in schemacoll)
{
schemas.Script(scropt);
}