Question How can I Load Diferrent XML Files?

SebGM2018

New member
Joined
Jun 16, 2018
Messages
4
Programming Experience
3-5
Good day, I need to do Something like this https://youtu.be/ecvunlsNm9k

But Instead Of using tables, using textboxes In one form, and with different Files, let me explain you with an example: In Form 1, There are 2 Textboxes, and 1 button that button opens a file like in the video, but with the difference that can open different Files (Ex. File A and file B)like File A in one Textbox and File B in the second one.

What I Have Tried.

I've tried doing a list in the application settings of type file, and when the form 1 loads do a for each loop like

foreach (File in List) //Deserialize file in List

But it doesn't allow me to do lists with 'file' type. Thanks.

Update And what if I save Two strings in a XML file, and let's say that now there are 4 textboxes, 2 for file a and another 2 for file B, how can I deserialize that info and place it into that textboxes?

Update 2 I need to deserialize two files, one open in a textbox and another in other Textbox, (This should repeat every time I load the form, like first you select one file and now the app knows which file you selected open it even if you close and open again the app), The XML Values are date, string and bool. update 3 This is my complete code:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    if (File.Exists(openFileDialog1.FileName))
    {
        if ((openFileDialog1.OpenFile()) != null)
        {
            if (openFileDialog1.FileName != null)
            {
                openFileDialog1.Filter = "Analytica Files | *.analy";
                XmlSerializer XS = new XmlSerializer(typeof(Reminders.Information));
                FileStream FS = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read)
                Information Info = (Information)XS.Deserialize(FS);
                XS.Deserialize(FS);
                Settings.Default.Counter++;
                Settings.Default.Save();

The problem is that when I try to open a file, appears the next error: System.InvalidOperationException: 'Error in The document XML (0, 0).' Inner Exception
 
Last edited by a moderator:
Your code is a bit crazy. Firstly, there's no point testing for yourself that the selected file exists when you can set CheckFileExists to 'true' on the dialogue itself. If you do that then it won't close unless the user selects an existent file.

Secondly, you're calling OpenFile on the dialogue and simply discarding the result. That OpenFile method returns an open Stream on the file so you're supposed to use it to access the file data. Not only does discarding that Stream mean that it will stay open until you exit the application, but you are then opening a another FileStream later on in the code.

Thirdly, what's the point of testing whether the FileName property is null when you have already checked that a file with that path exists and you've opened that file?

You should be doing something like this:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    using (var file = openFileDialog1.OpenFile())
    {
        // file is the Stream and you use it here.
    } // The Stream is implicitly closed here.
}

Any objects that implement IDisposable that are created and destroyed in the same block should be created with a 'using' statement, so that they are implicitly disposed at the end of the block.

Finally, you are calling Deserialize twice. Why would you think that that was a good idea? Get rid of the second call.

If your issue persists after making those changes, post your new code and also post the code that serialises the data in the first place.
 

Latest posts

Back
Top Bottom