我使用以下代码来读取指定的XML
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<feed version=\"0.3\" xmlns=\"http://purl.org/atom/ns#\">
<title>Gmail - Inbox for xxxx@gmail.com</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/mail?account_id=agl.testauto@gmail.com&message_id=152bb8ccd28d824b&view=conv&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>no-reply@accounts.google.com</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
只是想知道问题出在哪里。
由于
答案 0 :(得分: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。
答案 1 :(得分:2)
试
message_subject = node2.SelectSingleNode(“// feed:entry / feed:title”, nsmgr).InnerText;
注意:您已使用“feed”作为命名空间的名称,因此标题也应该是合格的
答案 2 :(得分: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 xxxx@gmail.com</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/mail?account_id=agl.testauto@gmail.com&message_id=152bb8ccd28d824b&view=conv&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>no-reply@accounts.google.com</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();
}
}
}