tags:

views:

18

answers:

1

I have a type MyParameter that i pass as a parameter to a wcf service [Serializable] public class MyParameter : IXmlSerializable { public string Name { get; set; } public string Value { get; set; } public string Mytype { get; set; }

    #region IXmlSerializable Members

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {
        XElement e = XElement.Parse(reader.ReadOuterXml());
        IEnumerable<XElement> i = e.Elements();
        List<XElement> l = new List<XElement>(i);
        Name = l[0].Name.ToString();
        Value = l[0].Value.ToString();
        Mytype = l[0].Attribute("type").Value.ToString();
    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteStartElement(Name);
        writer.WriteAttributeString("xsi:type", Mytype);
        writer.WriteValue(Value);
        writer.WriteEndElement();
    }

    #endregion
}

the service contract looks like this: [ServiceContract] public interface IOperation { [OperationContract] void Operation(List list); }

where data defines a data contract [DataContract] public class Data { public string Name { get; set; } public List Parameters{ get; set; } }

when i run the service and test it i get rhe exception in readXml of MyParameter "the prefix xsi is not defined" xsi should define the namespace "http://w3.org/2001/xmlschema-instance"

how do i fix the problem

i am very new to this so a sample code will be very very very helpfull thanks

A: 

You have to explicitly tell the XmlWriter what xsi maps to. Try this instead:

writer.WriteAttributeString("xsi", "type", "http://w3.org/2001/xmlschema-instance", MyType);
tomasr
i dont understand can you please explain
asdas
You don't understand what? It's simple, you're assuming that the XmlWriter somehow magically knows what the xsi prefix maps to. It doesn't , so you have to tell it explicitly.Just replace the line where you write the xsi:type attribute with what I provided.
tomasr