Having problems deserializing some xml into an object in C#.
The error that I receive is...
xmlns=\'\'> was not expected.
The XS
Without a full xsd / xml, or (alternatively) your C# classes, we can't reproduce. But working from the xml upwards, this works fine for me; meaning: the error is not (as far as I can see) in the code/data you posted. Can you post a more complete (reproducible) example?
public class ListeAvisRemboursements
{
private readonly List<AvisRemboursement> items = new List<AvisRemboursement>();
[XmlElement("AvisRemboursement", Namespace = "xml.AAAAAAA.com/commerce/apres-vente_technique/assistance")]
public List<AvisRemboursement> Items { get { return items; } }
}
public class AvisRemboursement
{
[XmlAttribute] public string NumeroDT {get;set;}
[XmlAttribute] public string CodeRA {get;set;}
[XmlAttribute] public string NumeroDC {get;set;}
public DateTime DateTraitement { get; set; }
public decimal MontantDC { get; set; }
public decimal MontantMO { get; set; }
public decimal SommeAD { get; set; }
public decimal MontantPR { get; set; }
public decimal SommePR { get; set; }
public decimal FraisGestion { get; set; }
public int NombreHeuresTotalRemboursees { get; set; }
public string Etat { get; set; }
public string NoteCredit { get; set; }
public string Imputation { get; set; }
}
static void Main()
{
var ser = new XmlSerializer(typeof(ListeAvisRemboursements));
var wrapper = (ListeAvisRemboursements)ser.Deserialize(new StringReader(xml));
// inspect wrapper.Items etc
}
also works fine with:
var ser = new XmlSerializer(typeof(ListeAvisRemboursements));
using (var reader = XmlReader.Create("inputfile.xml"))
{
var wrapper = (ListeAvisRemboursements)ser.Deserialize(reader);
}
and:
XmlDocument _Doc = new XmlDocument();
_Doc.Load("inputfile.xml");
var ser = new XmlSerializer(typeof(ListeAvisRemboursements));
var wrapper = (ListeAvisRemboursements)ser.Deserialize(new StringReader(_Doc.OuterXml));
and
XmlDocument _Doc = new XmlDocument();
_Doc.Load("inputfile.xml");
var ser = new XmlSerializer(typeof(ListeAvisRemboursements));
var wrapper = (ListeAvisRemboursements)ser.Deserialize(new XmlNodeReader(_Doc.DocumentElement));
Here's what I am using (sorry am kind of late to the party):
Public Function Serialize(Of YourXMLClass)(ByVal obj As YourXMLClass,
Optional ByVal omitXMLDeclaration As Boolean = True,
Optional ByVal omitXMLNamespace As Boolean = True) As String
Dim serializer As New XmlSerializer(obj.GetType)
Using memStream As New MemoryStream()
Dim settings As New XmlWriterSettings() With {
.Encoding = Encoding.UTF8,
.Indent = True,
.OmitXmlDeclaration = omitXMLDeclaration}
Using writer As XmlWriter = XmlWriter.Create(memStream, settings)
Dim xns As New XmlSerializerNamespaces
If (omitXMLNamespace) Then xns.Add("", "")
serializer.Serialize(writer, obj, xns)
End Using
Return Encoding.UTF8.GetString(memStream.ToArray())
End Using
End Function
Public Function Deserialize(Of YourXMLClass)(ByVal obj As YourXMLClass, ByVal xml As String) As YourXMLClass
Dim result As YourXMLClass
Dim serializer As New XmlSerializer(GetType(YourXMLClass))
Using memStream As New MemoryStream()
Dim bytes As Byte() = Encoding.UTF8.GetBytes(xml.ToArray)
memStream.Write(bytes, 0, bytes.Count)
memStream.Seek(0, SeekOrigin.Begin)
Using reader As XmlReader = XmlReader.Create(memStream)
result = DirectCast(serializer.Deserialize(reader), YourXMLClass)
End Using
End Using
Return result
End Function