This is my first code in C# ever. It compiles and works as intended (not complete), but I want to see what I'm doing right and wrong as a first-timer.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.IO;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// Event driven by browsing a file
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void uploadFile_Click(object sender, EventArgs e)
{
OpenFileDialog oOpenFileDialog = new OpenFileDialog();
oOpenFileDialog.Title = "Open XML File";
oOpenFileDialog.Filter = "XML Files|*.xml";
if (!oOpenFileDialog.CheckFileExists) { MessageBox.Show("Invalid Filename!"); }
if (IsLinux) { MessageBox.Show("Linux! Not supported!"); }
else { oOpenFileDialog.InitialDirectory = @"C:\...too..long"; }
if (oOpenFileDialog.ShowDialog() == DialogResult.OK)
{
buildTree(oOpenFileDialog.FileName.ToString());
PBC pbcXmlData = Deserialize(oOpenFileDialog.FileName.ToString());
Console.WriteLine("----------------------------------");
Console.Write("Validating.......");
ValidateXml(oOpenFileDialog.FileName.ToString());
Console.WriteLine();
Console.WriteLine("File Path: " + oOpenFileDialog.FileName.ToString());
PrintXmlData(pbcXmlData);
Console.WriteLine("-----------------------------------");
}
}
/// <summary>
/// Builds the treeview of the user's XML file
/// </summary>
/// <param name="szPbcFilePath">XML Filename</param>
public void buildTree(String szPbcFilePath)
{
XmlDataDocument xmldoc = new XmlDataDocument();
XmlNode xmlnode;
FileStream fs = new FileStream(szPbcFilePath, FileMode.Open, FileAccess.Read);
xmldoc.Load(fs);
xmlnode = xmldoc.ChildNodes[1];
treeView.Nodes.Clear();
treeView.Nodes.Add(new TreeNode(xmldoc.DocumentElement.Name));
TreeNode tNode;
tNode = treeView.Nodes[0];
AddNode(xmlnode, tNode);
}
/// <summary>
/// Helper for buildTree() to add nodes to the treeview
/// </summary>
/// <param name="inXmlNode">input XML Node</param>
/// <param name="inTreeNode">input Tree</param>
public void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
{
XmlNode xNode ;
TreeNode tNode ;
XmlNodeList nodeList ;
int i = 0;
if (inXmlNode.HasChildNodes)
{
nodeList = inXmlNode.ChildNodes;
for (i = 0; i <= nodeList.Count - 1; i++)
{
xNode = inXmlNode.ChildNodes[i];
inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = inTreeNode.Nodes[i];
AddNode(xNode, tNode);
}
}
else
{
inTreeNode.Text = inXmlNode.InnerText.ToString();
}
}
/// <summary>
/// User's PBC.xml is validated against the PBC.xsd schema.
/// If the PBC is well-formed and valid, writes to console
/// "Document is valid." Otherwise, it gives an error message.
///
/// Rules:
/// 1.) Exactly one PBCVersion, ProjectName, ProjectVersion element
/// 2.) PBC/Packages/Package/Depedencies/BuildDependency/RelativeSandboxPath element must be unique
/// 3.) PBC/Targets/Target/TargetName element must be unique
/// </summary>
/// <param name="szPbcFilePath">User's PBC.xml file path</param>
public void ValidateXml(String szPbcFilePath)
{
XmlSchemaSet oSchemas = new XmlSchemaSet();
oSchemas.Add(null, @"C:\Users\Jonathan\Documents\Visual Studio 2010\Projects\WindowsFormsApplication1\PBC.xsd");
XDocument oPbcDocument = XDocument.Load(szPbcFilePath);
string szErrorMessage = "";
oPbcDocument.Validate(oSchemas, (o, e) =>
{
szErrorMessage += e.Message + Environment.NewLine;
});
Console.WriteLine(szErrorMessage == "" ? "Document is valid" : "Document invalid: " + szErrorMessage);
}
public static void PrintXmlData(PBC pbcXmlData) {} **IRRELEVANT*
/// <summary>
/// User's PBC.xml is deserialized into a PBC object
/// </summary>
/// <param name="szPbcFilePath">User's PBC.xml file path</param>
/// <returns>Deserialized PBC.xml of object PBC</returns>
public static PBC Deserialize(String szPbcFilePath)
{
XmlSerializer oXmlDeserializer = new XmlSerializer(typeof(PBC));
TextReader oTextReader = new StreamReader(szPbcFilePath);
object oDeserializerObject = oXmlDeserializer.Deserialize(oTextReader);
PBC oPbcXmlData = (PBC)oDeserializerObject;
oTextReader.Close();
return oPbcXmlData;
}
/// <summary>
/// Detects the execution platform at runtime.
/// PlatformID 4 = Unix
/// PlatformID 6 = MacOSX
/// PlatformID 128 = Unix (.NET 1.0 and 1.1)
/// </summary>
public static bool IsLinux
{
get
{
int iPlatformVersion = (int)Environment.OSVersion.Platform;
return (iPlatformVersion == 4) || (iPlatformVersion == 6) || (iPlatformVersion == 128);
}
}
}
/// <summary>
/// Root Element: <PBC>
/// Parents: <PBCVersion>, <ProjectName>, <ProjectVersion>,
/// <Targets>, <Packages>
/// Children: <Targets/Target>, <Packages/Package>
/// </summary>
[XmlRoot("PBC")]
public class PBC
{
[XmlElement("PBCVersion")]
public String szPBCVersion;
[XmlElement("ProjectName")]
public String szProjectName;
[XmlElement("ProjectVersion")]
public String szProjectVersion;
[XmlArray("Targets")]
[XmlArrayItem("Target")]
public List<Target> oTargetList = new List<Target>();
[XmlArray("Packages")]
[XmlArrayItem("Package")]
public List<Package> oPackageList = new List<Package>();
}
public class Target : Elements
{
[XmlElement("TargetName")]
public String szTargetName { get; set; }
}
public class Package : Elements
{
[XmlElement("PackageName")]
public String szPackageName { get; set; }
}
public class Elements
{
[XmlElement("CommandLine")]
public String szCommandLine { get; set; }
[XmlElement("BuildEnvTypeName")]
public String szBuildEnvTypeName { get; set; }
[XmlElement("RelativeOutputPath")]
public String szRelativeOutputPath { get; set; }
[XmlElement("RelativeLogPath")]
public String szRelativeLogPath { get; set; }
[XmlArray("Dependencies")]
[XmlArrayItem("BuildDependency")]
public List<BuildDependency> oBuildDependency = new List<BuildDependency>();
}
/// <summary>
/// Parents: <ProjectName>, <ProjectVersion>, <TargetName>,
/// <ProjectBuild>, <RelativeSandboxPath>
/// </summary>
public class BuildDependency
{
[XmlElement("ProjectName")]
public String szBdProjectName { get; set; }
[XmlElement("ProjectVersion")]
public String szBdProjectVersion { get; set; }
[XmlElement("TargetName")]
public String szBdTargetName { get; set; }
[XmlElement("ProjectBuild")]
public String szBdProjectBuild { get; set; }
[XmlElement("RelativeSandboxPath")]
public String szBdRelativeSandboxPath { get; set; }
}
}
int
like you're doing inIsLinux
, you're defeating the purpose of the PlatformID enumeration! Instead of having easily readable, self-documenting, strongly-typed identifiers in your code, you've put magic numbers! \$\endgroup\$