我收到以下xml字符串(我删除了实际的命名空间名称)
<?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>
我有一个对“changeNotificationDto”的web引用,这已经生成了一个代理类(在我的应用程序中创建/使用这种类型的对象没有问题)[类型的实际内容发生了变化,但我有来自的代理类我要接收的所有对象的Web引用。 出于测试的目的,我一直在使用streamReader从文件中提取xml,但文件是从原始消息中写入的,通过套接字接收]
我有一个小型测试工具如下: - (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()
抛出错误“Root元素丢失”。
任何人都可以帮助我,我有点失落。
我假设根元素是或者 元素。
无论如何,我希望我已经明白了......
TIA
戳
答案 0 :(得分:0)
在文件开头
中有额外的字符' - '<?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>
上面的xml。当你在IE中打开xml并将其粘贴到记事本中时会发生这种情况。它带有额外的' - '字符。