views:

27

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<Data> list);
}

Where data defines a data contract

[DataContract]
public class Data
{
 public string Name { get; set; }
 public List<MyParameter> 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 helpful thanks

A: 

Add:

   writer.WriteAttributeString("xmlns","xsi", null,@"http://w3.org/2001/xmlschema-instance");
Nix
thanks for the replythe fisrst method works but it not sutable for mehow do i do the seconed method?i dont understand what to change?
asdas
Why is the first not suitable?
Nix
it causes a problem somwhere lse in the code (not related to wcf)
asdas
What problem is is causing...
Nix
its hard for me to explain that problem.... I tried setting the schema with the xsi for MyParameter and that didnt help..
asdas
Maybe you should explain your end goal. What are you trying to solve with this example? Custom serialization? Because there are better ways to mark up "DataContracts"
Nix