2014-10-08 62 views
0

我有一個小的wcf服務器,它將通過XML接收POST請求(請注意,我對XML沒有控制權)。我可以反序列化它,除非它具有xsi:type =「something」屬性。.net/wcf:使用xsi反序列化xml:類型

當我嘗試序列化我的類時,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> 

服務器拋出一個錯誤的請求(400)時,我送這個XML,但如果我刪除 「的xsi:type =」 東西 「」,寄託都起作用。

下面是我點的序列化類是服務器的發送:

<?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

我想原因是'http://www.w3.org/2001/XMLSchema-instance:type'屬性在解串時具有特殊的含義。它應該告訴序列化程序它應該創建映射到這個特定類型的對象。這是你想要的行爲嗎? – Beemen 2014-10-08 14:16:21

+0

我試圖映射到一個類SOMETHING,但它沒有幫助。事實是,我根本不需要這些信息,但是它總是會在我接收到的xml中:/ – itazuka 2014-10-08 14:19:05

+0

如果用[XmlIgnore]標記此Type屬性會有幫助嗎? – Beemen 2014-10-08 14:40:25

回答

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)); 
+0

感謝您的幫助,但服務器只能接收xml/json,我無法從流或字符串反序列化。 我只需要使用我的類來反序列化:/ – itazuka 2014-10-09 06:36:52