Skip to main content
 首页 » 编程设计

c#之HR-XML 错误,试图反序列化 XML 示例

2024年02月24日40telwanggs

HR-XML 3.0 规范提供了 WSDL 来生成它们的实体。我试图在他们的文档中反序列化他们的示例 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." 

有什么建议吗?

更新:我尝试对 CandidatePerson 元素进行 XmlSerializing,看起来它使用 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; 

请您参考如下方法:

下面比较多的是评论,但是太长了,放在这里吧。

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 架构中,那么它们将在同一个命名空间中。但如果他们不是呢?然后你可以有一个像这样的实例文​​档:

<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 Serializer。 CandidateType 类型在特定命名空间中的事实并不能确定 Candidate 元素的命名空间。