Error Deserializing Xml to Object - Xmlns='' Was Not Expected

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 response xmlns='' was not expected

The capitalization of the XML elements and of your classes do not match. You have two options:

  • Rename your classes to match the actual cases.
  • Add attributes to the classes that map to the actual names.

I would recommend the second way. For the Response class, this would look like:

<XmlRoot(ElementName:="response")>
Public Class Response
'...
End Class

You should not need any other attributes because the classes are explicitly stated in the root class.

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.

{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);

Deserializing XML and Getting an Error in XML Document (2, 2)

I think there are a number of issues here, more specifically with the Generated Code for the XML.

In Visual Studio, I created a new Class and copied your XML content and used Edit -> Paste Special -> Paste XML as Classes.

Here's the generated code from that exercise:

[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:us:gov:treasury:irs:msg:irstransmitterstatusrequest")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:us:gov:treasury:irs:msg:irstransmitterstatusrequest", IsNullable = false)]
public partial class ACABulkRequestTransmitterStatusDetailResponse
{

private ACABulkRequestTransmitterResponse aCABulkRequestTransmitterResponseField;

private ACABulkReqTrnsmtStsRespGrpDtl aCABulkReqTrnsmtStsRespGrpDtlField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20")]
public ACABulkRequestTransmitterResponse ACABulkRequestTransmitterResponse
{
get
{
return this.aCABulkRequestTransmitterResponseField;
}
set
{
this.aCABulkRequestTransmitterResponseField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20")]
public ACABulkReqTrnsmtStsRespGrpDtl ACABulkReqTrnsmtStsRespGrpDtl
{
get
{
return this.aCABulkReqTrnsmtStsRespGrpDtlField;
}
set
{
this.aCABulkReqTrnsmtStsRespGrpDtlField = value;
}
}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20", IsNullable = false)]
public partial class ACABulkRequestTransmitterResponse
{

private string transmissionStatusCdField;

private string receiptIdField;

/// <remarks/>
public string TransmissionStatusCd
{
get
{
return this.transmissionStatusCdField;
}
set
{
this.transmissionStatusCdField = value;
}
}

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:common")]
public string ReceiptId
{
get
{
return this.receiptIdField;
}
set
{
this.receiptIdField = value;
}
}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20", IsNullable = false)]
public partial class ACABulkReqTrnsmtStsRespGrpDtl
{

private BulkExchangeFile bulkExchangeFileField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:common")]
public BulkExchangeFile BulkExchangeFile
{
get
{
return this.bulkExchangeFileField;
}
set
{
this.bulkExchangeFileField = value;
}
}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:us:gov:treasury:irs:common")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:us:gov:treasury:irs:common", IsNullable = false)]
public partial class BulkExchangeFile
{

private Include includeField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.w3.org/2004/08/xop/include")]
public Include Include
{
get
{
return this.includeField;
}
set
{
this.includeField = value;
}
}
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2004/08/xop/include")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.w3.org/2004/08/xop/include", IsNullable = false)]
public partial class Include
{

private string hrefField;

/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string href
{
get
{
return this.hrefField;
}
set
{
this.hrefField = value;
}
}
}

Using your Deserialize method and this new Class I was able to successfully parse your XML file.

I would use this Class to compare against your own Generated Code and see where the main differences are. Unless you have scope to replace the Generated Code?

The first one I can spot is that the attributes needs to be amended above the ACABulkRequestTransmitterStatusDetailResponse partial class to:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.4084.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:us:gov:treasury:irs:msg:irstransmitterstatusrequest")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:us:gov:treasury:irs:msg:irstransmitterstatusrequest", IsNullable = false)]

I hope this helps you on your way.

Deserializing Xml to ListT - 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.

xmlns='' was not expected. - There is an error in XML document (2, 2) while DeserializeXml to object

Got it need to fix class

[Serializable, XmlRoot(ElementName = "HotelListResponse", Namespace = "http://v3.hotel.wsapi.ean.com/")]
public class HotelListResponse
{
[XmlElement(ElementName = "customerSessionId", Namespace = "")]
public string customerSessionId { get; set; }
[XmlElement(ElementName = "numberOfRoomsRequested", Namespace = "")]
public int numberOfRoomsRequested { get; set; }
[XmlElement(ElementName = "moreResultsAvailable", Namespace = "")]
public bool moreResultsAvailable { get; set; }
}


Related Topics



Leave a reply



Submit