在反序列化然后序列化时保留 Xml Xsd schemaLocation

Preserve Xml Xsd schemaLocation when deserializing then serializing

我正在将一个 Xml 文件反序列化为 .NET class,修改属性,然后再序列化回同一个文件。这是 .NET 模型(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

反序列化和序列化 (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);

原始 Xml 文件,包括位于 Xml 文件旁边的我的本地架构文件 xsi:schemaLocation="http://www.w3schools.com StationSetpoints.xsd"(第 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>

序列化文件时,不包括该架构路径

<?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>

如何在 .NET class 中保留该模式路径,以便序列化文件包含它?

您可以通过为此目的添加显式 属性 来反序列化和重新序列化 xsi:schemaLocation 属性,如 this answer by dtb to XmlSerialization and xsi:SchemaLocation (xsd.exe) 所示,适当翻译为 VB.NET:

Public Partial Class XmlStationSetpoints
    <XmlAttribute("schemaLocation", Namespace:="http://www.w3.org/2001/XMLSchema-instance")>
    Public Property xsiSchemaLocation As String = "http://www.w3schools.com StationSetpoints.xsd"
End Class

演示 fiddle #1 here.

如果您想硬编码 xsi:schemaLocation 的值并让它在序列化时无条件出现,您可以使用显式实现的 属性像这样:

Public Partial Class XmlStationSetpoints
    <XmlAttribute("schemaLocation", Namespace:="http://www.w3.org/2001/XMLSchema-instance")>
    Public Property xsiSchemaLocation() As String
        Get
            Return "http://www.w3schools.com StationSetpoints.xsd"
        End Get
        Set
            ' Do nothing, it's hardcoded.  A setter is required because XmlSerializer will only serialize properties with public getters and setters.
        End Set
    End Property
End Class

演示 fiddle #2 here.

并且如果您想对 xsi:schemaLocation 的值进行硬编码但控制它是否出现(因为例如它应该只在 XmlStationSetpoints 是 [=45= 的根元素时出现) ] 文件)你可以用 属性 这样做:

Public Partial Class XmlStationSetpoints
    <XmlAttribute("schemaLocation", Namespace:="http://www.w3.org/2001/XMLSchema-instance")>
    Public Property xsiSchemaLocation() As String
        Get
            Return "http://www.w3schools.com StationSetpoints.xsd"
        End Get
        Set
            ' Do nothing, it's hardcoded.  A setter is required because XmlSerializer will only serialize properties with public getters and setters.
        End Set
    End Property
    <XmlIgnore>
    Public Property xsiSchemaLocationSpecified() As Boolean
End Class

属性会捕获反序列化时是否遇到xsi:schemaLocation属性,并会控制在序列化时是否发出属性。

演示 fiddle #3 here.