我有一个小的wcf服务器,它接收带有XML的POST请求(注意我没有控制XML)。除非它有xsi:type =“something”属性,否则我可以毫无问题地反序列化它。
当我尝试序列化我的类时,everthing工作(甚至是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“”,则everthing有效。
以下是我要求序列化类时服务器发送的内容:
<?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 = "";
}
}
请帮帮我:(
答案 0 :(得分:0)
试试这堂课:
[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));