xml - 使用多个命名空间反序列化 xml

标签 xml vb.net serialization namespaces

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

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

我可以获取、<summary>,但不能获取<yt:location><br><br>这是类(class),我也尝试将 ElementName:="yt:location"放入,但没有成功<br><pre><code><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) </code></pre></p> </div> <div style="margin-top: 20px;"> <p> <strong><font size="5">最佳答案</font></strong> </p> </div> <div id="answer"> <p>“yt:location”意味着您必须在元素定义中包含以“yt”为别名的命名空间。你有没有尝试过这样的事情?<br><pre><code><XmlElement(elementname="location" namespace="[yt's URI <- look in the xml for a xmlns:yt=blah]")> </code></pre><br> <strong>更新 </strong><br>从链接中,您可以处理这些命名空间:<br><pre><code>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' </code></pre></p> </div> <div style="margin-top: 20px; margin-bottom: 40px;"> <p style="font-size: 20px;">关于xml - 使用多个命名空间反序列化 xml,我们在Stack Overflow上找到一个类似的问题: <a href="https://stackoverflow.com/questions/14146878/" rel="noreferrer noopener nofollow" style="color: red;"> https://stackoverflow.com/questions/14146878/ </a> </p> </div> </div> <div class="text-center" style="margin-top: 40px;"> <h3 style="font-size: 18px;"> <span>上一篇:<a href="/article/7803455" title="php - 正确使用 php 命名空间">php - 正确使用 php 命名空间</a></span> </h3> <h3 style="font-size: 18px;"> <span>下一篇:<a href="/article/7803457" title="namespaces - 将页面 move 到新的命名空间">namespaces - 将页面 move 到新的命名空间</a></span> </h3> </div> </div> <div style="width: 100%; margin-top: 30px;"> <p> <font style="font-size: 20px;"><strong>相关文章:</strong></font> </p> <p style="margin: 0 auto; margin-top: 5px;"> <a href="/article/1012122" title="xml - 模式有效性错误 : Element No matching global declaration available for the validation root">xml - 模式有效性错误 : Element No matching global declaration available for the validation root</a> </p> <p style="margin: 0 auto; margin-top: 5px;"> <a href="/article/3093658" title="c# - 有没有更好的方法来编写以下 Linq 查询">c# - 有没有更好的方法来编写以下 Linq 查询</a> </p> <p style="margin: 0 auto; margin-top: 5px;"> <a href="/article/5004326" title="python - 如何使用 ElementMaker 向元素添加属性?">python - 如何使用 ElementMaker 向元素添加属性?</a> </p> <p style="margin: 0 auto; margin-top: 5px;"> <a href="/article/3653113" title="android - 在抽屉菜单上设置可见性">android - 在抽屉菜单上设置可见性</a> </p> <p style="margin: 0 auto; margin-top: 5px;"> <a href="/article/8145198" title="vb.net - 我应该使用进口吗?">vb.net - 我应该使用进口吗?</a> </p> <p style="margin: 0 auto; margin-top: 5px;"> <a href="/article/7132929" title=".net - WmiMonitorID - 将结果转换为 ASCII">.net - WmiMonitorID - 将结果转换为 ASCII</a> </p> <p style="margin: 0 auto; margin-top: 5px;"> <a href="/article/3034106" title="c# - ApplicationSettingsBase 为自定义集合写入空标签">c# - ApplicationSettingsBase 为自定义集合写入空标签</a> </p> <p style="margin: 0 auto; margin-top: 5px;"> <a href="/article/631760" title="android - 我可以序列化在 Canvas 上绘制的路径以在重新启动应用程序时重新绘制路径吗">android - 我可以序列化在 Canvas 上绘制的路径以在重新启动应用程序时重新绘制路径吗</a> </p> <p style="margin: 0 auto; margin-top: 5px;"> <a href="/article/8284736" title="asp.net - 将事件监听器分配给 ASP.NET 中动态创建的 HTML 按钮">asp.net - 将事件监听器分配给 ASP.NET 中动态创建的 HTML 按钮</a> </p> <p style="margin: 0 auto; margin-top: 5px;"> <a href="/article/2978038" title="c# - 在 C# 中使构造函数仅接受具有 [Serializable] 属性的对象">c# - 在 C# 中使构造函数仅接受具有 [Serializable] 属性的对象</a> </p> </div> </div> </div> <div class="footer text-center" style="float: left; width: 100%; margin-top: 40px;"> <p> <font color="gray">©2024 </font><a href="/"><font color="gray">IT工具网</font></a>  <a href="/article/7294988" rel="nofollow"><font color="gray">联系我们</font></a> </p> </div> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?d26f2298d3a7fe583e547d2101e22936"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> <script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-6220481272339728" crossorigin="anonymous"></script> <script type="text/javascript" src="https://cdn.staticfile.org/jquery/3.4.1/jquery.min.js"></script> <script type="text/javascript" src="https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script> <script type="text/javascript" src="/static/js/highlight-10.1.2.min.js"></script> <script>hljs.initHighlightingOnLoad();</script> <script type="text/javascript"> $("code").each(function(){ var codeHTML = $(this).prop("innerHTML"); var newCodeHTML = codeHTML.replace(new RegExp("</.*>", "g"), ""); newCodeHTML = newCodeHTML.replace(new RegExp("<", "g"), "<"); $(this).html(newCodeHTML); }) </script> </body> </html>