2015-11-03 131 views
1

首先,我遵循給定的答案here,但我仍然無法獲得以下工作。從Web api反序列化XML數組

我從一個Web API檢索XML,並返回的結果是這樣:

<ArrayOf__ptd_student_charges 
    xmlns="http://schemas.datacontract.org/2004/07/something.something" 
    xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> 
    <__ptd_student_charges> 
     <accumulated_tuition>000.000</accumulated_tuition> 
     <course_id>AAA-000/L</course_id> 
     <invoice_date>01/01/2015</invoice_date> 
     <lecturer_name>John Doe</lecturer_name> 
     <net_tuition>000.000</net_tuition> 
     <section_no>1</section_no> 
     <semester>Summer</semester> 
     <student_id>123456</student_id> 
     <student_name>John Doe</student_name> 
     <year>2015</year> 
    </__ptd_student_charges> 
    <__ptd_student_charges> 
     <accumulated_tuition>000.000</accumulated_tuition> 
     <course_id>AAA-000/L</course_id> 
     <invoice_date>01/01/2015</invoice_date> 
     <lecturer_name>John Doe</lecturer_name> 
     <net_tuition>000.000</net_tuition> 
     <section_no>1</section_no> 
     <semester>Summer</semester> 
     <student_id>123456</student_id> 
     <student_name>John Doe</student_name> 
     <year>2015</year> 
    </__ptd_student_charges> 
</ArrayOf__ptd_student_charges> 

我試圖反序列化到學生的數組這一點。 我的學生類的定義是這樣的:

public class Student 
{ 
    [System.Xml.Serialization.XmlElement("accumulated_tuiton")] 
    public double AccumulatedTution { get; set; } 

    [System.Xml.Serialization.XmlElement("net_tuiton")] 
    public double NetTuiton { get; set; } 

    [System.Xml.Serialization.XmlElement("course_id")] 
    public string CourseID { get; set; } 

    [System.Xml.Serialization.XmlElement("invoice_date")] 
    public DateTime InvoiceDate { get; set; } 

    [System.Xml.Serialization.XmlElement("lecturer_name")] 
    public string LecturerName { get; set; } 

    [System.Xml.Serialization.XmlElement("semester")] 
    public string Semester { get; set; } 

    [System.Xml.Serialization.XmlElement("student_id")] 
    public string StudentId { get; set; } 

    [System.Xml.Serialization.XmlElement("student_name")] 
    public string StudentName { get; set; } 

    [System.Xml.Serialization.XmlElement("year")] 
    public string Year { get; set; } 

    [System.Xml.Serialization.XmlElement("section_no")] 
    public int Section { get; set; } 
} 

而我的學生收取的定義是這樣的:

[System.Xml.Serialization.XmlRoot("ArrayOf__ptd_student_charges xmlns=\"http://schemas.datacontract.org/2004/07/something.something\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance")] 
public class StudentCollection 
{ 
    [XmlArray("ArrayOf__ptd_student_charges")] 
    [XmlArrayItem("__ptd_student_charges", typeof(Student))] 
    public Student[] StudentArray { get; set; } 
} 

我使用這個代碼反序列化的結果:

private void MainWindow_Loaded(object sender, RoutedEventArgs e) 
{ 
    StudentCollection collection; 

    HttpWebRequest request = WebRequest.Create(stringUrl) as HttpWebRequest; 
    HttpWebResponse response = request.GetResponse() as HttpWebResponse; 

    XmlTextReader reader = new XmlTextReader(response.GetResponseStream()); 

    XmlSerializer serializer = new XmlSerializer(typeof(StudentCollection)); 
    collection = (StudentCollection)serializer.Deserialize(reader); 

    reader.Close(); 
} 

一旦我運行這個,我得到一個消息InvalidOperationException

ArrayOf__ptd_student_charges xmlns ='http://schemas.datacontract.org/2004/07/something.something'> 不是預期的。

我知道xmlns:...不應該在第一個標籤中,但不幸的是,我不確定如何繼續。

回答

1

基本上,你需要支持默認XML命名空間在XML文件 - 你可以要麼通過指定它的StudentCollection做到這一點:

[System.Xml.Serialization.XmlRoot("ArrayOf__ptd_student_charges")] 
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.datacontract.org/2004/07/something.something", IsNullable = false)] 
public class StudentCollection 
{ 
    [XmlArray("ArrayOf__ptd_student_charges")] 
    [XmlArrayItem("__ptd_student_charges", typeof(Student))] 
    public Student[] StudentArray { get; set; } 
} 

和實際Student類:

[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.datacontract.org/2004/07/something.something", IsNullable = false)] 
public class Student 
{ 
    .......... 
} 

您可以在反序列化時以編程方式指定它:

XmlSerializer serializer = new XmlSerializer(typeof(StudentCollection), 
              "http://schemas.datacontract.org/2004/07/something.something"); 

XmlSerializer的第二個參數是默認反序列化XML內容時要使用的XML名稱空間。

額外TIPP:,如果你有一個XML文件再次,你需要獲得C#代碼的類,它表示XML - 如果你的Visual Studio 2012或更高版本,只需要創建一個新的代碼類,複製您的將XML文件導入到剪貼板中,然後使用Edit > Paste Special > Paste XML as classes,然後獲取所有C#,包括所有XML屬性和XML命名空間以及粘貼到您的Visual Studio中的所有內容。

+1

謝謝!這兩個解決方案都有效,但是第一個我必須在同一行定義屬性,否則編譯器會抱怨重複屬性,如下所示:[XmlRoot(ElementName =「ArrayOf__ptd_student_charges」,Namespace =「http:// schemas。 datacontract.org/2004/07/something.something「)] – kskyriacou

+0

偉大的提示,真的很有幫助,太糟糕了我不能投票兩次! – kskyriacou