XML反串行化Namespace不统一而引起的错误

在反串行化 XML 的时候,如果 Namespace 不统一,就会引起错误。

比如需要反串行化这个 XML :

< Test xmlns:xsd =" http://www.w3.org/2001/XMLSchema " xmlns:xsi =" http://www.w3.org/2001/XMLSchema-instance " xmlns =" http://localhost/webservices ">
< A > 10 < B > just test 在程序里需要先声明一个 Class :

Public class Test
Public A As Integer
Public B As String
End class

然后:

Dim serializer As New XmlSerializer( GetType (Root))
t = CType (serializer.Deserialize(s), Root)

其中 t 是 Test 的实例,而 s 是 XML 的 Stream。

正常的反串行化应该是这个过程的,但是会得到一个错误。因为 serializer 不知道 XML 的 Namespace,所以需要指定 XML 的 Namespace,方法就是:

1<xmlroot( )="" :="  http://localhost/webservices  " namespace=""> _    
2Public class  Test   
3Public  A  As Integer    
4Public  B  As String    
5End class 
6
7这样,XML 的 Namespace 就统一了,也就可以正常的反串行化了。</xmlroot(>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus