2010-01-06 29 views
1

HR-XML 3.0規範提供了WSDL來生成它們的實體。我試圖在他們的文檔中反序列化它們的示例xml,但它不起作用。HR-XML錯誤,試圖反序列化XML示例

 Candidate.CandidateType candidate = null; 
     string path = "c:\\candidate.xml"; 
     XmlSerializer serializer = new XmlSerializer(typeof(Candidate.CandidateType), "http://www.hr-xml.org/3"); 
     StreamReader reader = null; 
     reader = new StreamReader(path); 
     candidate = (Candidate.CandidateType)serializer.Deserialize(reader); 

錯誤我收到:

"<Candidate xmlns='http://www.hr-xml.org/3'> was not expected." 

有什麼建議?

更新:我試着XmlSerializing一個CandidatePerson元素,它看起來像它使用CandidatePersonType而不是CandidatePerson。我想我在這裏做得不對,但...

Candidate.CandidateType第一線(所有自動生成):

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")] 
[System.SerializableAttribute()] 
[System.Diagnostics.DebuggerStepThroughAttribute()] 
[System.ComponentModel.DesignerCategoryAttribute("code")] 
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.hr-xml.org/3")] 
public partial class CandidateType { 

    private IdentifierType2 documentIDField; 

    private IdentifierType2[] alternateDocumentIDField; 

    private string documentSequenceField; 
+0

請顯示'Candidate.CandidateType'類的開頭。 – 2010-01-06 22:59:43

+0

我已經更新了課堂開始的問題。 – Drakarian 2010-01-06 23:13:18

+0

'[XmlType]'屬性僅適用於類型本身,而不適用於類型的元素。你需要提供更多信息。我會舉一個例子。 – 2010-01-07 00:29:02

回答

2

Muahaha我終於明白了!

John Saunders是對的,我需要在XmlSerializer中指定默認名稱空間,但除此之外,我必須指定XmlRootAttribute,因爲我嘗試反序列化的類沒有相同的名稱作爲根元素。

這裏是我的反序列化的HR-XML ProcessCandidate示例代碼:

protected static ImportTest.CandidateService.ProcessCandidateType DeserializeProcessCandidate(string path) 
    { 
     CandidateService.ProcessCandidateType processCandidate = null; 
     XmlRootAttribute root = new XmlRootAttribute("ProcessCandidate"); 
     XmlSerializer serializer = new XmlSerializer(typeof(CandidateService.ProcessCandidateType), new XmlAttributeOverrides(), new Type[0], root, "http://www.hr-xml.org/3"); 
     StreamReader reader = null; 

     try 
     { 
      reader = new StreamReader(path); 
      processCandidate = (CandidateService.ProcessCandidateType)serializer.Deserialize(reader); 
      reader.Close(); 
     } 
     catch (Exception ex) 
     { 
      reader.Close(); 
      throw (new Exception(ex.InnerException.Message)); 
     } 

     return processCandidate; 
    } 

感謝您的幫助約翰!

2

下面是更多的評論,但它太長了,所以我會把它放在這裏。

CandidateType類正確裝飾了XmlType屬性。這是一個適用於類型的屬性,並確定如何在任何生成的XML模式中發出類型。它與恰好具有相同類型的元素上的名稱空間無關。

考慮下面的C#代碼:

public class CandidateType {} 

public class Foo 
{ 
    CandidateType _candidate1; 
    CandidateType _candidate2; 
} 

請注意,你可以有多個相同類型的變量。以同樣的方式,你可以有:

<xs:element name="Candidate1" type="hrx:CandidateType"/> 
<xs:element name="Candidate2" type="hrx:CandidateType"/> 

這些是兩個元素將驗證對相同的類型定義,但是否則是不相關的。如果它們處於相同的XML Schema中,則它們將位於相同的名稱空間中。但如果他們不是?然後,你可以有一個像實例文檔:

<ns1:Candidate1 xmlns:ns1="namespace1" xmlns="http://www.hr-xml.org/3"> ... </ns1:Candidate1> 
<ns2:Candidate2 xmlns:ns2="namespace2" xmlns="http://www.hr-xml.org/3"> ... </ns1:Candidate2> 

你需要做的是指定Candidate元素XML序列化的命名空間。 CandidateType類型在特定名稱空間中的事實不會確定Candidate元素的名稱空間。

+0

好吧,我想我明白你的意思了。我將如何確定Candidate元素的名稱空間? 謝謝! – Drakarian 2010-01-07 18:25:14

+0

我試過使用http://www.hr-xml.org/3作爲XmlSerializer的命名空間,但我得到了同樣的錯誤。 – Drakarian 2010-01-07 18:37:51