Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deserialize XML with C# from URL

I'm pulling an XML-Sitemap from a website to parse it.

The easyest way would be to deserialize it into on objet.

I get throw the error "Error in XML-Document" on the last line in my example-code. Does anybody know why. There aren't any more details in the error-message.

My Code so far:

[Serializable, XmlRoot("urlset")]
public class Urlset
{
    public B5_Url[] urls;
}
[XmlType("url")]
public class B5_Url
{
    [XmlElement("loc")]
    public string loc;
    [XmlElement("lastmod")]
    public string lastmod;
    [XmlElement("changefreq")]
    public string changefreq;
}
class Program
{
    static void Main(string[] args)
    {
        string url = "http://www.myurl.de/sitemap.xml";

        XmlSerializer ser = new XmlSerializer(typeof(Urlset));

        WebClient client = new WebClient();

        string data = Encoding.Default.GetString(client.DownloadData(url));

        Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(data));

        Urlset reply = (Urlset)ser.Deserialize(stream);  
    }
}

This is the XML:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9             http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
  <loc>http://www.myurl.de/</loc>
  <lastmod>2012-06-25T17:10:30+00:00</lastmod>
  <changefreq>always</changefreq>
</url>
</urlset>

Thanks for your help :)

like image 253
B5-NDDT Avatar asked Mar 19 '26 01:03

B5-NDDT


2 Answers

You should do what @vitalygolub is suggesting. Also, you will still get an error because of Namespace set in the root element. To fix it:

[XmlRoot("urlset", Namespace="http://www.sitemaps.org/schemas/sitemap/0.9")]
public class Urlset
{
    [XmlElement("url")]
    public B5_Url[] urlset;
}

public class B5_Url
{
    [XmlElement("loc")]
    public string loc;
    [XmlElement("lastmod")]
    public string lastmod;
    [XmlElement("changefreq")]
    public string changefreq;
}

I tested this code and it works with your input.

like image 123
Arie Avatar answered Mar 20 '26 15:03

Arie


If that is your XML, you're missing a closing </url>:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9             http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
  <loc>http://www.myurl.de/</loc>
  <lastmod>2012-06-25T17:10:30+00:00</lastmod>
  <changefreq>always</changefreq>
</url>
</urlset>

I got the error <urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'> was not expected.

Changing:

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

To

<urlset>

Made it not error.

This is my working linqpad example

[Serializable, System.Xml.Serialization.XmlRoot("urlset")]
public class Urlset
{
    [System.Xml.Serialization.XmlElement("url")]
    public B5_Url[] urls;
}
[System.Xml.Serialization.XmlType("url")]
public class B5_Url
{
    [System.Xml.Serialization.XmlElement("loc")]
    public string loc;
    [System.Xml.Serialization.XmlElement("lastmod")]
    public string lastmod;
    [System.Xml.Serialization.XmlElement("changefreq")]
    public string changefreq;
}
class Program
{
    static void Main(string[] args)
    {
        var data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><urlset><url><loc>http://www.myurl.de/</loc><lastmod>2012-06-25T17:10:30+00:00</lastmod><changefreq>always</changefreq></url></urlset>";

        var ser = new System.Xml.Serialization.XmlSerializer(typeof(Urlset));

        Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(data));

        Urlset reply = (Urlset)ser.Deserialize(stream);  
        reply.Dump();
    }
}

The other change I had to make was the [System.Xml.Serialization.XmlElement("url")] attribute on the B5_Url[] array in UrlSet

like image 43
NikolaiDante Avatar answered Mar 20 '26 15:03

NikolaiDante



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!