Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用SelectSingleNode读取XML_C#_Xml - Fatal编程技术网

C# 使用SelectSingleNode读取XML

C# 使用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

<?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&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>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&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>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&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>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();


        }
    }
}