简体   繁体   中英

.net / wcf: deserialization of an xml with xsi:type

I have a little wcf server wich receive POST request with a XML (note that I HAVE NO CONTROL OVER THE XML). I can deserialize it with no problem unless it has a xsi:type = "something" attribute.

When I try to serialize my class, everthing works (even the xsi:type attribute).

The XML:

<?xml version="1.0" encoding="utf-8"?>
<Node1 Att1="" Att2=""
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:type="SOMETHING" 
    xmlns="http://www.CIP4.org/JDFSchema_1_1"  
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Node2/>
</Node1>

The server throw a bad request (400) when I send this XML, but if I remove the "xsi:type="SOMETHING"", everthing works.

Here is what the server send when i ask for the serialised class:

<?xml version="1.0" encoding="utf-8"?>
<Node1 Att1="" Att2="" xsi:type="SOMETHING"
    xmlns="http://www.CIP4.org/JDFSchema_1_1" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Node2/>
</Node1>

If the serialization works well, why the deserialization don't?

Here is my class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Xml.Serialization;
using System.Xml.Schema;

namespace ConsoleApplicationTest.dom
{

[Serializable()]
[XmlRoot(ElementName = "Node1", Namespace = "http://www.CIP4.org/JDFSchema_1_1")]
public class Test
{
    //attributes
    [XmlAttribute("Att1")]
    public string Att1 = "";
    [XmlAttribute("Att2")]
    public string Att2 = "";

    [XmlAttribute("type", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public string Type="typetypetype";



    [XmlElement("Node2")]
    public string node2 = "";


}




}

Please help me :(

Try this class:

[Serializable()]
[XmlRoot(ElementName = "Node1", Namespace = "http://www.CIP4.org/JDFSchema_1_1")]
public class SOMETHING
{
    //attributes
    [XmlAttribute("Att1")]
    public string Att1 = "";
    [XmlAttribute("Att2")]
    public string Att2 = "";

    [XmlElement("Node2")]
    public string node2 = "";


}

My code works fine

   XmlSerializer ser = new XmlSerializer(typeof(SOMETHING));

    SOMETHING t = (SOMETHING)ser.Deserialize(new System.IO.StringReader(textBox1.Text));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM