C# Deserializing Atom XML - c#

I want to deserialize the follwoing Atom XML:
<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:tel="http://tel.search.ch/api/spec/result/1.0/" xml:lang="de">
<id>https://tel.search.ch/api/ed14c7f5e42390640730cb18523640d5</id>
<title type="text">tel.search.ch API Search Results</title>
<generator version="1.0" uri="https://tel.search.ch">tel.search.ch</generator>
<updated>2018-08-28T02:00:00Z</updated>
<link href="https://tel.search.ch/result.html?was=0418553553" rel="alternate" type="text/html" />
<link href="http://tel.search.ch/api/?was=0418553553" type="application/atom+xml" rel="self" />
<openSearch:totalResults>1</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>10</openSearch:itemsPerPage>
<openSearch:Query role="request" searchTerms="1234567890 " startPage="1" />
<openSearch:Image height="1" width="1" type="image/gif">https://www.search.ch/audit/CP/tel/de/api</openSearch:Image>
<entry>
<id>urn:uuid:ef812a3ea1e8e6f0</id>
<updated>2018-08-28T02:00:00Z</updated>
<published>2018-08-28T02:00:00Z</published>
<title type="text">Power, Max</title>
<content type="text">Max Power, Firststreet 1 6000 Switzerland/SZ 123 456 78 90</content>
<tel:nopromo>*</tel:nopromo>
<author>
<name>tel.search.ch</name>
</author>
<link href="https://tel.search.ch/switzerland/maxPower" title="Details" rel="alternate" type="text/html" />
<link href="https://tel.search.ch/switzerland/maxPower" type="text/x-vcard" title="VCard Download" rel="alternate" />
<link href="https://tel.search.ch/edit/?id=ef812a3ea1e8e6f0" rel="edit" type="text/html" />
</entry>
</feed>
Generatet by local.ch (Phonebook from Switzerland)
With this post (InvalidOperationException deserializing Atom XML) i created this tiny console application:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace consXMLtest
{
class Program
{
static void Main(string[] args)
{
XmlSerializer xs = new XmlSerializer(typeof(RssFeedModel));
XmlTextReader reader = new XmlTextReader(#"D:/test.xml");
RssFeedModel rssFeedModel = (RssFeedModel)xs.Deserialize(reader);
Console.WriteLine(rssFeedModel.Title);
Console.ReadKey();
}
}
}
[XmlRoot("feed", Namespace = "http://www.w3.org/2005/Atom")]
public class RssFeedModel
{
[XmlElement("title")]
public string Title { get; set; }
[XmlElement("link")]
public List<Link> link { get; set; }
[XmlElement("entry")]
public List<Entry> entry { get; set; }
}
[XmlRoot("link")]
public class Link
{
[XmlAttribute("rel")]
public string rel { get; set; }
[XmlAttribute("type")]
public string type { get; set; }
}
[XmlRoot("entry")]
public class Entry
{
[XmlElement("id")]
public string Id { get; set; }
[XmlElement("published")]
public DateTime PublishDate { get; set; }
[XmlElement("content")]
public Content content { get; set; }
}
[XmlRoot("content")]
public class Content
{
[XmlAttribute("type")]
public string type { get; set; }
[XmlText]
public string text { get; set; }
}
Now my question: How can I access to the "Content" class?
For my application i only need the content.
Thank you for your help.
Greetings from Switzerland
niju

If I understand you correctly you simply iterate through all entries with
foreach(var c in rssFeedModel.entry)
Console.WriteLine(c.content.text);
and can read out the content properties.

Try this:
public static void Main()
{
XmlSerializer xs = new XmlSerializer(typeof(RssFeedModel));
XmlTextReader reader = new XmlTextReader(#"D:/test.xml");
RssFeedModel rssFeedModel = (RssFeedModel)xs.Deserialize(reader);
Console.WriteLine(rssFeedModel.entry.FirstOrDefault()?.content.text);
Console.ReadKey();
}

Related

XML array with custom attribute and element names

I want to serialize/deserialize xml document in C# like:
<library>
<my.books genre =""classic"">
<book title = ""1984"" author=""George Orwell"" />
<book title = ""Robinson Crusoe"" author=""Daniel Defoe"" />
<book title = ""Frankenstein"" author=""Mary Shelly"" />
</my.books>
</library>";
There are 2 important things:
Element "my.books" must have custom name (not a property name)
my.books element must have an attribute ("genre").
Here is my code (sample is on https://dotnetfiddle.net/bH5WVX) :
using System;
using System.Xml;
using System.Xml.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;
using System.IO;
public class Program
{
public static void Main()
{
Library lib = new Library(myBooks: new MyBooks(
genre: "classic",
booklist: new List<Book>{
new Book("1984", "George Orwell"),
new Book("Robinson Crusoe", "Daniel Defoe"),
new Book("Oliver Twist", "Mary Shelly"),
}));
XmlSerializer formatter = new XmlSerializer(typeof(Library));
using (StringWriter sw = new StringWriter())
{
formatter.Serialize(sw, lib);
Console.Write(sw.ToString());
}
string desiredOutput =
#"<library>
<my.books genre =""classic"">
<book title = ""1984"" author=""George Orwell"" />
<book title = ""Robinson Crusoe"" author=""Daniel Defoe"" />
<book title = ""Frankenstein"" author=""Mary Shelly"" />
</my.books>
</library>";
}
[XmlRoot("library")]
public class Library
{
public MyBooks MyBooks { get; set; }
[XmlElement("my.books")]
public List<Book> Books { get; set; }
public Library()
{
}
public Library(MyBooks myBooks = null)
{
MyBooks = myBooks;
}
}
[XmlType("my.books")]
public class MyBooks
{
[XmlAttribute("genre")]
public string Genre { get; set; }
[XmlElement("book")]
public List<Book> Booklist { get; set; }
public MyBooks(string genre, List<Book> booklist = null)
{
Genre = genre;
Booklist = booklist;
}
public MyBooks()
{
}
}
public class Book
{
[XmlAttribute("title")]
public string Title { get; set; }
[XmlAttribute("author")]
public string Author { get; set; }
public Book() { }
public Book(string title, string author)
{
Title = title;
Author = author;
}
}
}
And the output is:
<library>
<MyBooks genre="classic">
<book title="1984" author="George Orwell" />
<book title="Robinson Crusoe" author="Daniel Defoe" />
<book title="Oliver Twist" author="Mary Shelly" />
</MyBooks>
</library>
The only problem is that I can't force element "MyBooks" to use name "my.books"
I found only one related article on this topic - http://www.codemeit.com/xml/c-xmlserializer-add-an-attribute-to-an-array-element.html, it suggests to use "XmlType" attribute on class, but it doesn't work here.
Is there any way to apply custom name attribute on this element?
It looks like your attribute was on the wrong property.
Try this:
[System.Xml.Serialization.XmlElement("my.books")]
public MyBooks MyBooks { get; set; }
public List<Book> Books { get; set; }
I now get this output:
<?xml version="1.0" encoding="utf-16"?>
<library xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<my.books genre="classic">
<book title="1984" author="George Orwell" />
<book title="Robinson Crusoe" author="Daniel Defoe" />
<book title="Oliver Twist" author="Mary Shelly" />
</my.books>
</library>
Well done on a superbly written question!

Deserialize XML doc to .NET list

I have a XML doc that looks like this:
https://gyazo.com/87aef26804136ee0cac49cf8b529f9cd
https://gyazo.com/aeb8c56689da52c67afe9b0bf7c19348
How can I convert this to an .NET List object?
I have tried this:
Deserialization Code:
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (TextReader reader = new StringReader(xmlString))
{
List<Person> result = (List<Person>)serializer.Deserialize(reader);
}
Person class:
public class Person
{
public int id { get; set; }
public int client_id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public string phone_office { get; set; }
public string phone_mobile { get; set; }
public string fax { get; set; }
public string title { get; set; }
public DateTime createad_at { get; set; }
public DateTime updated_at { get; set; }
public bool isFromHighriseOrHarvest { get; set; }
}
XML:
<?xml version="1.0" encoding="UTF-8"?>
-<people type="array">
-<person>
<author-id type="integer">543801</author-id>
<background>Vi är har jobbat ihop och är vänner / Nathalie</background>
<company-id type="integer">81499881</company-id>
<created-at type="datetime">2011-08-10T08:39:45Z</created-at>
<first-name>Per</first-name>
<group-id type="integer" nil="true"/>
<id type="integer">81500134</id>
<last-name>"Cromwell" (Eriksson)</last-name>
<owner-id type="integer" nil="true"/>
<title>ägare, grafiker</title>
<updated-at type="datetime">2011-08-16T08:17:43Z</updated-at>
<visible-to>Everyone</visible-to>
<company-name>Studio Total</company-name>
<linkedin-url nil="true"/>
< avatar_url>https://secure.highrisehq.com/avatar_proxy/eJxj4Yhmz2SWLWTMZHk2_TYLABiEBDM|9d29b49d8f165ff33f28b7f7fac2926eb8487319</avatar_url>
-<contact-data>
-<web-addresses type="array">
-<web-address>
<id type="integer">70306124</id>
<location>Work</location>
<url>http://www.studiototal.se</url>
</web-address>
</web-addresses>
<twitter-accounts type="array"/>
-<email-addresses type="array">
-<email-address>
<address>per#studiototal.se</address>
<id type="integer">39720318</id>
<location>Work</location>
</email-address>
</email-addresses>
<addresses type="array"/>
-<phone-numbers type="array">
-<phone-number>
<id type="integer">70306123</id>
<location>Work</location>
<number>0703689909</number>
</phone-number>
</phone-numbers>
<instant-messengers type="array"/>
</contact-data>
</person>
-<person>
<author-id type="integer">848257</author-id>
<background/>
<company-id type="integer">153838696</company-id>
<created-at type="datetime">2013-02-18T12:49:37Z</created-at>
<first-name>"Kristofer"</first-name>
<group-id type="integer" nil="true"/>
<id type="integer">153838730</id>
<last-name>"Malmer"</last-name>
<owner-id type="integer" nil="true"/>
<title>Projektledare Online listening</title>
<updated-at type="datetime">2013-02-18T12:49:37Z</updated-at>
<visible-to>Everyone</visible-to>
<company-name>Santa Maria</company-name>
<linkedin-url nil="true"/>
<avatar_url>https://secure.highrisehq.com/avatar_proxy/eJxj4Yhmz2SWLWTMZOlK0eYEABUgAvk|d7e22f72a1a3ae2efa83df54e4184d429120cd9f</avatar_url>
-<contact-data>
<web-addresses type="array"/>
<twitter-accounts type="array"/>
<email-addresses type="array"/>
<addresses type="array"/>
-<phone-numbers type="array">
-<phone-number>
<id type="integer">129346649</id>
<location>Work</location>
<number>031-674151</number>
</phone-number>
</phone-numbers>
<instant-messengers type="array"/>
</contact-data>
</person>
-<person>
<author-id type="integer">848257</author-id>
<background/>
<company-id type="integer">151848665</company-id>
<created-at type="datetime">2013-02-01T10:14:27Z</created-at>
<first-name>"Sorush"</first-name>
<group-id type="integer" nil="true"/>
<id type="integer">151848627</id>
<last-name/>
<owner-id type="integer" nil="true"/>
<title/>
<updated-at type="datetime">2013-02-01T10:16:29Z</updated-at>
<visible-to>Everyone</visible-to>
<company-name>Rancold</company-name>
<linkedin-url nil="true"/>
<avatar_url>https://secure.highrisehq.com/avatar_proxy/eJxj4Yhmz2SWLWTMZNnMxssJABRuAqY|1606d5054fb0e0f0b5ccc657dffcd80966ab9b64</avatar_url>
-<contact-data>
-<web-addresses type="array">
-<web-address>
<id type="integer">127911276</id>
<location>Work</location>
<url>http://www.rancold.com</url>
</web-address>
</web-addresses>
<twitter-accounts type="array"/>
-<email-addresses type="array">
-<email-address>
<address>sa#rancold.com</address>
<id type="integer">76736018</id>
<location>Work</location>
</email-address>
</email-addresses>
<addresses type="array"/>
-<phone-numbers type="array">
-<phone-number>
<id type="integer">127911275</id>
<location>Work</location>
<number>031-7441284</number>
</phone-number>
</phone-numbers>
<instant-messengers type="array"/>
</contact-data>
</person>
The error is System.InvalidOperationException.
Additional message: there is something wrong with your xml document (2, 2).
You should annotate your Person type and its members with XML serialization attributes. See Controlling XML Serialization Using Attributes for more information.
You can build your C# class codefile automatically based on your data. First, clean up your XML (extra whitespace on < avatar_url>, adding the final end tag, getting rid of the stray - characters...
Given good XML, save to a file, and execute the following from a VS command prompt:
xsd.exe people.xml
This creates an XSD file from the XML. Then you need to create your code file:
xsd.exe /c people.xsd
Now you have a C# codefile which has everything configured properly. You can modify this codefile for your needs, but leave all of the attributes and other XML-related stuff alone.
Try xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication7
{
class Program
{
const string FILENAME = #"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
Person.people = doc.Descendants("person").Select(x => new Person()
{
id = (int)x.Element("author-id"),
client_id = (int)x.Element("company-id"),
first_name = (string)x.Element("first-name"),
last_name = (string)x.Element("last-name"),
email = (string)x.Descendants("email-addresses").FirstOrDefault(),
phone_office = x.Descendants("phone-number").Where(y => (string)y.Element("location") == "Work").Select(z => (string)x.Descendants("number").FirstOrDefault()).FirstOrDefault(),
phone_mobile = x.Descendants("phone-number").Where(y => (string)y.Element("location") == "Mobile").Select(z => (string)x.Descendants("number").FirstOrDefault()).FirstOrDefault(),
fax = x.Descendants("phone-number").Where(y => (string)y.Element("location") == "Fax").Select(z => (string)x.Descendants("number").FirstOrDefault()).FirstOrDefault(),
title = (string)x.Element("title"),
createad_at = (DateTime)x.Element("created-at"),
updated_at = (DateTime)x.Element("updated-at"),
}).ToList();
}
}
public class Person
{
public static List<Person> people = new List<Person>();
public int id { get; set; }
public int client_id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public string email { get; set; }
public string phone_office { get; set; }
public string phone_mobile { get; set; }
public string fax { get; set; }
public string title { get; set; }
public DateTime createad_at { get; set; }
public DateTime updated_at { get; set; }
public bool isFromHighriseOrHarvest { get; set; }
}
}

How to read XML elements c# (sample code provided)

I have the following XML sample that I want to read the data. I only provided one "project" element but this can have many "project" inside the "projects" root.
<?xml version="1.0" encoding="utf-8"?>
<projects>
<project>
<details>
<projectName><![CDATA[CxWtGZxYT]]></projectName>
<uniqueID>Pt144</uniqueID>
<collaboratingList>
<collaboratingOrganisation id="5318" value="EpCyxCv RvGxrXAYXGpA xCxWtGZxYT"/>
<collaboratingOrganisation id="0000" value="EpCyxCv RvGxrXAYXGpA xCxWtGZxYTd"/>
</collaboratingList>
<researchOutputList>
<item>
<pubDate>2014-02-04T00:00:00+00:00</pubDate>
<title><![CDATA[rGDEZ]]></title>
<link>link</link>
<guid>guid</guid>
<description><![CDATA[uGDB BDstA rGDEZ]]></description>
</item>
<item>
<pubDate>2015-08-04T00:00:00+00:00</pubDate>
<title><![CDATA[AERx CApCYZ.]]></title>
<link>link</link>
<guid>guid</guid>
<description><![CDATA[vwtpY]]></description>
</item>
</researchOutputList>
</details>
</project>
</projects>
I can read the <projectName> and <uniqueId> but cannot <researchOutputList> and <collaboratingList>. I am not sure what I am missing or if my XML structure is correct. My codes below:
[Serializable]
[XmlRoot("projects")]
public class BulkProjectRoot
{
public BulkProjectRoot()
{
BulkProjectDetails = new List<BulkProjectData>();
}
[XmlArray("project")]
[XmlArrayItem("details")]
public List<BulkProjectData> BulkProjectDetails { get; set; }
}
[Serializable]
[XmlRoot("details")]
public class BulkProjectData
{
public BulkProjectData()
{
ProjectResearches = new List<ProjectResearchOutputs>();
}
[XmlElement("projectName")]
public string Name { get; set; }
[XmlElement("uniqueID")]
public string ProjectUniqueId { get; set; }
[XmlElement("researchOutputList")]
public List<ProjectResearchOutputs> ProjectResearches { get; set; }
}
[Serializable]
[XmlRoot("researchOutputList")]
public class ProjectResearchOutputs
{
public ProjectResearchOutputs()
{
ResearchItemsList = new List<ResearchOutputItems>();
}
[XmlElement("item")]
public List<ResearchOutputItems> ResearchItemsList { get; set; }
}
[Serializable]
[XmlRoot("item")]
public class ResearchOutputItems
{
[XmlElement("pubDate")]
public DateTime? PublishDate { get; set; }
[XmlElement("title")]
public string ResearchTitle { get; set; }
[XmlElement("link")]
public string ResearchLink { get; set; }
[XmlElement("guid")]
public string ResearchGuid { get; set; }
[XmlElement("description")]
public string ResearchDescription { get; set; }
}
The reasearchlist deserializes fine with Linqpad, but you are missing the collaboration information in your BulkProjectData class.
void Main()
{
var xml = #"<?xml version=""1.0"" encoding=""utf - 8""?>
<projects>
<project>
<details>
<projectName><![CDATA[CxWtGZxYT]]></projectName>
<uniqueID> Pt144 </uniqueID>
<collaboratingList>
<collaboratingOrganisation id = ""5318"" value = ""EpCyxCv RvGxrXAYXGpA xCxWtGZxYT"" />
<collaboratingOrganisation id = ""0000"" value = ""EpCyxCv RvGxrXAYXGpA xCxWtGZxYTd"" />
</collaboratingList>
<researchOutputList>
<item>
<pubDate>2014-02-04T00:00:00+00:00</pubDate>
<title><![CDATA[rGDEZ]]></title>
<link> link </link>
<guid> guid </guid>
<description><![CDATA[uGDB BDstA rGDEZ]]></description>
</item>
<item>
<pubDate>2015-08-04T00:00:00+00:00</pubDate>
<title><![CDATA[AERx CApCYZ.]]></title>
<link> link </link>
<guid> guid </guid>
<description><![CDATA[vwtpY]]></description>
</item>
</researchOutputList>
</details>
</project>
</projects>";
var serializer = new XmlSerializer(typeof(BulkProjectRoot));
var result = (BulkProjectRoot)serializer.Deserialize(new StringReader(xml));
result.Dump();
Jsut make this change -
[Serializable]
[XmlRoot("details")]
public class BulkProjectData
{
public BulkProjectData()
{
// Not a list
ProjectResearches = new ProjectResearchOutputs();
}
[XmlElement("projectName")]
public string Name { get; set; }
[XmlElement("uniqueID")]
public string ProjectUniqueId { get; set; }
[XmlElement("researchOutputList")]
public ProjectResearchOutputs ProjectResearches { get; set; }
}

deserializing a nested list in xml

I am c# silverlight beginner i am under a situation that i have thios xml code:
string xmlstring = #"<?xml version='1.0' encoding='utf-8' ?>
<par>
<name>amount</name>
<label>Amount</label>
<unit>Hundred</unit >
<comp>
<type>Combo</type>
<attributes>
<type>Integer</type>
<displayed>4</displayed>
<selected>0</selected>
<item>5</item>
</attributes>
</comp>
</par>";
** Now what is the problem ?**
In the last line when i try to debug "item" which is asssigned several value like 5 on line Debug.WriteLine(attrib.item);i just see only "5" on debugging it dont show other values. I guess i need to implement a list for it. But how to do it here that i don't know. Could some one please help me so that i will be able to have all the assigned values to item in this c# code because after this step i havce to create a GUI of it. Woudl be a big help.
Note: I cannot use ArrayList because silverligth dont support it any other lastertnative please ?
This is what you need:
[XmlRoot(ElementName = "parameter")]
public class Parameter
{
[XmlElement("name")]
public string Name { get; set; }
[XmlElement("label")]
public string Label { get; set; }
[XmlElement("unit")]
public string Unit { get; set; }
[XmlElement("component")]
public Component Component { get; set; }
}
[XmlRoot(ElementName = "component")]
public class Component {
[XmlElement("type")]
public string Type { get; set; }
[XmlElement("attributes")]
public Attributes Attributes { get; set; }
}
[XmlRoot(ElementName = "attributes")]
public class Attributes
{
[XmlElement("type")]
public string Type { get; set; }
[XmlElement("displayed")]
public string Displayed { get; set; }
[XmlElement("selected")]
public string Selected { get; set; }
[XmlArray("items")]
[XmlArrayItem("item", typeof(string))]
public List<string> Items { get; set; }
}
class Program
{
static void Main(string[] args)
{
string xmlstring = #"<?xml version='1.0' encoding='utf-8' ?>
<parameter>
<name>max_amount</name>
<label>Max Amount</label>
<unit>Millions</unit>
<component>
<type>Combo</type>
<attributes>
<type>Integer</type>
<displayed>4</displayed>
<selected>0</selected>
<items>
<item>5</item>
<item>10</item>
<item>20</item>
<item>50</item>
</items>
</attributes>
</component >
</parameter>";
XmlSerializer deserializer = new XmlSerializer(typeof(Parameter));
XmlReader reader = XmlReader.Create(new StringReader(xmlstring));
Parameter parameter = (Parameter)deserializer.Deserialize(reader);
Console.WriteLine("Type: {0}", parameter.Component.Attributes.Type);
Console.WriteLine("Displayed: {0}", parameter.Component.Attributes.Displayed);
Console.WriteLine("Selected: {0}", parameter.Component.Attributes.Selected);
Console.WriteLine("Items: ");
foreach (var item in parameter.Component.Attributes.Items)
{
Console.WriteLine("\t{0}", item);
}
Console.ReadLine();
}
}
Note small change in xmlstring, now every <item></item> is inside container:
<items>
<item>5</item>
<item>10</item>
<item>20</item>
<item>50</item>
</items>

Deserializing an XML document with the root being a list

I have an XML document provided to me externally that I need to import into my application. The document is badly written, but not something I can really do much about.
<?xml version="1.0" encoding="iso-8859-1"?>
<xml>
<Items>
<Property1 />
<Property2 />
...
</Items>
<Items>
<Property1 />
<Property2 />
...
</Items>
...
</xml>
How should I go about using the XmlSerializer for this?
It doesn't seem to matter what class setup I use, or wether I put XmlRoot(ElementName="xml") on my base class it always says that the <xml xmlns=''> is unexpected.
Edit: Added the C# code I am using
[XmlRoot(ElementName = "xml")]
public class Container
{
public List<Items> Items { get; set; }
}
public class Items
{
public short S1 { get; set; }
public short S2 { get; set; }
public short S3 { get; set; }
public short S4 { get; set; }
public short S5 { get; set; }
public short S6 { get; set; }
public short S7 { get; set; }
}
public class XMLImport
{
public Container Data{ get; private set; }
public static XMLImport DeSerializeFromFile(string fileName)
{
XMLImport import = new XMLImport();
XmlSerializer serializer = new XmlSerializer(typeof(Container));
using (StreamReader reader = new StreamReader(fileName))
import.Data = (Container)serializer.Deserialize(reader);
return import;
}
}
Say you have a class Items maps to each <Items> node:
public class Items
{
public string Property1 { get; set; }
public string Property2 { get; set; }
}
You can deserialize your list of Items like this:
var doc = XDocument.Parse(
#"<?xml version=""1.0"" encoding=""iso-8859-1""?>
<xml>
<Items>
<Property1 />
<Property2 />
</Items>
<Items>
<Property1 />
<Property2 />
</Items>
</xml>");
var serializer = new XmlSerializer(typeof(List<Items>), new XmlRootAttribute("xml"));
List<Items> list = (List<Items>)serializer.Deserialize(doc.CreateReader());
The root of your XML is not a List, the root of your xml is the <xml> node I think you are just confused by its name :)
please visit the following link, It has many good answers voted by many people.
Here is the link: How to Deserialize XML document
Error Deserializing Xml to Object - xmlns='' was not expected
Simply take off the Namespace =:
[XmlRoot("xml"), XmlType("xml")]
public class RegisterAccountResponse {...}

Categories