【问题标题】:How can I access to this XML field?如何访问此 XML 字段?
【发布时间】:2013-08-25 07:29:09
【问题描述】:

我想从this XML 中提取字段yt:username

var xDoc = XDocument.Load(requestedURL);
var m_oListaMeteo = xDoc.Descendants(ns + "entry").Select(n =>
{
    return new
    {
        username = n.Element(ns + "yt:username").Value
    };
});

XDocument 本身说The ':' character, hexadecimal value 0x3A, cannot be included in a name.

需要字符串替换吗?还是需要我管理 youtube 的命名空间?

【问题讨论】:

    标签: c# .net namespaces linq-to-xml deserialization


    【解决方案1】:

    yt 是命名空间,试试这个:

    var xDoc = XDocument.Load(@"https://gdata.youtube.com/feeds/api/users/djfonplaz/subscriptions?v=2");
    var ns = XNamespace.Get("http://www.w3.org/2005/Atom");
    var yt = XNamespace.Get("http://gdata.youtube.com/schemas/2007");
    var m_oListaMeteo = xDoc.Descendants(ns + "entry").Select(n =>
    {
        return new
        {
            username = n.Element(yt + "username").Value
        };
    });
    

    【讨论】:

      【解决方案2】:

      确保您使用了正确的命名空间:

      var xDoc = XDocument.Load(requestedURL);
      var m_oListaMeteo = xDoc
          .Root
          .Elements("{http://www.w3.org/2005/Atom}entry")
          .Select(entry => new
          {
              username = entry.Element("{http://gdata.youtube.com/schemas/2007}username").Value
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-11-20
        • 1970-01-01
        • 1970-01-01
        • 2019-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多