本文整理汇总了VB.NET中System.Xml.Serialization.XmlSerializer.XmlSerializer构造函数的典型用法代码示例。如果您正苦于以下问题:VB.NET XmlSerializer构造函数的具体用法?VB.NET XmlSerializer怎么用?VB.NET XmlSerializer使用的例子?那么, 这里精选的构造函数代码示例或许可以为您提供帮助。您也可以进一步了解该构造函数所在类System.Xml.Serialization.XmlSerializer
的用法示例。
在下文中一共展示了XmlSerializer构造函数的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的VB.NET代码示例。
示例1: SerializeObject
Private Sub SerializeObject(ByVal filename As String)
Dim serializer As New XmlSerializer(GetType(OrderedItem))
' Create an instance of the class to be serialized.
Dim i As New OrderedItem()
' Set the public property values.
With i
.ItemName = "Widget"
.Description = "Regular Widget"
.Quantity = 10
.UnitPrice = CDec(2.3)
End With
' Writing the document requires a TextWriter.
Dim writer As New StreamWriter(filename)
' Serialize the object, and close the TextWriter.
serializer.Serialize(writer, i)
writer.Close()
End Sub
' This is the class that will be serialized.
Public Class OrderedItem
Public ItemName As String
Public Description As String
Public UnitPrice As Decimal
Public Quantity As Integer
End Class
示例2: Group
' 导入命名空间
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization
Imports System.Xml.Schema
Public Class Group
<SoapAttribute (Namespace:= "http:'www.cpandl.com")> _
Public GroupName As String
<SoapAttribute(DataType:= "base64Binary")> _
Public GroupNumber() As Byte
<SoapAttribute(DataType:= "date", _
AttributeName:= "CreationDate")> _
Public Today As DateTime
<SoapElement(DataType:= "nonNegativeInteger", _
ElementName:= "PosInt")> _
Public PostitiveInt As String
' This is ignored when serialized unless it's overridden.
<SoapIgnore> _
Public IgnoreThis As Boolean
Public Grouptype As GroupType
Public MyVehicle As Vehicle
' The SoapInclude allows the method to return a Car.
<SoapInclude(GetType(Car))> _
Public Function myCar(licNumber As String ) As Vehicle
Dim v As Vehicle
if licNumber = "" Then
v = New Car()
v.licenseNumber = "!!!!!!"
else
v = New Car()
v.licenseNumber = licNumber
End If
return v
End Function
End Class
' SoapInclude allows Vehicle to accept Car type.
<SoapInclude(GetType(Car))> _
Public MustInherit class Vehicle
Public licenseNumber As String
Public makeDate As DateTime
End Class
Public Class Car
Inherits Vehicle
End Class
Public enum GroupType
' These enums can be overridden.
<SoapEnum("Small")> _
A
<SoapEnum("Large")> _
B
End Enum
Public Class Run
Shared Sub Main()
Dim test As Run = New Run()
test.SerializeOriginal("SoapOriginal.xml")
test.SerializeOverride("SoapOverrides.xml")
test.DeserializeOriginal("SoapOriginal.xml")
test.DeserializeOverride("SoapOverrides.xml")
End SUb
Public Sub SerializeOriginal(filename As String)
' Create an instance of the XmlSerializer class.
Dim myMapping As XmlTypeMapping = _
(New SoapReflectionImporter().ImportTypeMapping _
(GetType(Group)))
Dim mySerializer As XmlSerializer = _
New XmlSerializer(myMapping)
Dim myGroup As Group =MakeGroup()
' Writing the file requires a TextWriter.
Dim writer As XmlTextWriter = _
New XmlTextWriter(filename, Encoding.UTF8)
writer.Formatting = Formatting.Indented
writer.WriteStartElement("wrapper")
' Serialize the class, and close the TextWriter.
mySerializer.Serialize(writer, myGroup)
writer.WriteEndElement()
writer.Close()
End Sub
Public Sub SerializeOverride(filename As String)
' Create an instance of the XmlSerializer class
' that overrides the serialization.
Dim overRideSerializer As XmlSerializer = _
CreateOverrideSerializer()
Dim myGroup As Group =MakeGroup()
' Writing the file requires a TextWriter.
Dim writer As XmlTextWriter = _
New XmlTextWriter(filename, Encoding.UTF8)
writer.Formatting = Formatting.Indented
writer.WriteStartElement("wrapper")
' Serialize the class, and close the TextWriter.
overRideSerializer.Serialize(writer, myGroup)
writer.WriteEndElement()
writer.Close()
End Sub
private Function MakeGroup() As Group
' Create an instance of the class that will be serialized.
Dim myGroup As Group = New Group()
' Set the object properties.
myGroup.GroupName = ".NET"
Dim hexByte()As Byte = new Byte(1){Convert.ToByte(100), _
Convert.ToByte(50)}
myGroup.GroupNumber = hexByte
Dim myDate As DateTime = new DateTime(2002,5,2)
myGroup.Today = myDate
myGroup.PostitiveInt = "10000"
myGroup.IgnoreThis = true
myGroup.Grouptype = GroupType.B
Dim thisCar As Car
thisCar =CType(myGroup.myCar("1234566"), Car)
myGroup.myVehicle=thisCar
return myGroup
End Function
Public Sub DeserializeOriginal(filename As String)
' Create an instance of the XmlSerializer class.
Dim myMapping As XmlTypeMapping = _
(New SoapReflectionImporter().ImportTypeMapping _
(GetType(Group)))
Dim mySerializer As XmlSerializer = _
New XmlSerializer(myMapping)
' Reading the file requires an XmlTextReader.
Dim reader As XmlTextReader = _
New XmlTextReader(filename)
reader.ReadStartElement("wrapper")
' Deserialize and cast the object.
Dim myGroup As Group = _
CType(mySerializer.Deserialize(reader), Group)
reader.ReadEndElement()
reader.Close()
End Sub
Public Sub DeserializeOverride(filename As String)
' Create an instance of the XmlSerializer class.
Dim overRideSerializer As XmlSerializer = _
CreateOverrideSerializer()
' Reading the file requires an XmlTextReader.
Dim reader As XmlTextReader = _
New XmlTextReader(filename)
reader.ReadStartElement("wrapper")
' Deserialize and cast the object.
Dim myGroup As Group = _
CType(overRideSerializer.Deserialize(reader), Group)
reader.ReadEndElement()
reader.Close()
ReadGroup(myGroup)
End Sub
private Sub ReadGroup(myGroup As Group)
Console.WriteLine(myGroup.GroupName)
Console.WriteLine(myGroup.GroupNumber(0))
Console.WriteLine(myGroup.GroupNumber(1))
Console.WriteLine(myGroup.Today)
Console.WriteLine(myGroup.PostitiveInt)
Console.WriteLine(myGroup.IgnoreThis)
Console.WriteLine()
End Sub
Private Function CreateOverrideSerializer() As XmlSerializer
Dim soapOver As SoapAttributeOverrides = New SoapAttributeOverrides()
Dim soapAtts As SoapAttributes = New SoapAttributes()
Dim mySoapElement As SoapElementAttribute = New SoapElementAttribute()
mySoapElement.ElementName = "xxxx"
soapAtts.SoapElement = mySoapElement
soapOver.Add(GetType(Group), "PostitiveInt", soapAtts)
' Override the IgnoreThis property.
Dim myIgnore As SoapIgnoreAttribute = new SoapIgnoreAttribute()
soapAtts = New SoapAttributes()
soapAtts.SoapIgnore = false
soapOver.Add(GetType(Group), "IgnoreThis", soapAtts)
' Override the GroupType enumeration.
soapAtts = New SoapAttributes()
Dim xSoapEnum As SoapEnumAttribute = new SoapEnumAttribute()
xSoapEnum.Name = "Over1000"
soapAtts.SoapEnum = xSoapEnum
' Add the SoapAttributes to the SoapOverrides object.
soapOver.Add(GetType(GroupType), "A", soapAtts)
' Create second enumeration and add it.
soapAtts = New SoapAttributes()
xSoapEnum = New SoapEnumAttribute()
xSoapEnum.Name = "ZeroTo1000"
soapAtts.SoapEnum = xSoapEnum
soapOver.Add(GetType(GroupType), "B", soapAtts)
' Override the Group type.
soapAtts = New SoapAttributes()
Dim soapType As SoapTypeAttribute = New SoapTypeAttribute()
soapType.TypeName = "Team"
soapAtts.SoapType = soapType
soapOver.Add(GetType(Group),soapAtts)
Dim myMapping As XmlTypeMapping = (New SoapReflectionImporter( _
soapOver)).ImportTypeMapping(GetType(Group))
Dim ser As XmlSerializer = new XmlSerializer(myMapping)
return ser
End Function
End Class
示例3: SerializeObject
Private Sub SerializeObject(ByVal filename As String)
Dim serializer As New XmlSerializer(GetType(OrderedItem), _
"http://www.cpandl.com")
' Create an instance of the class to be serialized.
Dim i As New OrderedItem()
' Insert code to set property values.
' Writing the document requires a TextWriter.
Dim writer As New StreamWriter(filename)
' Serialize the object, and close the TextWriter.
serializer.Serialize(writer, i)
writer.Close()
End Sub
Private Sub DeserializeObject(ByVal filename As String)
Dim serializer As New XmlSerializer(GetType(OrderedItem), _
"http://www.cpandl.com")
' A FileStream is needed to read the XML document.
Dim fs As New FileStream(filename, FileMode.Open)
' Declare an object variable of the type to be deserialized.
Dim i As OrderedItem
' Deserialize the object.
i = CType(serializer.Deserialize(fs), OrderedItem)
' Insert code to use the properties and methods of the object.
End Sub
示例4: Teacher
' 导入命名空间
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
' This defines the object that will be serialized.
Public Class Teacher
Public Name As String
Public Sub New()
End Sub
' Note that the Info field returns an array of objects.
' Any object can be added to the array by adding the
' object type to the array passed to the extraTypes argument.
<XmlArray(ElementName := "ExtraInfo", IsNullable := True)> _
Public Info() As Object
Public PhoneInfo As Phone
End Class
' This defines one of the extra types to be included.
Public Class Address
Public City As String
Public Sub New()
End Sub
Public Sub New(city As String)
me.City = city
End Sub
End Class
' Another extra type to include.
Public Class Phone
Public PhoneNumber As String
Public Sub New()
End Sub
Public Sub New(phoneNumber As String)
me.PhoneNumber = phoneNumber
End Sub
End Class
' Another type, derived from Phone.
Public Class InternationalPhone
Inherits Phone
Public CountryCode As String
Public Sub New()
End Sub
Public Sub New(countryCode As String)
me.CountryCode = countryCode
End Sub
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeObject("Teacher.xml")
test.DeserializeObject("Teacher.xml")
End Sub
Private Sub SerializeObject(filename As String)
' Writing the file requires a TextWriter.
Dim myStreamWriter As New StreamWriter(filename)
' Create a Type array.
Dim extraTypes(2) As Type
extraTypes(0) = GetType(Address)
extraTypes(1) = GetType(Phone)
extraTypes(2) = GetType(InternationalPhone)
' Create the XmlSerializer instance.
Dim mySerializer As New XmlSerializer(GetType(Teacher), extraTypes)
Dim teacher As New Teacher()
teacher.Name = "Mike"
' Add extra types to the Teacher object.
Dim info(1) As Object
info(0) = New Address("Springville")
info(1) = New Phone("555-0100")
teacher.Info = info
teacher.PhoneInfo = New InternationalPhone("000")
mySerializer.Serialize(myStreamWriter, teacher)
myStreamWriter.Close()
End Sub
Private Sub DeserializeObject(filename As String)
' Create a Type array.
Dim extraTypes(2) As Type
extraTypes(0) = GetType(Address)
extraTypes(1) = GetType(Phone)
extraTypes(2) = GetType(InternationalPhone)
' Create the XmlSerializer instance.
Dim mySerializer As New XmlSerializer(GetType(Teacher), extraTypes)
' Reading a file requires a FileStream.
Dim fs As New FileStream(filename, FileMode.Open)
Dim teacher As Teacher = CType(mySerializer.Deserialize(fs), Teacher)
' Read the extra information.
Dim a As Address = CType(teacher.Info(0), Address)
Dim p As Phone = CType(teacher.Info(1), Phone)
Dim Ip As InternationalPhone = CType(teacher.PhoneInfo, InternationalPhone)
Console.WriteLine(teacher.Name)
Console.WriteLine(a.City)
Console.WriteLine(p.PhoneNumber)
Console.WriteLine(Ip.CountryCode)
End Sub
End Class
示例5: New
' Beginning of HighSchool.dll
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Imports HighSchool
Namespace HighSchool
Public Class Student
Public Name As String
Public ID As Integer
End Class
Public Class MyClass1
Public Students() As Student
End Class
End Namespace 'HighSchool
Namespace College
Public Class Graduate
Inherits HighSchool.Student
Public Sub New()
End Sub
Public University As String
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.WriteOverriddenAttributes("College.xml")
test.ReadOverriddenAttributes("College.xml")
End Sub
Private Sub WriteOverriddenAttributes(filename As String)
' Writing the file requires a TextWriter.
Dim myStreamWriter As New StreamWriter(filename)
' Create an XMLAttributeOverrides class.
Dim attrOverrides As New XmlAttributeOverrides()
' Create the XmlAttributes class.
Dim attrs As New XmlAttributes()
' Override the Student class. "Alumni" is the name
' of the overriding element in the XML output.
Dim attr As New XmlElementAttribute("Alumni", GetType(Graduate))
' Add the XmlElementAttribute to the collection of
' elements in the XmlAttributes object.
attrs.XmlElements.Add(attr)
' Add the XmlAttributes to the XmlAttributeOverrides.
' "Students" is the name being overridden.
attrOverrides.Add(GetType(HighSchool.MyClass1), "Students", attrs)
' Create the XmlSerializer.
Dim mySerializer As New XmlSerializer(GetType(HighSchool.MyClass1), attrOverrides)
Dim oMyClass As New MyClass1()
Dim g1 As New Graduate()
g1.Name = "Jackie"
g1.ID = 1
g1.University = "Alma Mater"
Dim g2 As New Graduate()
g2.Name = "Megan"
g2.ID = 2
g2.University = "CM"
Dim myArray As Student() = {g1, g2}
oMyClass.Students = myArray
mySerializer.Serialize(myStreamWriter, oMyClass)
myStreamWriter.Close()
End Sub
Private Sub ReadOverriddenAttributes(filename As String)
' The majority of the code here is the same as that in the
' WriteOverriddenAttributes method. Because the XML being read
' doesn't conform to the schema defined by the DLL, the
' XMLAttributesOverrides must be used to create an
' XmlSerializer instance to read the XML document.
Dim attrOverrides As New XmlAttributeOverrides()
Dim attrs As New XmlAttributes()
Dim attr As New XmlElementAttribute("Alumni", GetType(Graduate))
attrs.XmlElements.Add(attr)
attrOverrides.Add(GetType(HighSchool.MyClass1), "Students", attrs)
Dim readSerializer As New XmlSerializer(GetType(HighSchool.MyClass1), attrOverrides)
' To read the file, a FileStream object is required.
Dim fs As New FileStream(filename, FileMode.Open)
Dim oMyClass As MyClass1
oMyClass = CType(readSerializer.Deserialize(fs), MyClass1)
' Here is the difference between reading and writing an
' XML document: You must declare an object of the derived
' type (Graduate) and cast the Student instance to it.
Dim g As Graduate
Dim grad As Graduate
For Each grad In oMyClass.Students
g = CType(grad, Graduate)
Console.Write((g.Name & ControlChars.Tab))
Console.Write((g.ID.ToString & ControlChars.Tab))
Console.Write((g.University & ControlChars.Cr))
Next grad
End Sub
End Class
End Namespace 'College
示例6: SerializeObject
Private Sub SerializeObject(ByVal filename As String)
' Create an XmlRootAttribute, and set its properties.
Dim xRoot As New XmlRootAttribute()
xRoot.ElementName = "CustomRoot"
xRoot.Namespace = "http://www.cpandl.com"
xRoot.IsNullable = True
' Construct the XmlSerializer with the XmlRootAttribute.
Dim serializer As New XmlSerializer(GetType(OrderedItem), xRoot)
' Create an instance of the object to serialize.
Dim i As New OrderedItem()
' Insert code to set properties of the ordered item.
' Writing the document requires a TextWriter.
Dim writer As New StreamWriter(filename)
serializer.Serialize(writer, i)
writer.Close()
End Sub
Private Sub DeserializeObject(ByVal filename As String)
' Create an XmlRootAttribute, and set its properties.
Dim xRoot As New XmlRootAttribute()
xRoot.ElementName = "CustomRoot"
xRoot.Namespace = "http://www.cpandl.com"
xRoot.IsNullable = True
Dim serializer As New XmlSerializer(GetType(OrderedItem), xRoot)
' A FileStream is needed to read the XML document.
Dim fs As New FileStream(filename, FileMode.Open)
' Deserialize the object.
Dim i As OrderedItem = CType(serializer.Deserialize(fs), OrderedItem)
' Insert code to use the object's properties and methods.
End Sub
示例7: New
'Beginning of the HighSchool.dll
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
' Imports HighSchool
Namespace HighSchool
Public Class Student
Public Name As String
Public ID As Integer
End Class
Public Class ClassRoom
Public Students() As Student
End Class
End Namespace 'HighSchool
Namespace College
Public Class Graduate
Inherits HighSchool.Student
Public Sub New()
End Sub
' Add a new field named University.
Public University As String
' Use extra types to use this field.
Public Info() As Object
End Class
Public Class Address
Public City As String
End Class
Public Class Phone
Public Number As String
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.WriteOverriddenAttributes("College.xml")
test.ReadOverriddenAttributes("College.xml")
End Sub
Private Sub WriteOverriddenAttributes(ByVal filename As String)
' Writing the file requires a TextWriter.
Dim myStreamWriter As New StreamWriter(filename)
' Create an XMLAttributeOverrides class.
Dim attrOverrides As New XmlAttributeOverrides()
' Create the XmlAttributes class.
Dim attrs As New XmlAttributes()
' Override the Student class. "Alumni" is the name
' of the overriding element in the XML output.
Dim attr As New XmlElementAttribute("Alumni", GetType(Graduate))
' Add the XmlElementAttribute to the collection of
' elements in the XmlAttributes object.
attrs.XmlElements.Add(attr)
' Add the XmlAttributes to the XmlAttributeOverrides.
' "Students" is the name being overridden.
attrOverrides.Add(GetType(HighSchool.ClassRoom), "Students", attrs)
' Create array of extra types.
Dim extraTypes(1) As Type
extraTypes(0) = GetType(Address)
extraTypes(1) = GetType(Phone)
' Create an XmlRootAttribute.
Dim root As New XmlRootAttribute("Graduates")
' Create the XmlSerializer with the
' XmlAttributeOverrides object.
Dim mySerializer As New XmlSerializer(GetType(HighSchool.ClassRoom), _
attrOverrides, extraTypes, root, "http://www.microsoft.com")
Dim oMyClass As New HighSchool.ClassRoom()
Dim g1 As New Graduate()
g1.Name = "Jacki"
g1.ID = 1
g1.University = "Alma"
Dim g2 As New Graduate()
g2.Name = "Megan"
g2.ID = 2
g2.University = "CM"
Dim myArray As HighSchool.Student() = {g1, g2}
oMyClass.Students = myArray
' Create extra information.
Dim a1 As New Address()
a1.City = "Ionia"
Dim a2 As New Address()
a2.City = "Stamford"
Dim p1 As New Phone()
p1.Number = "555-0101"
Dim p2 As New Phone()
p2.Number = "555-0100"
Dim o1() As Object = {a1, p1}
Dim o2() As Object = {a2, p2}
g1.Info = o1
g2.Info = o2
mySerializer.Serialize(myStreamWriter, oMyClass)
myStreamWriter.Close()
End Sub
Private Sub ReadOverriddenAttributes(ByVal filename As String)
' The majority of the code here is the same as that in the
' WriteOverriddenAttributes method. Because the XML being read
' doesn't conform to the schema defined by the DLL, the
' XMLAttributesOverrides must be used to create an
' XmlSerializer instance to read the XML document.
Dim attrOverrides As New XmlAttributeOverrides()
Dim attrs As New XmlAttributes()
Dim attr As New XmlElementAttribute("Alumni", GetType(Graduate))
attrs.XmlElements.Add(attr)
attrOverrides.Add(GetType(HighSchool.ClassRoom), "Students", attrs)
Dim extraTypes(1) As Type
extraTypes(0) = GetType(Address)
extraTypes(1) = GetType(Phone)
Dim root As New XmlRootAttribute("Graduates")
Dim readSerializer As New XmlSerializer(GetType(HighSchool.ClassRoom), _
attrOverrides, extraTypes, root, "http://www.microsoft.com")
' A FileStream object is required to read the file.
Dim fs As New FileStream(filename, FileMode.Open)
Dim oMyClass As HighSchool.ClassRoom
oMyClass = CType(readSerializer.Deserialize(fs), HighSchool.ClassRoom)
' Here is the difference between reading and writing an
' XML document: You must declare an object of the derived
' type (Graduate) and cast the Student instance to it.
Dim g As Graduate
Dim a As Address
Dim p As Phone
Dim grad As Graduate
For Each grad In oMyClass.Students
g = CType(grad, Graduate)
Console.Write((g.Name & ControlChars.Tab))
Console.Write((g.ID & ControlChars.Tab))
Console.Write((g.University & ControlChars.Cr))
a = CType(g.Info(0), Address)
Console.WriteLine(a.City)
p = CType(g.Info(1), Phone)
Console.WriteLine(p.Number)
Next grad
End Sub
End Class
End Namespace 'College