0

This is XML structure:

<items>
        <item>
            <title>Weź udział w grze historycznej</title>
            <link>https://legionowo.pl/a/wez-udzial-w-grze-historycznej</link>
            <description><![CDATA[<img src="https://legionowo.pl/img/artykuly/4/2022_07/x41594.jpg.pagespeed.ic.9BJkaqvzdK.jpg%22%3E<br><br>]]></description>
            <pubDate>Fri, 01 Jul 2022 09:10:50 +0000</pubDate>
            <media:content url="https://legionowo.pl/img/artykuly/4/2022_07/x41594.jpg.pagespeed.ic.9BJkaqvzdK.jpg" medium="image" />
            <guid isPermaLink="false">https://legionowo.pl/a/wez-udzial-w-grze-historycznej</guid>
        </item>
<items>

this is C# method:

private async Task _XElementGet()
        {
            XElement doc = XElement.Load("C:\\mail.xml");
            List<XElement> list;
            list = (from prod in doc.Elements("item")
                    select prod).ToList();
            foreach(XElement item in list)
            {
                Console.WriteLine(item.Name);
            }
        }

and its empty, but when i am trying to use XDocument it works and it looks:

private async Task _XDocumentGet()
        {
            XDocument doc = XDocument.Load("C:\\mail.xml");
            List<XElement> list;
            list = (from prod in doc.Descendants("item")
                    select prod).ToList();
            foreach(XElement item in list)
            {
                //Console.WriteLine(item.Element("title"));
                Console.WriteLine(item.FirstAttribute.Value);
            }
        }

This is first.

Second is about this Attribute "media:content" -> i cannot get its value because i get this error message:

System.Xml.XmlException: 'The ':' character, hexadecimal value 0x3A, cannot be included in a name.' how to read value from this attribute

Any ideas how i can fix it?

1 Answers1

0

Try following :

using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using System.Xml.Linq;


namespace ConsoleApp2
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement description = doc.Descendants("description").FirstOrDefault();
            string descr = (string)description;
            string descrDecoded = System.Net.WebUtility.UrlDecode(descr);
 
 
        }

    }
 


}
jdweng
  • 33,250
  • 2
  • 15
  • 20