Problem deserializing XML string
I'm receiving the following xml string (i've removed the actual namespace name)
<?xml version="1.0" encoding="UTF-8"?>
- <changeNotificationDto xmlns="http://www.Company.com/item" changeEventTypeCode="RemoveDeliveryPoint" messageId="10" sentDateTime="1970-01-01T00:00:00.000Z">
- <entry action="Remove" type="DeliveryPoint">
- <removeEntityNotification>
<type>DeliveryPoint</type>
<compassKey>1139</compassKey>
</removeEntityNotification>
- <changeDelta>
<changeDeltaType>ObjectRemove</changeDeltaType>
<recordId>1139<开发者_运维知识库;/recordId>
<recordType>DeliveryPoint</recordType>
<subRecordId>2324</subRecordId>
<subRecordType>ExternalReference</subRecordType>
</changeDelta>
- <changeDelta>
<changeDeltaType>ObjectRemove</changeDeltaType>
<recordId>1139</recordId>
<recordType>DeliveryPoint</recordType>
</changeDelta>
</entry>
</changeNotificationDto>
I have a web reference to the "changeNotificationDto" and this has generated a proxy class (I have no problem creating / using objects of this type within my application) [The actual contents of the type changes but I have proxy classes from web references for all objects I'm going to recieve. For the purposes of testing I have been using a streamReader to extract the xml from a file but the file was written from the original message as receieved via a socket]
I have a small test harness as follows :- ( VB.NEt / VS2010 / FrmWrk 4 )
Dim objStreamReader As New StreamReader("C:\Testing\101 VB.NET Samples\SerializeandDeserializeXML\rdp.xml")
Dim p2 As ChangeEventManagerService.ChangeNotificationDto
Dim output As StringBuilder = New StringBuilder()
Using xmlrdr As XmlReader = XmlReader.Create(New StringReader(objStreamReader.ReadToEnd()))
Dim ws As XmlWriterSettings = New XmlWriterSettings()
ws.Indent = True
Using writer As XmlWriter = XmlWriter.Create(output, ws)
' Parse the file and display each of the nodes.
While xmlrdr.Read()
Select Case xmlrdr.NodeType
Case XmlNodeType.Element
writer.WriteStartElement(xmlrdr.Name)
Case XmlNodeType.Text
writer.WriteString(xmlrdr.Value)
Case XmlNodeType.XmlDeclaration
Case XmlNodeType.ProcessingInstruction
writer.WriteProcessingInstruction(xmlrdr.Name, xmlrdr.Value)
Case XmlNodeType.Comment
writer.WriteComment(xmlrdr.Value)
Case XmlNodeType.EndElement
writer.WriteFullEndElement()
End Select
End While
End Using
Console.Write(output.ToString()) '# up to this point all is good
If x.CanDeserialize(xmlrdr) Then '# this fails
p2 = CType(x.Deserialize(objStreamReader), ChangeEventManagerService.ChangeNotificationDto) '# forcing it to here throws an error as expected!
objStreamReader.Close()
End If
End Using '# reader
''Display property values of the new product object.
Console.WriteLine(p2.changeEventTypeCode)
Console.WriteLine(p2.messageId)
Catch ex As Exception
Console.WriteLine(ex.InnerException.ToString())
Console.WriteLine(ex.ToString())
End Try
Console.ReadLine()
The error thrown "Root element is missing".
Can anyone help me with this, I'm at a bit of a loss.
I had presumed the root element to be either or the element.
Anyway, I hope I've made that all clear...
TIA
JaB
It has extra char '-' in the beginning of the file
<?xml version="1.0" encoding="UTF-8"?>
<changeNotificationDto xmlns="http://www.Company.com/item" changeEventTypeCode="RemoveDeliveryPoint" messageId="10" sentDateTime="1970-01-01T00:00:00.000Z">
<entry action="Remove" type="DeliveryPoint">
<removeEntityNotification>
<type>DeliveryPoint</type>
<compassKey>1139</compassKey>
</removeEntityNotification>
<changeDelta>
<changeDeltaType>ObjectRemove</changeDeltaType>
<recordId>1139</recordId>
<recordType>DeliveryPoint</recordType>
<subRecordId>2324</subRecordId>
<subRecordType>ExternalReference</subRecordType>
</changeDelta>
<changeDelta>
<changeDeltaType>ObjectRemove</changeDeltaType>
<recordId>1139</recordId>
<recordType>DeliveryPoint</recordType>
</changeDelta>
</entry>
</changeNotificationDto>
The the above xml. It happens when you open the xml in IE and paste it in notepad. It carries the extra '-' characters.
精彩评论