[英].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.