Reputation: 460
For 3 days I've been facing the same problem and I can't figure out what I am doing wrong.
Context: I am creating a new WCF service for a fixed XML.
Problem: it looks like the deserialisation of the XML goes wrong. I do get the data
object back but without the property items
filled.
Tried so far:
Adding multiple attributes like:
[ServiceContract(Namespace = "urn:oasis:names:tc:SPML:2:0"), XmlSerializerFormat].
[System.Xml.Serialization.XmlArrayItemAttribute("attr", IsNullable = false)]
to the Items
property
This is the xml I am going to recieve:
<spml:data xmlns:spml="urn:oasis:names:tc:SPML:2:0">
<attr name="mailone" xmlns="urn:oasis:names:tc:DSML:2:0:core">
<value>[email protected]</value>
</attr>
<attr name="mailtwo" xmlns="urn:oasis:names:tc:DSML:2:0:core">
<value>[email protected]</value>
</attr>
<attr name="mailthree" xmlns="urn:oasis:names:tc:DSML:2:0:core">
<value>[email protected]</value>
</attr>
</spml:data>
With the xsd (4.0 currently used in the wcf project for other objects) I get this c# class from the xsd:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.33440.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:tc:SPML:2:0")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:oasis:names:tc:SPML:2:0", IsNullable = false)]
public partial class data
{
private attr[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("attr")]
public attr[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:oasis:names:tc:SPML:2:0")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:oasis:names:tc:SPML:2:0", IsNullable = false)]
public partial class attr
{
private string valueField;
private string nameField;
/// <remarks/>
public string value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
}
I wrote this program to simplify the mapping problem:
using System;
using System.Xml;
using System.Xml.Serialization;
namespace XmlDeserializer
{
class Program
{
static void Main(string[] args)
{
XmlSerializer ser = new XmlSerializer(typeof(data));
data data;
using (XmlReader reader = XmlReader.Create(PATH))
{
data = (data)ser.Deserialize(reader);
}
}
}
}
If you run this console application with the C# class in the same project and debug on data you will get a data object back with items = null
.
Can someone had me in the right direction?
EDIT: it has to do with the namespaces: I removed all the namespaces in the XML and c# object and it worked.
Kind regards, Pieter
Upvotes: 0
Views: 600
Reputation: 34429
The following code works. I changed the namespaces :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication137
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(data));
data d = (data)serializer.Deserialize(reader);
}
}
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:oasis:names:tc:SPML:2:0", IsNullable = false)]
public partial class data
{
private attr[] itemsField;
/// <remarks/>
[XmlElement(ElementName = "attr", Namespace = "urn:oasis:names:tc:DSML:2:0:core")]
public attr[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:oasis:names:tc:DSML:2:0:core", IsNullable = false)]
public partial class attr
{
private string valueField;
private string nameField;
/// <remarks/>
public string value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
}
}
Upvotes: 1