简体   繁体   中英

SyndicationFeed Unable To Read ATOM Feed

I am trying to parse the following atom XML feed:

<dealer version="1.12" xmlns:atom="http://www.w3.org/2005/Atom"><atom:link rel="self" href="http://Blah.com/dealers/1234"/><atom:link rel="http://Blah.com/rels/dealer_notification_prefs" href="http://Blah.com/dealers/1234/notification_prefs"/><atom:link rel="http://Blah.com/rels/dealer_systems" href="http://Blah.com/dealers/1234/systems"/><atom:link rel="http://Blah.com/rels/dealer_logo" href="http://Blah.com/dealers/1234/logo"/><pid>1234</pid><name>ABC Heating &amp; Air Conditioning</name><first>X</first><last>X</last><street1>PO Box 321</street1><street2/><city>Orson</city><state>IN</state><country>United States</country><postal>46142</postal><phone>317-555-5555</phone><phoneExt/><url></url><email>someone@noemail.com</email></dealer>

The C# code I am using is:

 using (var client = new HttpClient()) // Using block for disposing of HttpClient when finished
        {
            client.DefaultRequestHeaders.Accept.Clear();
            client.BaseAddress = new Uri(_baseUriPart); // Set to core base Uri for whole Api
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("OAuth", _builtParamsString);

            // Send HTTP Requests Async

            try
            {
                bool respSuccess = false;


                HttpResponseMessage response = await client.GetAsync(_resourceUriPart);
                //HttpResponseMessage response = await client.SendAsync(myRequestTest);
                response.EnsureSuccessStatusCode(); // Throw Exception if not a success code. // ...}

                Stream stream = await response.Content.ReadAsStreamAsync();
                var prereader = new StreamReader(stream);
                string readContent = prereader.ReadToEnd();

                string readOut = string.Empty;

                TextReader tr = new StringReader(readContent);

                XmlReader reader = XmlReader.Create(tr);

                SyndicationFeed feed = SyndicationFeed.Load(reader);
                if(null!=feed)
                {                    
                 foreach(var item in feed.Items)
                 {
                     //readOut = readOut + item.Title.Text + ":" + ((TextSyndicationContent)item.Content).Text+ Environment.NewLine;
                 }
                }
                respSuccess = response.IsSuccessStatusCode;
                TextBox1.Text = respSuccess.ToString();
                TextBox2.Text = response.StatusCode.ToString();
                TextBox3.Text = readOut;
            }
            catch (HttpRequestException e)
            {
                TextBox1.Text = "False";
                TextBox2.Text = "See Content Message";
                TextBox3.Text = e.Message;
            }
        } // End using block

I can connect to the web service, and request the dealer info as you can see. But the error I get when the SyndicationFeed begins reading the XML is:

"The element with name 'dealer' and namespace '' is not an allowed feed format. "

Can someone please shed some light on this for me? Thanks!!!

dealer isn't a valid tag for the atom feed root. See the Atom Syndication Format RFC for details. It should be atom:feed .

It's unfortunately pretty common to find invalid RSS/Atom feeds. SyndicationFeed is strict so you have to do some massaging of the input data to get it working.

It's ugly but the simple approach is to do a String.Replace for the dealer tags.

// ...    

readContent = readContent.Replace("&lt;dealer ", "&lt;atom:feed ").Replace("&lt;/dealer&gt;", "&lt;/atom:feed&gt;");

TextReader tr = new StringReader(readContent);

// ...

I've also fixed feeds in the past by deriving from XmlTextReader and fixing the bad elements as they are read.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM