在反序列化和序列化时保留Xml Xsd schemaLocation



我正在将一个Xml文件反序列化为一个。net类,修改属性,然后再序列化回同一个文件。这是。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类中保留该模式路径,以便序列化文件包含它?

您可以通过为此目的添加显式属性来反序列化和重新序列化xsi:schemaLocation属性,如dtb对XmlSerialization和xsi:SchemaLocation (xsd.exe)的回答所示,适当地转换为VB。净:

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

在这里演示小提琴#1。

如果您希望硬编码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

在这里演示小提琴#2。

如果你想硬编码xsi:schemaLocation的值,但控制它是否出现(因为例如,它应该只出现在XmlStationSetpoints是XML文件的根元素时),你可以这样做与xsiSchemaLocationSpecified属性,如:

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属性,并将控制在序列化过程中是否发出该属性。

在这里演示小提琴#3。

最新更新