該賞金過期5天。這個問題的答案有資格獲得 50聲望獎勵。 djv希望更多地關注這個問題。
我將 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 類中保留該架構路徑,以便序列化檔案包含它?
uj5u.com熱心網友回復:
你可以反序列化和重新序列的xsi:schemaLocation加入明確的財產為目的的屬性,如在此答案由DTB向XmlSerialization和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
演示小提琴 #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但控制它是否出現(因為例如它應該只出現在XmlStationSetpointsXML 檔案的根元素時),你可以使用這樣的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在這里。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/314912.html
下一篇:如何從單獨的串列框中系結資料?
