如何反序列化具有多个命名空间的 xml?
我正在从 youtube 中提取 xml:https://gdata.youtube.com/feeds/api/users/OnlyChillstep?v=2
我可以获取
这是类(class),我也尝试将 ElementName:="yt:location"放入,但没有成功
<Xml.Serialization.XmlRoot(elementname:="entry", namespace:="http://www.w3.org/2005/Atom")> _
Public Class YoutubeFeed
<XmlElement(ElementName:="title")> _
Public title As String
<XmlElement(ElementName:="location")> _
Public location As String
End Class
Dim requestUri2 As String = "https://gdata.youtube.com/feeds/api/users/OnlyChillstep?v=2"
Dim request2 As HttpWebRequest = DirectCast(WebRequest.Create(requestUri2), HttpWebRequest)
Dim resultSet2 As YoutubeFeed
Using response2 As WebResponse = request2.GetResponse()
Using responseStream As Stream = response2.GetResponseStream()
Dim serializer As New XmlSerializer(GetType(YoutubeFeed))
resultSet2 = DirectCast(serializer.Deserialize(responseStream), YoutubeFeed)
End Using
End Using
Console.WriteLine(resultSet2.title)
Console.WriteLine(resultSet2.location)
最佳答案
“yt:location”意味着您必须在元素定义中包含以“yt”为别名的命名空间。你有没有尝试过这样的事情?
<XmlElement(elementname="location" namespace="[yt's URI <- look in the xml for a xmlns:yt=blah]")>
更新
从链接中,您可以处理这些命名空间:
xmlns='http://www.w3.org/2005/Atom'
xmlns:media='http://search.yahoo.com/mrss/'
xmlns:gd='http://schemas.google.com/g/2005'
xmlns:yt='http://gdata.youtube.com/schemas/2007'
关于xml - 使用多个命名空间反序列化 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14146878/