开发者

cannot figure out why this does not write into a new xml file anymore

开发者 https://www.devze.com 2023-02-13 16:39 出处:网络
recently i made some changes to the button \"save\" event and it just doenst write into the xml file or create a new one anymore. I can\'t seem to figure out what is wrong over here...My savepath stri

recently i made some changes to the button "save" event and it just doenst write into the xml file or create a new one anymore. I can't seem to figure out what is wrong over here...My savepath string here looks o开发者_如何学Gok to me though. Please help me figure out what is wrong with my code. Many thanks.

Here is my code:

private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            string savepath;
            SaveFileDialog DialogSave = new SaveFileDialog();
            // Default file extension
            DialogSave.DefaultExt = "txt";
            // Available file extensions
            DialogSave.Filter = "XML file (*.xml)|*.xml|All files (*.*)|*.*";
            // Adds a extension if the user does not
            DialogSave.AddExtension = true;
            // Restores the selected directory, next time
            DialogSave.RestoreDirectory = true;
            // Dialog title
            DialogSave.Title = "Where do you want to save the file?";
            // Startup directory
            DialogSave.InitialDirectory = @"C:/";
            if (DialogSave.ShowDialog().Equals(true))
            {
                savepath = DialogSave.FileName;
                //DialogSave.Dispose();
                DialogSave = null;

                FormSaving formsaving = new FormSaving();
                formsaving.Builderemail = comboBox1.SelectedIndex;
                formsaving.Manageremail = comboBox2.SelectedIndex;

                if (MajorversionresultLabel != null && MajorversionresultLabel.Content != null && MajorversionLabel.Content.ToString() != string.Empty)
                    formsaving.Majorversion = MajorversionresultLabel.Content.ToString();
                if (MinorversionresultLabel != null && MinorversionresultLabel.Content != null && MinorversionLabel.Content.ToString() != string.Empty)
                    formsaving.Minorversion = MinorversionresultLabel.Content.ToString();
                if (ProjectnumberresultLabel != null && ProjectnumberresultLabel.Content != null && ProjectnumberLabel.Content.ToString() != string.Empty)
                    formsaving.Projectnumber = ProjectnumberresultLabel.Content.ToString();
                if (BuildnumberresultLabel != null && BuildnumberresultLabel.Content != null && BuildnumberLabel.Content.ToString() != string.Empty)
                    formsaving.Buildnumber = BuildnumberresultLabel.Content.ToString();
                if (PreviousbuildversionresultLabel != null && PreviousbuildversionresultLabel.Content != null && PreviousbuildversionresultLabel.Content.ToString() != string.Empty)
                    formsaving.Previousbuildversion = PreviousbuildversionresultLabel.Content.ToString();

                formsaving.Startzbuildfrom = StartzbuildfromcomboBox.SelectedIndex;
                formsaving.Fullorsingle = FullorsinglecomboBox.SelectedIndex;
                formsaving.Builddrive = BuilddrivecomboBox.SelectedIndex;

                if (TruecmtipresultTextBlock != null && TruecmtipresultTextBlock.Text != null && TruecmtipresultTextBlock.Text != string.Empty)
                    formsaving.Truecmtip = TruecmtipresultTextBlock.Text;
                formsaving.Truecmcomments = TruecmcommentsresultTextBlock.Text;

                ...

                using (Stream savestream = new FileStream(savepath, FileMode.Create))
                {
                    XmlSerializer serializer = new XmlSerializer(typeof(FormSaving));
                    serializer.Serialize(savestream, formsaving);
                }
            }
            DialogSave.Dispose();
        }


Which SaveFileDialog class are you using ? The one from Microsoft.Win32 or the one from System.Windows.Forms ?

The first one returns bool?, so your code should be OK (except for setting DialogSave to null, then disposing it outside of the if block).

If you're using the second one, it returns a DialogResult enum, which can never equal 'true'.


I bet your original version of the code was using SelectedItem everywhere you're using SelectedIndex, unless you're really doing something horrible.

Also, good heavens, do you need to be using data binding.

0

精彩评论

暂无评论...
验证码 换一张
取 消