<?xml version="1.0" encoding="utf-8"?>
<ScriptCommand version="3" xmlns="http://www.waterproof.fr/PHPEdit/ScriptCommand">
	<Description>Serialize the given class to XML.</Description>
	<Category>Code Browser</Category>
	<Prototypes>
    <Prototype>
      <Param name="destinationFilename" required="true" type="string">Filename to save the XML to.</Param>
      <Param name="className" required="false" type="string">Name of the class to serialize. If no name is provided, the class at the caret will be used.</Param>
    </Prototype>
  </Prototypes>
  <scr:Script version="3" language="C#" xmlns:scr="http://www.waterproof.fr/PHPEdit/Script">
    <scr:ValidationRules>
			<scr:Rule name="Parameters.Count" possibleCounts="1,2" />
      <scr:Rule name="Documents.HasActiveProjectFile" />
    </scr:ValidationRules>
    <scr:GetEnabled />
    <scr:Execute>
      <![CDATA[
IObjectModelClassifier classToDump = null;
switch (args.Length)
{
    case 1:
        IProjectFileViewEditionObjectModel view;
        if (ProjectFileViews.GetEditionObjectModelView(Program.Documents.Projects.ActiveProjectFile, out view))
        {
            classToDump = view.GetCurrentClassifier();
        }
        break;
    case 2:
        IProjectFileViewObjectModel objectModelView;
        if (ProjectFileViews.GetObjectModelView(Program.Documents.Projects.ActiveProjectFile, CodeEditionView.GetActiveLanguage(), out objectModelView))
        {
            foreach (IObjectModelClassifier currentClassifier in objectModelView.UnitPackage.GetLocalClassifiers())
            {
                if (currentClassifier.Name.Equals(args[1], StringComparison.CurrentCultureIgnoreCase))
                {
                    classToDump = currentClassifier;
                    break;
                }
            }
        }
        break;
}

if (classToDump != null)
{
    ISerializableObjectOrientedLanguage serializableObjectOrientedLanguage = CodeEditionView.GetActiveLanguage() as ISerializableObjectOrientedLanguage;
    if (serializableObjectOrientedLanguage != null)
    {
        XmlDocument xml = new XmlDocument();
        XmlElement rootNode = xml.CreateElement("DOM");
        xml.AppendChild(rootNode);
        serializableObjectOrientedLanguage.CreateSerializer().Execute(classToDump, rootNode);
        xml.Save(args[0]);
    }
}
]]>
    </scr:Execute>
  </scr:Script>
</ScriptCommand>
