better xml migration strategy

run only once, remove xmlns:xsi and xmlns:xsd
This commit is contained in:
Kilian von Pflugk 2024-04-11 21:59:17 +02:00
parent e37de35445
commit 4647a4ae84

View File

@ -2,7 +2,6 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Runtime.Serialization; using System.Runtime.Serialization;
using System.Text;
using System.Xml; using System.Xml;
namespace ConfusedPolarBear.Plugin.IntroSkipper namespace ConfusedPolarBear.Plugin.IntroSkipper
@ -44,8 +43,10 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper
Console.WriteLine($"Error deserializing XML: {ex.Message}"); Console.WriteLine($"Error deserializing XML: {ex.Message}");
} }
ArgumentNullException.ThrowIfNull(result);
// Return the deserialized object // Return the deserialized object
return result!; return result;
} }
public static void MigrateXML(string filePath) public static void MigrateXML(string filePath)
@ -56,15 +57,19 @@ namespace ConfusedPolarBear.Plugin.IntroSkipper
XmlDocument xmlDoc = new XmlDocument(); XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath); xmlDoc.Load(filePath);
// Replace the namespace declaration ArgumentNullException.ThrowIfNull(xmlDoc.DocumentElement);
XmlNamespaceManager nsManager = new XmlNamespaceManager(xmlDoc.NameTable);
nsManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
nsManager.AddNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
xmlDoc.DocumentElement?.SetAttribute("xmlns", "http://schemas.datacontract.org/2004/07/ConfusedPolarBear.Plugin.IntroSkipper");
xmlDoc.DocumentElement?.SetAttribute("xmlns:i", "http://www.w3.org/2001/XMLSchema-instance");
// Save the modified XML document // check that the file has not already been migrated
xmlDoc.Save(filePath); if (xmlDoc.DocumentElement.HasAttribute("xmlns:xsi"))
{
xmlDoc.DocumentElement.RemoveAttribute("xmlns:xsi");
xmlDoc.DocumentElement.RemoveAttribute("xmlns:xsd");
xmlDoc.DocumentElement.SetAttribute("xmlns", "http://schemas.datacontract.org/2004/07/ConfusedPolarBear.Plugin.IntroSkipper");
xmlDoc.DocumentElement.SetAttribute("xmlns:i", "http://www.w3.org/2001/XMLSchema-instance");
// Save the modified XML document
xmlDoc.Save(filePath);
}
} }
} }
} }