[英].net / wcf: deserialization of an xml with xsi:type
我有一个WCF服务器,它可以接收带有XML的POST请求(请注意,我无法控制XML)。 除非它具有xsi:type =“ something”属性,否则我可以对其进行反序列化。
当我尝试序列化我的类时,一切都会起作用(甚至xsi:type属性)。
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>
当我发送XML时,服务器抛出一个错误的请求(400),但是如果我删除了“ xsi:type =” SOMETHING“”,一切都会正常。
这是我要求序列化类时服务器发送的内容:
<?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>
如果序列化工作良好,为什么反序列化不能呢?
这是我的课:
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 = "";
}
}
请帮我 :(
试试这个课程:
[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 = "";
}
我的代码工作正常
XmlSerializer ser = new XmlSerializer(typeof(SOMETHING));
SOMETHING t = (SOMETHING)ser.Deserialize(new System.IO.StringReader(textBox1.Text));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.