CURRENT STATUS: The forums you are now looking at are CLOSED. Visit the new forums.


Invoke-Rest method and XML CDATA?

Creating PowerShell extensions in Visual Studio? Post here! Monitored by MVP Doug Finke.
Forum rules
1. Use the CODE or POWERSHELL buttons to format code and commands.
2. Avoid the use of obscure and punctuation aliases (like ? and %).
3. Don't apologize for being a "noob." We don't mind.
4. If we help you solve your question, please click the green SOLVED checkmark.

More forums tips & etiquette

Invoke-Rest method and XML CDATA?

Postby yooakim » Wed Aug 15, 2012 9:20 am

I am Learning PowerShell and love it more by every day. It's simple and elegant! And I love the object pipeline, it makes stitching things togehter childs play (almost!)

I have a question about using the Invoke-Rest method. I use it against a RSS feed. This particular RSS feed wraps the contents of title, description and other elements in
Code: Select all
<title><![CDATA[Här är texten med lite specialare]]</title>


When I use this code

Code: Select all
Invoke-RestMethod $url


Everything works but the result does not give me the content of the CDATA elements. I know I can use .innerText on these elements but am wondering if there is Another, easier, way to do this?

Any suggestions of how to obtain content of CDATA elements?

Cheers,
Joakim
yooakim
 
Posts: 23
Joined: Wed Aug 15, 2012 9:10 am

Re: Invoke-Rest method and XML CDATA?

Postby DougFinke » Wed Aug 15, 2012 8:14 pm

Hi Joakim and thanks for the post.

You should be able to get the content of the CDATA section this way.

Code: Select all
(Invoke-RestMethod $url).Title.InnerText


Here's the challenge, if the Title does not contain a CDATA section, this will print nothing.

So, you'll need to foreach over the results and check and either print the Title or Title.InnerText.

Post the RSS feed url and we can take a look.

Thanks
Doug
Doug's Book | Doug's Blog | PowerShell MVP
User avatar
DougFinke
MVP
MVP
 
Posts: 11
Joined: Mon Aug 13, 2012 1:24 pm
Location: New York City

Re: Invoke-Rest method and XML CDATA?

Postby yooakim » Thu Aug 16, 2012 6:24 am

Thanks, this is the URL I'm reading from:

http://sverigesradio.se/api/rss/pod/4023

I'm not sure why the keep encoding everything in CDATA since it is UTF-8 but they do... ;-)

/j
yooakim
 
Posts: 23
Joined: Wed Aug 15, 2012 9:10 am

Re: Invoke-Rest method and XML CDATA?

Postby DougFinke » Thu Aug 16, 2012 12:31 pm

Yes, many sites do things differently with their feeds. The good news is, PowerShell is flexible enough so you don't hit too many walls. Below are a couple ways you could process that feed. From a quick look, it seems they are all wrapped in CDATA sections.


Code: Select all
Invoke-RestMethod http://sverigesradio.se/api/rss/pod/4023 |
    Select Pubdate, Author, @{Name='Title'; Expression={$_.Title.InnerText}}



Code: Select all
Invoke-RestMethod http://sverigesradio.se/api/rss/pod/4023 | ForEach {
    [PSCustomObject] @{
        Pubdate = $_.pubdate
        Title   = $_.Title.InnerText
        Author  = $_.Author
    }
}
Doug's Book | Doug's Blog | PowerShell MVP
User avatar
DougFinke
MVP
MVP
 
Posts: 11
Joined: Mon Aug 13, 2012 1:24 pm
Location: New York City

Re: Invoke-Rest method and XML CDATA?

Postby yooakim » Thu Aug 16, 2012 12:59 pm

With PowerShell v3.0 it's so elegant! Thanks :-)

/joakim
yooakim
 
Posts: 23
Joined: Wed Aug 15, 2012 9:10 am


Return to PowerShell for Developers

Who is online

Users browsing this forum: No registered users and 1 guest

cron