/// <summary> /// Takes an InstallSettings object and serializes into xml /// </summary> /// <param name="incoming">An InstallSettings object</param> /// <returns>String containing the serialized objects xml</returns> public string Serialize(FullInstallSettings incoming) { //To determine what dataObject to use for serialization based on the dhcp button selected bool useDHCP = (incoming.Dhcp.Equals("true")) ? true : false; //Resulting xml string var xmlResult = ""; //Settings to remove namspaces from the xml XmlSerializerNamespaces xsn = new XmlSerializerNamespaces(); xsn.Add(string.Empty, string.Empty); //Create the custom stringWriter to use utf-8 instead of utf-16 StringWriterWithEncoding utf8StringWriter = new StringWriterWithEncoding(Encoding.UTF8); XmlWriterSettings ws = new XmlWriterSettings() { Indent = true, Encoding = Encoding.UTF8 }; //Create a serializer XmlSerializer serializer = null; if (useDHCP == true) //Serialize the DHCP version { DHCPInstallSettings settings = new DHCPInstallSettings(incoming.CallSign, incoming.Dhcp, incoming.Updated); //Initialize the serializer and writer serializer = new XmlSerializer(typeof(DHCPInstallSettings)); XmlWriter writer = XmlWriter.Create(utf8StringWriter, ws); //Srialize and then save the output string to result and pass back serializer.Serialize(writer, settings, xsn); xmlResult = utf8StringWriter.ToString(); } else // Serialize the full installsettings version { XmlSerializer xs = new XmlSerializer(typeof(FullInstallSettings)); //Initialize the serializer and writer serializer = new XmlSerializer(typeof(FullInstallSettings)); XmlWriter writer = XmlWriter.Create(utf8StringWriter, ws); //Srialize and then save the output string to result and pass back serializer.Serialize(writer, incoming, xsn); xmlResult = utf8StringWriter.ToString(); } return(xmlResult); }
/// <summary> /// Looks for current settings in config.xml and deserializes into an object /// </summary> /// <returns>InstallSettings object</returns> /// <param name="fileName">file name of the file to deserialize</param> public FullInstallSettings Deserialize(string fileName) { XmlDocument doc = new XmlDocument(); doc.Load(fileName); string callsignText, dhcpText, ipText, subnetText, gatewayText, preferredDnsText, alternateDnsText; callsignText = dhcpText = ipText = subnetText = gatewayText = preferredDnsText = alternateDnsText = ""; XmlNodeList callsign; XmlNodeList dhcp; XmlNodeList ip; XmlNodeList subnet; XmlNodeList gateway; XmlNodeList preferredDns; XmlNodeList altDns; try // Check to make sure the callsign element is present, else assign empty string { callsign = doc.GetElementsByTagName("callsign"); callsignText = callsign[0].InnerText; } catch (NullReferenceException) { callsignText = ""; } try // Check to make sure the dhcp element is present, else assign empty string { dhcp = doc.GetElementsByTagName("useDHCP"); dhcpText = dhcp[0].InnerText; } catch (NullReferenceException) { dhcpText = ""; } if (dhcpText.Equals("false") || dhcpText.Equals("")) // if dhcp is set to manual gather all other fields { try // Check to make sure the ip element is present, else assign empty string { ip = doc.GetElementsByTagName("ip"); ipText = ip[0].InnerText; } catch (NullReferenceException) { ipText = ""; } try // Check to make sure the subnet element is present, else assign empty string { subnet = doc.GetElementsByTagName("subnet"); subnetText = subnet[0].InnerText; } catch (NullReferenceException) { subnetText = ""; } try // Check to make sure the gateway element is present, else assign empty string { gateway = doc.GetElementsByTagName("gateway"); gatewayText = gateway[0].InnerText; } catch (NullReferenceException) { gatewayText = ""; } try // Check to make sure the preferredDns element is present, else assign empty string { preferredDns = doc.GetElementsByTagName("dns1"); preferredDnsText = preferredDns[0].InnerText; } catch (NullReferenceException) { preferredDnsText = ""; } try // Check to make sure the alternateDns element is present, else assign empty string { altDns = doc.GetElementsByTagName("dns2"); alternateDnsText = altDns[0].InnerText; } catch (NullReferenceException) { alternateDnsText = ""; } } FullInstallSettings settings = new FullInstallSettings(callsignText, dhcpText, ipText, subnetText, gatewayText, preferredDnsText, alternateDnsText); return(settings); }
public MainWizard() { InitializeComponent(); // Load existing xml settings if present try { //Retrieve and store the imported xml data InstallImporter importer = new InstallImporter(); string fileName = "config.xml"; baseSettings = importer.Deserialize(fileName); } catch (Exception x) { baseSettings = new FullInstallSettings(); } //Auto populate the network fields. txtCallsign.Text = baseSettings.CallSign; //Split the pulled data on the dot and populate the ip textboxes try { string[] ipArray = baseSettings.IpAddress.Split('.'); ipMi1.Text = ipArray[0]; ipMi2.Text = ipArray[1]; ipMi3.Text = ipArray[2]; ipMi4.Text = ipArray[3]; string[] subnetArray = baseSettings.SubnetMask.Split('.'); subnetMi1.Text = subnetArray[0]; subnetMi2.Text = subnetArray[1]; subnetMi3.Text = subnetArray[2]; subnetMi4.Text = subnetArray[3]; string[] gatewayArray = baseSettings.DefaultGateway.Split('.'); gatewayMi1.Text = gatewayArray[0]; gatewayMi2.Text = gatewayArray[1]; gatewayMi3.Text = gatewayArray[2]; gatewayMi4.Text = gatewayArray[3]; string[] prefDns = baseSettings.PreferredDns.Split('.'); prefDnsMi1.Text = prefDns[0]; prefDnsMi2.Text = prefDns[1]; prefDnsMi3.Text = prefDns[2]; prefDnsMi4.Text = prefDns[3]; string[] altArray = baseSettings.AlternateDns.Split('.'); altDnsMi1.Text = altArray[0]; altDnsMi2.Text = altArray[1]; altDnsMi3.Text = altArray[2]; altDnsMi4.Text = altArray[3]; } catch (Exception) { } rdbManualIP.Checked = (baseSettings.Dhcp.Equals("false")) ? true : false; }
private void wizardControl1_Finished(object sender, EventArgs e) { // Collect all input settings and prepare for xml serialization finalSettings = new FullInstallSettings(); finalSettings.CallSign = txtCallsign.Text; finalSettings.Dhcp = (rdbAutoIP.Checked == true) ? "true" : "false"; finalSettings.Updated = DateTime.Now.ToString("yyyyMMddHHmm"); string xmlResult = ""; InstallImporter importer = null; if (rdbAutoIP.Checked) //Discard any old imported network settings { } else // Use new settings { // Concat all the boxes for each field together and then remove all whitespace just as a safety measure finalSettings.IpAddress = ipMi1.Text + "." + ipMi2.Text + "." + ipMi3.Text + "." + ipMi4.Text; finalSettings.IpAddress.Replace(" ", string.Empty); finalSettings.SubnetMask = subnetMi1.Text + "." + subnetMi2.Text + "." + subnetMi3.Text + "." + subnetMi4.Text; finalSettings.SubnetMask.Replace(" ", string.Empty); finalSettings.DefaultGateway = gatewayMi1.Text + "." + gatewayMi2.Text + "." + gatewayMi3.Text + "." + gatewayMi4.Text; finalSettings.DefaultGateway.Replace(" ", string.Empty); finalSettings.PreferredDns = prefDnsMi1.Text + "." + prefDnsMi2.Text + "." + prefDnsMi3.Text + "." + prefDnsMi4.Text; finalSettings.PreferredDns.Replace(" ", string.Empty); string altdns = altDnsMi1.Text + "." + altDnsMi2.Text + "." + altDnsMi3.Text + "." + altDnsMi4.Text; //If alternate dns is only 3 characters ("..."), save it as an empty string, else, use the inputed altDns if (altdns.Length == 3) { finalSettings.AlternateDns = ""; } else { finalSettings.AlternateDns = altdns; finalSettings.AlternateDns.Replace(" ", string.Empty); } } try { importer = new InstallImporter(); //Serialize the object to xml xmlResult = importer.Serialize(finalSettings); } catch (Exception) { } //Write the xml to the file try { importer.WriteXmlToCurrentDirectory(xmlResult); MessageBox.Show("Safely remove the thumb drive and insert into the Syncbox. It should stay in the Syncbox while it's in use.", "Configuration Saved", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception) { MessageBox.Show("Drive removed. Reinsert the syncbak drive and try again. Application now closing", "Drive removed!", MessageBoxButtons.OK, MessageBoxIcon.Error); } }