在C#中解析XML feed

时间:2016-03-08 14:11:37

标签: xml feed

下面是我要在C#中解析的XML Atom提要,并检索&#39; d:Name&#39;,&#39; d:LookupID&#39;等等的值... < / p>

<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns:gml="http://www.opengis.net/gml" xmlns:georss="http://www.georss.org/georss" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns="http://www.w3.org/2005/Atom">
<id>Query Results</id>
<title/>
<updated>2016-03-07T14:06:33Z</updated>
<entry>
<category scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" term="CC.AppFx.OData.Query"/>
<id/>
<title/>
<updated>2016-03-07T14:06:33Z</updated>
<author>
<content type="application/xml">
<m:properties>
<d:Name>Emelia Kubala</d:Name>
<d:Firstname>Emelia</d:Firstname>
       <d:LastOrganizationGroupHouseholdname>Kubala</d:LastOrganizationGroupHouseholdname>
   <d:LookupID>1261855</d:LookupID>
   <d:Age m:type="Edm.Int32">60</d:Age>
   <d:Birthdate>1/1/1956</d:Birthdate>
   <d:AddressPrimaryFulladdress>4031 Blue Lane Merville, CT 99999-3681</d:AddressPrimaryFulladdress>
</m:properties>
</content>
</entry>
</feed>

我尝试了以下代码,但只考虑了我能够从内容&#39;是元素的连接文本值:

        Atom10FeedFormatter formatter = new Atom10FeedFormatter();
        XmlUrlResolver resolver = new XmlUrlResolver();
        resolver.Credentials = new NetworkCredential("username", "password");
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.XmlResolver = resolver;

        using ( XmlReader reader = XmlReader.Create("http://localhost02308/feed/Query.ashx?dbname=dbname&QueryID=54d52042-3a48-4ba6-8ab6-9513069503de", settings))
        {
            formatter.ReadFrom(reader);
        }

        foreach (SyndicationItem item in formatter.Feed.Items)
        {
            Console.WriteLine(item.PublishDate.ToString());
            Console.WriteLine(item.LastUpdatedTime.ToString());
            Console.WriteLine(item.Content.Type.ToString());
            Console.WriteLine(item.Authors.ToString());
            Console.WriteLine("contentType = {0}", item.Content == null ? "null" : item.Content.Type);



            XDocument docOData = new XDocument();
            using (XmlWriter writer = docOData.CreateWriter())
                item.Content.WriteTo(writer, "content", "w");


            XElement content = docOData.Root != null ? docOData.Root.Elements().FirstOrDefault() : null;

            Console.WriteLine("content:{0}", content.Value);
            Console.WriteLine("content:{0}", content.Value[0]);
            Console.WriteLine("content:{0}", content.Value.Length);

            IEnumerable<XElement> childList =
             from el in docOData.Elements()
             select el;

            foreach (XElement e in childList)
            {
                Console.WriteLine(e.Value);
                string xPath = "//w/m/d";
                 var eleList = docOData.XPathSelectElements(xPath).ToList();
                foreach (var xElement in eleList)
                {
                    Console.WriteLine(xElement);
                }

                Console.ReadLine();

我想知道如何找回&#39;有些价值&#34;从 &#39;内容类型=&#34; application / xml&#39; - &gt; &#39; M:性质&#39; - &GT; &#39; d:姓名&#39;有些值&#39; / d:姓名&#39;

谢谢

1 个答案:

答案 0 :(得分:0)

试试这个:

                XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
                XElement po = XElement.Load(@"c:\users\user\documents\visual studio 2015\Projects\BigDataProj\UnitTestProject1\XMLFile1.xml");
                XElement tempElement = po.Descendants(d + "Name").FirstOrDefault();
                string name = tempElement.Value;
                XElement tempElement1 = po.Descendants(d + "LookupID").FirstOrDefault();
                string lookup = tempElement1.Value;