C# 使用SelectSingleNode读取XML
我使用以下代码读取指定的XMLC# 使用SelectSingleNode读取XML,c#,xml,C#,Xml,我使用以下代码读取指定的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</t
<?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>
只是想知道问题出在哪里
感谢使用
System.ServiceModel
程序集中定义到System.ServiceModel.Syndication
命名空间中的SyndicationFeed
使您的生活更轻松
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
提供的属性,请检查此项 试试看
message_subject=node2.选择SingleNode(//feed:entry/feed:title),
nsmgr.InnerText
注意:您使用了“feed”作为名称空间的名称,因此标题也应该是限定的我更喜欢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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
const int NUMBER_OF_XML=3;
静态void Main(字符串[]参数)
{
字符串xml=
"" +
"" +
“Gmail-电子邮件收件箱”xxxx@gmail.com" +
“Gmail收件箱中的新邮件”+
"1" +
"" +
“2016-02-07T12:11:21Z”+
"" +
“已打开对不太安全的应用的访问”+
“对不太安全的应用的访问已打开嗨,伙计,你最近更改了安全设置,所以”+
"" +
“2016-02-07T11:45:12Z”+
“2016-02-07T11:45:12Z”+
标签:gmail.google.com,2004:1525516088640373323+
"" +
“谷歌”+
”“没有-reply@accounts.google.com" +
"" +
"" +
"";
XDocument doc=XDocument.Parse(xml);
XElement firstNode=(XElement)doc.firstNode;
XNamespace ns=firstNode.Name.Namespace;
var results=doc.Elements(ns+“feed”)。选择(x=>new{
tagline=(字符串)x.Element(ns+“tagline”),
消息主题=(字符串)x.Element(ns+“条目”).Element(ns+“标题”)
}).FirstOrDefault();
}
}
}
我不明白,为什么要删除名称空间,然后重新引入它?无论如何,请尝试//feed:entry/title。
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();
}
}
}