使用 SelectSingleNode 读取 XML
本文关键字:XML 读取 SelectSingleNode 使用 | 更新日期: 2023-09-27 18:33:34
>我使用以下代码来读取指定的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
只是想知道问题可能在哪里。
谢谢
试试
message_subject = 节点 2。SelectSingleNode("//feed:entry/feed:title", NSMGR)。内部文本;
注意:您已使用"feed"作为命名空间的名称,因此标题也应限定
通过使用从
程序集System.ServiceModel.Syndication
命名空间中定义的SyndicationFeed
使您的生活更轻松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
提供哪些属性,请查看此链接。
我更喜欢 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();
}
}
}