2016-02-08 2 views
2

Я использую следующий код, чтобы прочитать указанный XMLЧтение XML с помощью SelectSingleNode

<?xml version=\"1.0\" encoding=\"UTF-8\"?> 
<feed version=\"0.3\" xmlns=\"http://purl.org/atom/ns#\"> 
    <title>Gmail - Inbox for [email protected]</title> 
    <tagline>New messages in your Gmail Inbox</tagline> 
    <fullcount>1</fullcount> 
    <link rel=\"alternate\" href=\"https://mail.google.com/mail\" type=\"text/html\" /> 
    <modified>2016-02-07T12:11:21Z</modified> 
    <entry> 
     <title>Access for less secure apps has been turned on</title> 
     <summary>Access for less secure apps has been turned on Hi Buddy, You recently changed your security settings so</summary> 
     <link rel=\"alternate\" href=\"https://mail.google.com/[email protected]&amp;message_id=152bb8ccd28d824b&amp;view=conv&amp;extsrc=atom\" type=\"text/html\" /> 
     <modified>2016-02-07T11:45:12Z</modified> 
     <issued>2016-02-07T11:45:12Z</issued> 
     <id>tag:gmail.google.com,2004:1525516088640373323</id> 
     <author> 
      <name>Google</name> 
      <email>[email protected]</email> 
     </author> 
    </entry> 
</feed> 

Ниже код используется, и проблема в том, что я не получаю значения для title элемента с последней строки кода.

response = Encoding.UTF8.GetString(objclient.DownloadData("https://mail.google.com/mail/feed/atom")); 
response = response.Replace("<feed version*\"0.3\" xmlns=\"http://purl.org/atom/01#\">", "<feed>"); 

xdoc.LoadXml(response); 

var nsmgr = new XmlNamespaceManager(xdoc.NameTable); 
nsmgr.AddNamespace("feed", "http://purl.org/atom/ns#"); 
node = xdoc.SelectSingleNode("//feed:fullcount", nsmgr); 


Variables.emailcount = Convert.ToInt16(node.InnerText); 
System.Diagnostics.Debug.Write(Variables.emailcount); 

tagline = xdoc.SelectSingleNode("//feed:tagline", nsmgr).InnerText; 

node2 = xdoc.SelectSingleNode("//feed:entry", nsmgr); 

message_subject = node2.SelectSingleNode("//feed/entry/title", nsmg).InnerText; ---- >>> Issue Line 

Просто интересно, где может быть проблема.

Благодаря

+1

Я не понимаю, почему ты удаленное пространство имен, а затем ввести его? Во всяком случае, попробуйте // feed: entry/title. – Jules

ответ

2

попробовать

message_subject = node2.SelectSingleNode ("// подача: вход/подача: название", nsmgr) .InnerText;

примечание: вы использовали «корм», как имя пространства имен, поэтому название также следует квалифицировать

2

Сделайте свою жизнь проще с помощью SyndicationFeed определено в System.ServiceModel.Syndication имен из System.ServiceModel сборки.

SyndicationFeed syndicationFeed = null; 
using (var reader = XmlReader.Create("https://mail.google.com/mail/feed/atom")) 
{ 
    syndicationFeed = SyndicationFeed.Load(reader); 
} 

if(syndicationFeed != null) 
{ 
    foreach (SyndicationItem item in syndicationFeed.Items) 
    { 
     // Do everything you want here by checking properties you need from SyndicationItem item variable. 
    } 
} 

Чтобы узнать, какие свойства, которые SyndicationItem предложение проверить эту link.

2

Я предпочитаю XML LINQ

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const int NUMBER_OF_XML = 3; 
     static void Main(string[] args) 
     { 
      string xml = 
       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 
        "<feed version=\"0.3\" xmlns=\"http://purl.org/atom/ns#\">" + 
         "<title>Gmail - Inbox for [email protected]</title>" + 
         "<tagline>New messages in your Gmail Inbox</tagline>" + 
         "<fullcount>1</fullcount>" + 
         "<link rel=\"alternate\" href=\"https://mail.google.com/mail\" type=\"text/html\" />" + 
         "<modified>2016-02-07T12:11:21Z</modified>" + 
         "<entry>" + 
          "<title>Access for less secure apps has been turned on</title>" + 
          "<summary>Access for less secure apps has been turned on Hi Buddy, You recently changed your security settings so</summary>" + 
          "<link rel=\"alternate\" href=\"https://mail.google.com/[email protected]&amp;message_id=152bb8ccd28d824b&amp;view=conv&amp;extsrc=atom\" type=\"text/html\" />" + 
          "<modified>2016-02-07T11:45:12Z</modified>" + 
          "<issued>2016-02-07T11:45:12Z</issued>" + 
          "<id>tag:gmail.google.com,2004:1525516088640373323</id>" + 
          "<author>" + 
           "<name>Google</name>" + 
           "<email>[email protected]</email>" + 
          "</author>" + 
         "</entry>" + 
        "</feed>"; 

      XDocument doc = XDocument.Parse(xml); 
      XElement firstNode = (XElement)doc.FirstNode; 
      XNamespace ns = firstNode.Name.Namespace; 
      var results = doc.Elements(ns + "feed").Select(x => new { 
        tagline = (string)x.Element(ns + "tagline"), 
        message_subject = (string)x.Element(ns + "entry").Element(ns + "title") 
       }).FirstOrDefault(); 


     } 
    } 
} 
Смежные вопросы