{ user xmlns='' was not expected.} Deserializing Twitter XML
Either decorate your root entity with the XmlRoot attribute which will be used at compile time.
[XmlRoot(Namespace = "www.contoso.com", ElementName = "MyGroupName", DataType = "string", IsNullable=true)]
Or specify the root attribute when de serializing at runtime.
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "user";
// xRoot.Namespace = "http://www.cpandl.com";
xRoot.IsNullable = true;
XmlSerializer xs = new XmlSerializer(typeof(User),xRoot);
Error Deserializing Xml to Object - xmlns='' was not expected
Simply take off the Namespace =
:
[XmlRoot("register-account"), XmlType("register-account")]
public class RegisterAccountResponse {...}
since your xml doesn't seem to be in an xml-namespace. Also, [Serializable]
isn't used by XmlSerializer
.
If your xml was using a namespace it would have an xmlns
at the root.
Also, to help with callers you could add where T : class, new()
(the , new()
being the addition) to your Deserialize
method, since XmlSerializer
demands a public parameterless constructor.
Deserializing Xml to List T - xmlns='' was not expected
You need only two classes:
[XmlType("level")]
public class Level
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("complete")]
public string Complete { get; set; }
[XmlAttribute("stars")]
public string Stars { get; set; }
[XmlAttribute("firstMisson")]
public string FirstMisson { get; set; }
[XmlAttribute("secondMission")]
public string SecondMission { get; set; }
[XmlAttribute("thridMission")]
public string ThridMission { get; set; }
}
[XmlType("location")]
public class Location
{
[XmlElement("level")]
public Level Level { get; set; }
[XmlAttribute("id")]
public string Id { get; set; }
}
The important is to apply XmlType
attribute to the class Location
rather than XmlRoot
.
Now you can deserialize your XML to List<Location>
like this:
var xmlSerializer = new XmlSerializer(typeof(List<Location>),
new XmlRootAttribute("locations"));
List<Location> locations;
using (var stream = File.OpenRead(filepath))
locations = (List<Location>)xmlSerializer.Deserialize(stream);
The trick is to specify the root element name ("locations" in your case) using the XmlSerializer(Type, XmlRootAttribute)
constructor overload.
XML Deserialize rss xmlns='' was not expected
The issue is one of namespaces. The rss
element in your XML is in the default namespace, but the XmlRoot
attribute on googleRss
has a namespace of http://base.google.com/ns/1.0
.
The namespace declaration of xmlns:g="..."
binds a namespace to the prefix g
, but this isn't used anywhere in the snippet of XML in your question.
Remove the Namespace
value from the XmlRoot
attribute:
[XmlRoot(ElementName = "rss")]
public partial class googleRss
And remove the default namespace of rss
from the serializer constructor:
var serializer = new XmlSerializer(typeof(GCatalog.googleRss));
See this fiddle for a demo.
xmlns='' was not expected when deserializing nested classes
After alot of testing I have finally found an error. It was not an encoding problem, neither was the problem in the other code nor it was the missing namespace.
The missing part was the annotation for type of objects in the array when deserializing.
So I had to change my StatusUpdate
class to
[XmlRoot("StatusUpdate")]
public class StatusUpdate
{
public StatusUpdate()
{
}
[XmlArray("Players"), XmlArrayItem(ElementName = "Player", Type = typeof(PlayerInfo))]
public PlayerInfo[] Players { get; set; }
[XmlElement("Command")]
public ServerCommand Update { get; set; }
}
and serialization started working perfectly.
I hope that helps someone else.
xmlns='' was not expected. - There is an error in XML document (2, 2)
Declaring XmlSerializer as
XmlSerializer s = new XmlSerializer(typeof(string),new XmlRootAttribute("response"));
is enough.
Related Topics
Why Is Multiple Inheritance Not Allowed in Java or C#
How Using Try Catch for Exception Handling Is Best Practice
Keeping ASP.NET Session Open/Alive
Casting Interfaces for Deserialization in JSON.Net
Distinct by Property of Class with Linq
Run Async Method Regularly with Specified Interval
Resolving Instances with ASP.NET Core Di from Within Configureservices
How to Create a Simple Proxy in C#
Detect Windows Version in .Net
C# Switch Statement Limitations - Why
Open Existing File, Append a Single Line
Windows Like Services Development in Linux Using Mono
C# Execute a Terminal Command in Linux