Search code examples
c#xmlxml-serialization

XML Serialization error when updating an existing XML file


I have a problem that I didn't find in any thread! when updating the XML file(Through serialization) with new Object values it works only when new values are bigger than the existing attribute values in the XML but doesn't work when the new values are less than the existing values.

here's the code I use for serialization

NewFit NF = new NewFit { A1 = CA1.SelectedItem.ToString(), A2 = A2.Text, A3 = A3.Text, A4 = A4.Text, A5 = A5.Text, A6 = A6.Text, A7 = A7.Text, A8 = A8.Text, B1 = B1.Text, B2 = B2.Text, B3 = B3.Text, B4 = B4.Text, B5 = B5.Text, B6 = B6.Text, B7 = B7.Text, B8 = B8.Text, C1 = C1.Text, C2 = C2.Text, C3 = C3.Text, C4 = C4.Text, C5 = C5.Text, C6 = C6.Text, C7 = C7.Text, C8 = C8.Text, D1 = D1.Text, D2 = D2.Text, D3 = D3.Text, D4 = D4.Text, D5 = D5.Text, D6 = D6.Text, D7 = D7.Text, D8 = D8.Text,
            E1 = E1.Text, E2 = E2.Text, E3 = E3.Text, E4 = E4.Text, E5 = E5.Text, E6 = E6.Text, E7 = E7.Text, E8 = E8.Text, F1 = F1.Text, F2 = F2.Text, F3 = F3.Text, F4 = F4.Text, F5 = F5.Text, F6 = F6.Text, F7 = F7.Text, F8 = F8.Text, G1 = G1.Text, G2 = G2.Text, G3 = G3.Text, G4 = G4.Text, G5 = G5.Text, G6 = G6.Text, G7 = G7.Text, G8 = G8.Text, H1 = H1.Text, H2 = H2.Text, H3 = H3.Text, H4 = H4.Text, H5 = H5.Text, H6 = H6.Text, H7 = H7.Text, H8 = H8.Text, I1 = I1.Text, I2 = I2.Text,
            I3 = I3.Text, I4 = I4.Text, I5 = I5.Text, I6 = I6.Text, I7 = I7.Text, I8 = I8.Text, J1 = J1.Text, J2 = J2.Text, J3 = J3.Text, J4 = J4.Text, J5 = J5.Text, J6 = J6.Text, J7 = J7.Text, J8 = J8.Text, K1 = K1.Text, K2 = K2.Text, K3 = K3.Text, K4 = K4.Text, K5 = K5.Text, K6 = K6.Text, K7 = K7.Text, K8 = K8.Text,L1 = L1.Text,L2=L2.Text,L3=L3.Text,L4=L4.Text,L5=L5.Text,L6=L6.Text,L7=L7.Text,L8=L8.Text};

        XmlSerializer serial = new XmlSerializer(typeof(NewFit));
        //JsonConvert.SerializeObject(NF);
        using (FileStream fs = new FileStream(MyCmnd.DocPath, FileMode.Open, FileAccess.Write))
        {

            serial.Serialize(fs, NF);
            fs.Close();
        }

           **example**

if existing value is <A1>Class 150</A1> and new value is "class 150 AA" then everything is fine

when existing value is <A1>Class 150</A1> and new value is "Cl1" then it gives a serialization error at root level.

Thanks for your time!


Solution

  • Ideally you need to provide a verifiable example (i.e. one you can copy and paste and run to prove the issue), but I'm going to guess that FileMode.Open is your problem.

    Serialize will just write to the FileStream and stop when it finishes. If your file is, say, 1000 bytes and your new file is smaller - say 800 bytes - you'll have 200 bytes of garbage at the end. The contents will almost certainly be badly formed XML, and when you try to deserialize it will fail as a result.

    Use FileMode.Truncate instead. Per the docs, this will set its length to 0 bytes on opening:

    Specifies that the operating system should open an existing file. When the file is opened, it should be truncated so that its size is zero bytes.

    If you need to handle the case where the file might not already exist, use FileMode.Create:

    Specifies that the operating system should create a new file. If the file already exists, it will be overwritten ... FileMode.Create is equivalent to requesting that if the file does not exist, use CreateNew; otherwise, use Truncate.