0

I read multiple feed from many sources with C# Console, and i have this code where i load XML From sources:

XmlDocument doc = new XmlDocument();
doc.Load(sourceURLX);
XElement xdoc = XElement.Load(sourceURLX);

How to get enclosure url and show as variable?

Mister XP
  • 63
  • 1
  • 1
  • 9
  • 1
    Could you clarify the question? What do you mean by "enclosure url"? If appropriate, a sample of your XML might also be useful to include in the question. – Mike Baxter Jul 14 '17 at 10:44
  • I`m reading feeds, then i take values and put in database as strings, now i need to read th enclosure article image to put url to image in table. Because with console i read feeds and add in database as strings. Title, Description, URL-to article and Article Image. Now my problem is just to take URL from enclosure tag as string, maby as SElement is possible or something else idea. Maby now you can clearly understand my problem – Mister XP Jul 14 '17 at 10:50
  • `xDoc.Element("item").Element("enclosure").Attribute("url").Value` – Fabio Jul 14 '17 at 10:55
  • @Fabio - `var encImg1 = xdoc.Element("item").Element("enclosure").Attribute("url").V‌​alue;` this line return null value for url where exist enclosure tag :( – Mister XP Jul 14 '17 at 11:45

2 Answers2

0

If I understand your question correctly (I'm making a big assumption here) - you want to select an attribute from the root (or 'enclosing') tag, named 'url'?

You can make use of XPath queries here. Consider the following XML:

<?xml version="1.0" encoding="utf-8"?>
<root url='google.com'>
    <inner />
</root>

You could use the following code to retrieve 'google.com':

String query = "/root[1]/@url";
XmlDocument doc = new XmlDocument();
doc.Load(sourceURLX);
String value = doc.SelectSingleNode(query).InnerText;

Further information about XPath syntax can be found here.

Edit: As you stated in your comment, you are working with the following XML:

<item>
    <description>
    </description>
    <enclosure url="blablabla.com/img.jpg" />
</item>

Therefore, you can retrieve the url using the following XPath query:

/item[1]/enclosure[1]/@url
Mike Baxter
  • 6,868
  • 17
  • 67
  • 115
  • _I'm making a big assumption here_ - why not asking OP to clarify before? ;) – Fabio Jul 14 '17 at 10:46
  • I'll delete the answer if it's not appropriate :) – Mike Baxter Jul 14 '17 at 10:49
  • Thank you, I`m new in programming, and my english is not so good. But thank you. I try with your answer but not tags as in XML Feeds wich have enclosure, because enclosure tags is include item tag as this etc – Mister XP Jul 14 '17 at 11:05
  • @MisterXP I am having difficulty understanding you, but I have updated my answer. If it solves your issue then please mark the answer as accepted. :) – Mike Baxter Jul 14 '17 at 12:56
  • @Teifi - Hallo, i testing this code, but i make little modification. `var node = doc.SelectSingleNode("rss/channel/item/enclosure"); var encImg = node.Attributes["url"].Value;` and take the value, but all the time take just first link, not take diferent image for all 10 articles. Because i take one by one and save to database, and in this moment for all 10articles saving just image from first article. how to go to next and next and next etc... ? – Mister XP Jul 15 '17 at 12:29
0

With xml like below

<?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0">
        <channel>
            <title>title</title>
            <link>https://www.link.com</link>
            <description>description</description>
            <item>
                <title>RSS</title>
                <link>https://www.link.com/xml/xml_rss.asp</link>
                <description>description</description>
                <enclosure url="https://www.link.com/media/test.wmv"
                           length="10000" 
                           type="video/wmv"/>
           </item>
       </channel>
    </rss>

You will get url by reading attribute

var document = XDocument.Load(sourceURLX);
var url = document.Root
                  .Element("channel")
                  .Element("item")
                  .Element("enclosure")
                  .Attribute("url")
                  .Value;

To get multiple urls

var urls = document.Descendants("item")
                   .Select(item => item.Element("enclosure").Attribute("url").Value)
                   .ToList();

Using foreach loop

foreach (var item in document.Descendants("item"))
{
    var title = item.Element("title").Value;
    var link = item.Element("link").Value;
    var description = item.Element("description").Value;
    var url = item.Element("enclosure").Attribute("url").Value;
    // save values to database
}
Fabio
  • 31,528
  • 4
  • 33
  • 72
  • is this possible in Console application? i get this error: `Error CS1061 'XmlDocument' does not contain a definition for 'Root' and no extension method 'Root' accepting a first argument of type 'XmlDocument' could be found (are you missing a using directive or an assembly reference?)` – Mister XP Jul 14 '17 at 12:14
  • Yes it will work. Use `var document = XDocument.Load(sourceURLX)` – Fabio Jul 14 '17 at 12:17
  • It work, but for all 10 articles i get same image, from firstone article, i get 10 times just first image. – Mister XP Jul 14 '17 at 12:25
  • How to get one by one all of the image urls for diferent article? – Mister XP Jul 14 '17 at 12:40
  • This is also ok, and create a list of URLS, but if i try to take with foreach, foreach go continious, and i can`t take values one by one for everyone records. How to take one by one values,? Ex. to take first, continue code, save to database and than back to secund and continue etc.. ? Thank you – Mister XP Jul 14 '17 at 15:23
  • can you help me one more time ? – Mister XP Jul 15 '17 at 07:48
  • Now saving 10 articles 10 x10 articles, foreach image save all articles by 10 times, if source have 10 articles in database save 100 articles ? – Mister XP Jul 15 '17 at 08:33
  • Have you any idea how to save just one time ? – Mister XP Jul 15 '17 at 20:28
  • can you help me? – Mister XP Jul 16 '17 at 10:54