1

如何反序列化具有多个命名空间的 xml?

我正在从 youtube 中提取 xml:https ://gdata.youtube.com/feeds/api/users/OnlyChillstep?v=2

我可以得到 <title>、<summary>,但不能得到 <yt:location>

这是课程,我也尝试放置 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)
4

1 回答 1

2

“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'  
于 2013-01-03T21:19:54.010 回答