[C#][VB.NET]XML序列化私有欄位
#Region “Enum” Enum SexType Boy Girl End Enum #End Region
#Region “Var” Private _name As String Private _year As Integer Private _sex As SexType Private _friendNames As New List(Of String) #End Region
#Region “Property” Public Property Name() As String Get If String.IsNullOrEmpty(_name) Then Return String.Empty End If Return _name End Get Set(ByVal value As String) _name = value End Set End Property
Public Property Year() As Integer
Get
Return _year
End Get
Set(ByVal value As Integer)
_year = value
End Set
End Property
Public Property Sex() As SexType
Get
Return _sex
End Get
Set(ByVal value As SexType)
_sex = value
End Set
End Property
#End Region
#Region “Public Method” Public Sub AddFriend(ByVal people As Person) _friendNames.Add(people.Name) End Sub
Public Function IsFriend(ByVal people As Person) As Boolean
For Each friendName As String In _friendNames
If friendName = people.Name Then
Return True
End If
Next
Return False
End Function
#End Region
End Class
#region Enum
public enum SexType
{
Boy,
Girl
}
#endregion
#region Var
private string _name;
private int _year;
private SexType _sex;
private List<String> _friendNames = new List<String>();
#endregion
#region Property
public string Name
{
get
{
if (string.IsNullOrEmpty(_name))
{
return string.Empty;
}
return _name;
}
set { _name = value; }
}
public int Year
{
get { return _year; }
set { _year = value; }
}
public SexType Sex
{
get { return _sex; }
set { _sex = value; }
}
#endregion
#region Public Method
public void AddFriend(Person people)
{
_friendNames.Add(people.Name);
}
public bool IsFriend(Person people)
{
foreach (String friendName in _friendNames)
{
if (friendName == people.Name)
{
return true;
}
}
return false;
}
#endregion
}</pre>
Public Sub ReadXml(ByVal reader As System.Xml.XmlReader) Implements System.Xml.Serialization.IXmlSerializable.ReadXml
Dim startElementName As String = reader.Name
Dim currentElementName As String
Me._friendNames.Clear()
Do
currentElementName = reader.Name
If currentElementName = startElementName AndAlso (reader.MoveToContent = Xml.XmlNodeType.EndElement OrElse reader.IsEmptyElement) Then
reader.Read()
Exit Do
End If
Select Case currentElementName
Case "Name"
Me.Name = reader.ReadElementString()
Case "Sex"
Me.Sex = CType(reader.ReadElementString(), SexType)
Case "Year"
Me.Year = CInt(reader.ReadElementString())
Case "Friend"
Me._friendNames.Add(reader.GetAttribute("Name"))
reader.Read()
Case Else
reader.Read()
End Select
Loop
End Sub
Public Sub WriteXml(ByVal writer As System.Xml.XmlWriter) Implements System.Xml.Serialization.IXmlSerializable.WriteXml
writer.WriteElementString("Name", Name)
writer.WriteElementString("Sex", CInt(Sex).ToString)
writer.WriteElementString("Year", Year.ToString)
For Each friendName As String In _friendNames
writer.WriteStartElement("Friend")
writer.WriteAttributeString("Name", friendName)
writer.WriteEndElement()
Next
End Sub
#End Region
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(System.Xml.XmlReader reader)
{
string startElementName = reader.Name;
string currentElementName;
this._friendNames.Clear();
do
{
currentElementName = reader.Name;
if (currentElementName == startElementName && (reader.MoveToContent() == System.Xml.XmlNodeType.EndElement || reader.IsEmptyElement))
{
reader.Read();
break;
}
switch (currentElementName)
{
case "Name":
this.Name = reader.ReadElementString();
break;
case "Sex":
this.Sex = (SexType)int.Parse (reader.ReadElementString());
break;
case "Year":
this.Year = int.Parse(reader.ReadElementString());
break;
case "Friend":
this._friendNames.Add(reader.GetAttribute("Name"));
reader.Read();
break;
default:
reader.Read();
break;
}
}while (true);
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteElementString("Name", Name);
writer.WriteElementString("Sex", ((int)Sex).ToString());
writer.WriteElementString("Year", Year.ToString());
foreach (string friendName in _friendNames)
{
writer.WriteStartElement("Friend");
writer.WriteAttributeString("Name", friendName);
writer.WriteEndElement();
}
}
#endregion</pre>