将XML反序列化为对象会使所有内容都为null

时间:2016-02-25 15:25:15

标签: c# xml xsd

我正在编写一个C#库来使用名为PubMed的服务。这是一项由美国国立卫生研究院(NIH)运营的公共服务机构。

我使用的服务网址是here

所以我把DTD拉下来,用它来使用VS2010生成XSD。然后我使用xsd.exe生成一个C#类(下面)。

using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Xml.Serialization;

namespace PubMedSearch
{
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:4.0.30319.42000
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------

// 
// This source code was auto-generated by xsd, Version=4.0.30319.1.
// 


/// <remarks />
[GeneratedCode("xsd", "4.0.30319.1")]
[Serializable]
[DebuggerStepThrough]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://tempuri.org/esearch")]
[XmlRoot(Namespace = "http://tempuri.org/esearch", IsNullable = false)]
public class eSearchResult
{
    private ErrorList errorListField;

    private ItemsChoiceType[] itemsElementNameField;

    private object[] itemsField;

    private WarningList warningListField;

    /// <remarks />
    [XmlElement("Count", typeof (string))]
    [XmlElement("ERROR", typeof (string))]
    [XmlElement("IdList", typeof (IdList))]
    [XmlElement("QueryKey", typeof (string))]
    [XmlElement("QueryTranslation", typeof (string))]
    [XmlElement("RetMax", typeof (string))]
    [XmlElement("RetStart", typeof (string))]
    [XmlElement("TranslationSet", typeof (TranslationSet))]
    [XmlElement("TranslationStack", typeof (TranslationStack))]
    [XmlElement("WebEnv", typeof (string))]
    [XmlChoiceIdentifier("ItemsElementName")]
    public object[] Items
    {
        get { return itemsField; }
        set { itemsField = value; }
    }

    /// <remarks />
    [XmlElement("ItemsElementName")]
    [XmlIgnore]
    public ItemsChoiceType[] ItemsElementName
    {
        get { return itemsElementNameField; }
        set { itemsElementNameField = value; }
    }

    /// <remarks />
    public ErrorList ErrorList
    {
        get { return errorListField; }
        set { errorListField = value; }
    }

    /// <remarks />
    public WarningList WarningList
    {
        get { return warningListField; }
        set { warningListField = value; }
    }
}

/// <remarks />
[GeneratedCode("xsd", "4.0.30319.1")]
[Serializable]
[DebuggerStepThrough]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://tempuri.org/esearch")]
[XmlRoot(Namespace = "http://tempuri.org/esearch", IsNullable = false)]
public class IdList
{
    private string[] idField;

    /// <remarks />
    [XmlElement("Id")]
    public string[] Id
    {
        get { return idField; }
        set { idField = value; }
    }
}

/// <remarks />
[GeneratedCode("xsd", "4.0.30319.1")]
[Serializable]
[DebuggerStepThrough]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://tempuri.org/esearch")]
[XmlRoot(Namespace = "http://tempuri.org/esearch", IsNullable = false)]
public class TranslationSet
{
    private Translation[] translationField;

    /// <remarks />
    [XmlElement("Translation")]
    public Translation[] Translation
    {
        get { return translationField; }
        set { translationField = value; }
    }
}

/// <remarks />
[GeneratedCode("xsd", "4.0.30319.1")]
[Serializable]
[DebuggerStepThrough]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://tempuri.org/esearch")]
[XmlRoot(Namespace = "http://tempuri.org/esearch", IsNullable = false)]
public class Translation
{
    private string fromField;

    private string toField;

    /// <remarks />
    public string From
    {
        get { return fromField; }
        set { fromField = value; }
    }

    /// <remarks />
    public string To
    {
        get { return toField; }
        set { toField = value; }
    }
}

/// <remarks />
[GeneratedCode("xsd", "4.0.30319.1")]
[Serializable]
[DebuggerStepThrough]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://tempuri.org/esearch")]
[XmlRoot(Namespace = "http://tempuri.org/esearch", IsNullable = false)]
public class TranslationStack
{
    private object[] itemsField;

    /// <remarks />
    [XmlElement("OP", typeof (string))]
    [XmlElement("TermSet", typeof (TermSet))]
    public object[] Items
    {
        get { return itemsField; }
        set { itemsField = value; }
    }
}

/// <remarks />
[GeneratedCode("xsd", "4.0.30319.1")]
[Serializable]
[DebuggerStepThrough]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://tempuri.org/esearch")]
[XmlRoot(Namespace = "http://tempuri.org/esearch", IsNullable = false)]
public class TermSet
{
    private string countField;

    private string explodeField;

    private string fieldField;

    private string termField;

    /// <remarks />
    public string Term
    {
        get { return termField; }
        set { termField = value; }
    }

    /// <remarks />
    public string Field
    {
        get { return fieldField; }
        set { fieldField = value; }
    }

    /// <remarks />
    public string Count
    {
        get { return countField; }
        set { countField = value; }
    }

    /// <remarks />
    public string Explode
    {
        get { return explodeField; }
        set { explodeField = value; }
    }
}

/// <remarks />
[GeneratedCode("xsd", "4.0.30319.1")]
[Serializable]
[XmlType(Namespace = "http://tempuri.org/esearch", IncludeInSchema = false)]
public enum ItemsChoiceType
{
    /// <remarks />
    Count,

    /// <remarks />
    ERROR,

    /// <remarks />
    IdList,

    /// <remarks />
    QueryKey,

    /// <remarks />
    QueryTranslation,

    /// <remarks />
    RetMax,

    /// <remarks />
    RetStart,

    /// <remarks />
    TranslationSet,

    /// <remarks />
    TranslationStack,

    /// <remarks />
    WebEnv
}

/// <remarks />
[GeneratedCode("xsd", "4.0.30319.1")]
[Serializable]
[DebuggerStepThrough]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://tempuri.org/esearch")]
[XmlRoot(Namespace = "http://tempuri.org/esearch", IsNullable = false)]
public class ErrorList
{
    private string[] fieldNotFoundField;

    private string[] phraseNotFoundField;

    /// <remarks />
    [XmlElement("PhraseNotFound")]
    public string[] PhraseNotFound
    {
        get { return phraseNotFoundField; }
        set { phraseNotFoundField = value; }
    }

    /// <remarks />
    [XmlElement("FieldNotFound")]
    public string[] FieldNotFound
    {
        get { return fieldNotFoundField; }
        set { fieldNotFoundField = value; }
    }
}

/// <remarks />
[GeneratedCode("xsd", "4.0.30319.1")]
[Serializable]
[DebuggerStepThrough]
[DesignerCategory("code")]
[XmlType(AnonymousType = true, Namespace = "http://tempuri.org/esearch")]
[XmlRoot(Namespace = "http://tempuri.org/esearch", IsNullable = false)]
public class WarningList
{
    private string[] outputMessageField;

    private string[] phraseIgnoredField;

    private string[] quotedPhraseNotFoundField;

    /// <remarks />
    [XmlElement("PhraseIgnored")]
    public string[] PhraseIgnored
    {
        get { return phraseIgnoredField; }
        set { phraseIgnoredField = value; }
    }

    /// <remarks />
    [XmlElement("QuotedPhraseNotFound")]
    public string[] QuotedPhraseNotFound
    {
        get { return quotedPhraseNotFoundField; }
        set { quotedPhraseNotFoundField = value; }
    }

    /// <remarks />
    [XmlElement("OutputMessage")]
    public string[] OutputMessage
    {
        get { return outputMessageField; }
        set { outputMessageField = value; }
    }
}
}

看起来它与XML不太匹配,但我是新手,所以我试一试。我使用此代码发出请求:

var request = WebRequest.Create(url);
        var response = request.GetResponse();

        var stream = response.GetResponseStream();

        var deserializer = new XmlSerializer(typeof(eSearchResult), new XmlRootAttribute("eSearchResult"));
        var result = (eSearchResult)deserializer.Deserialize(stream);

结果以null属性和字段结束。我真的很喜欢在黑暗中工作。我按照我能够在信中找到的信息并继续获得相同的结果。任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

传入的XML没有在生成的代码中列出的命名空间。向接收到的XML添加命名空间将允许对其进行反序列化。

        var request = WebRequest.Create(url);
        var response = request.GetResponse();

        var stream = response.GetResponseStream();

        var result = new eSearchResult();

        try
        {
            MemoryStream XmlStream = new MemoryStream();
            XmlDocument MyDoc = new XmlDocument();
            MyDoc.Load(stream);

            XmlAttribute AddNamespace = MyDoc.CreateAttribute("xmlns");
            AddNamespace.Value = "http://tempuri.org/esearch";
            MyDoc.LastChild.Attributes.Append(AddNamespace);
            MyDoc.Save(XmlStream);

            XmlStream.Position = 0;

            result = (eSearchResult)deserializer.Deserialize(XmlStream);
        }
        catch (Exception Error)
        { }