反序列化XML字符串时出现问题
我收到以下xml字符串(我已经删除了实际的名称空间名称) 引发错误“根元素丢失” 谁能帮我一下吗,我有点不知所措 我假定根元素为或 元素 不管怎样,我希望我已经说清楚了 短暂性脑缺血发作反序列化XML字符串时出现问题,xml,vb.net,deserialization,Xml,Vb.net,Deserialization,我收到以下xml字符串(我已经删除了实际的名称空间名称) 引发错误“根元素丢失” 谁能帮我一下吗,我有点不知所措 我假定根元素为或 元素 不管怎样,我希望我已经说清楚了 短暂性脑缺血发作 JaB它在文件的开头有额外的字符“-” <?xml version="1.0" encoding="UTF-8"?> <changeNotificationDto xmlns="http://www.Company.com/item" changeEventTypeCode="RemoveDe
JaB它在文件的开头有额外的字符“-”
<?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>
交货点
1139
对象删除
1139
交货点
2324
外部参照
对象删除
1139
交货点
以上的xml格式。当您在IE中打开xml并将其粘贴到记事本中时,就会发生这种情况。它带有额外的“-”字符。您好,谢谢您的回复。。。。xml文件没有隐藏字符或我可以看到/查找/显示的“额外”字符。上面xml中的“-”是我创建此主题时添加的。实际文本字符串中没有换行符或回车符。我将删除标记之间的空白。我还缺少其他类型的角色吗?
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()
<?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>