I am deserializing an Xml file into a .NET class, modifying properties, then serializing back into the same file. This is the .NET model (vb.net)
<XmlRoot("StationSetpoints")>
Public Class XmlStationSetpoints
<XmlElement("Setpoint")>
Public Property Setpoints As List(Of XmlStationSetpointsSetpoint)
End Class
<Serializable>
Public Class XmlStationSetpointsSetpoint
<XmlElement("Point")>
Public Property Points As List(Of XmlStationSetpointsSetpointPoint)
' etc...
End Class
Deserialization and serialization (c#)
var settings = New XmlStationSetpoints();
var serializer = New XmlSerializer(XmlStationSetpoints);
// deserialize
using StreamReader sr = new StreamReader(path)
settings = (XmlStationSetpoints)serializer.Deserialize(sr);
// serialize
using StreamWriter sw = new StreamWriter(path, false)
serializer.Serialize(sw, settings);
The original Xml file, including my local schema file which resides next to the Xml file xsi:schemaLocation="http://www.w3schools.com StationSetpoints.xsd"
(line 5)
<?xml version="1.0" encoding="utf-8"?>
<StationSetpoints
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xsi:schemaLocation="http://www.w3schools.com StationSetpoints.xsd">
<Setpoint PartNumber="108022">
<Point InstrumentName="PD Stage" Value="10"/>
</Setpoint>
<Setpoint PartNumber="107983">
<Point Order="2" InstrumentName="PD Stage" Value="7.5"/>
</Setpoint>
</StationSetpoints>
When the file is serialized, that schema path is not included
<?xml version="1.0" encoding="utf-8"?>
<StationSetpoints
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Setpoint PartNumber="108022">
<Point Order="0" InstrumentName="PD Stage" Value="10"></Point>
</Setpoint>
<Setpoint PartNumber="107983">
<Point Order="2" InstrumentName="PD Stage" Value="7.5"></Point>
</Setpoint>
</StationSetpoints>
How can I preserve that schema path in the .NET class, so that the serialized file includes it?