反序列化从Web API的XML数组 [英] Deserializing an XML array from a web api

查看:119
本文介绍了反序列化从Web API的XML数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我跟着答案给这里,但我还是不能,得到下面的工作。

First off, I followed the answer given here, but I still can not get the following to work.

我检索XML从Web API,并返回的结果是这样:

I am retrieving XML from a web API, and the results returned are as such:

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



我试图反序列化为学生的数组这一点。
我的学生类的定义是这样的:

I'm trying to deserialize this into an array of students. My student class is defined like this:

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; }
}

和我的学生收取的定义是这样的:

And my student collection is defined like this:

[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; }
}

我使用此代码反序列化的结果:

I'm deserializing the results using this code:

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 与消息

Once I run this, I get an InvalidOperationException with a message

ArrayOf__ptd_student_charges
的xmlns ='的 http://schemas.datacontract.org/2004/07/something.something '>预计不会
了。

ArrayOf__ptd_student_charges xmlns='http://schemas.datacontract.org/2004/07/something.something'> was not expected.

我知道的xmlns:...不应该在第一个标签,但不幸的是,我不能确定上。如何进行

I know that the xmlns:... shouldn't be in the first tag, but unfortunately it is and I'm unsure on how to proceed.

推荐答案

基本上,你需要支持默认XML命名空间在XML文件 - 你可以的无论是的通过指定其在StudentCollection做到这一点:

Basically, you need to support the default XML namespace in your XML file - you can either do this by specifying it on the 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; }
}

和实际学生类:

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

的您可以在反序列化编程方式指定的:

or you can specify it programmatically when you deserialize:

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

这对的XmlSerializer 第二个参数是默认 XML命名空间来反序列化的XML内容时使用

That second parameter for the XmlSerializer is the default XML namespace to use when deserializing the XML content.

额外TIPP:如果您曾经有一个XML文件再次,你需要得到的C#代码类,代表了XML - 如果你的Visual Studio 2012或更高版本,只需创建一个新的代码类,XML文件复制到剪贴板中,然后用编辑>选择性粘贴>粘贴XML作为类,你会得到所有你的C#包括所有XML属性和XML命名空间,一切都粘贴到Visual Studio中右边有

Extra tipp: if you ever have an XML file again, and you need to get the C# code classes that represent that XML - if you have Visual Studio 2012 or newer, just create a new code class, copy your XML file into the clipboard, and then use Edit > Paste Special > Paste XML as classes and you get all your C# including all XML attribute and XML namespaces and everything pasted into your Visual Studio right there

这篇关于反序列化从Web API的XML数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆