【问题标题】:XMLSerializer not deserializing XMLXMLSerializer 不反序列化 XML
【发布时间】:2023-04-05 07:44:01
【问题描述】:

我在尝试反序列化 xml 时收到以下错误。这会产生错误:

XmlSerializer 序列化器 = 新 XmlSerializer(typeof(PrivateOptionsAPIResponse));
var result = serializer.Deserialize(streamReader);

例外:

System.InvalidOperationException 被捕获
Message=XML 文档中存在错误 (0, 0)
内部异常:System.Xml.XmlException
消息=缺少根元素
Source=System.Xml

我不知道如何解决这个问题。请求返回以下 XML:

<PrivateOptionsAPIResponse> <CountiesForPostalCodeResponse> <Counties> <County> <CountyName>PRINCE WILLIAM</CountyName> <StateCode>VA</StateCode> </County> <County> <CountyName>MANASSAS CITY</CountyName> <StateCode>VA</StateCode> </County> <County> <CountyName>MANASSAS PARK CITY</CountyName> <StateCode>VA</StateCode> </County> </Counties> </CountiesForPostalCodeResponse> </PrivateOptionsAPIResponse>

我使用 xsd.exe 来生成一个类。 PrivateOptionsAPIResponse 上的定义(由 xsd.exe 工具生成)显示:

公共部分类 PrivateOptionsAPIResponse {

private object itemField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("CountiesForPostalCodeResponse", typeof(ZipCodeValidationResponse))]
[System.Xml.Serialization.XmlElementAttribute("PlanDetailsForIndividualOrFamilyResponse", typeof(IndividualPlanBenefitResponse))]
[System.Xml.Serialization.XmlElementAttribute("PlansForIndividualOrFamilyResponse", typeof(IndividualPlanQuoteResponse))]
[System.Xml.Serialization.XmlElementAttribute("ProductDetailsForSmallGroupResponse", typeof(SmallGroupProductBenefitResponse))]
[System.Xml.Serialization.XmlElementAttribute("ProductsForSmallGroupResponse", typeof(SmallGroupProductQuoteResponse))]
public object Item {
    get {
        return this.itemField;
    }
    set {
        this.itemField = value;
    }
}

}

如果我随后浏览到 ZipCodeValidationResponse 定义,它会显示:

public partial class ZipCodeValidationResponse {

private CountyType[] countiesField;

/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("County", IsNullable=false)]
public CountyType[] Counties {
    get {
        return this.countiesField;
    }
    set {
        this.countiesField = value;
    }
}

}

如果我随后浏览 CountyType 上的定义,我会看到:

public partial class CountyType {

private string countyNameField;

private StateAbbreviationType stateCodeField;

/// <remarks/>
public string CountyName {
    get {
        return this.countyNameField;
    }
    set {
        this.countyNameField = value;
    }
}

/// <remarks/>
public StateAbbreviationType StateCode {
    get {
        return this.stateCodeField;
    }
    set {
        this.stateCodeField = value;
    }
}

}

---------工作解决方案----------------:

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            string status = ((HttpWebResponse)response).StatusDescription;

            if(status == "OK")
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader reader = new StreamReader(responseStream))
                    {
                        var xmlSerializer = new XmlSerializer(typeof(PrivateOptionsAPIResponse));
                        var privateOptionsAPIResponse = xmlSerializer.Deserialize(reader) as PrivateOptionsAPIResponse;
                    }
                }                    
            }
        }

【问题讨论】:

  • 假设阅读器内容已通过验证,您可能必须使用“streamReader.MoveToContent()”强制阅读器到其内容位置。

标签: c# xml xsd xml-serialization


【解决方案1】:

你是如何声明你的streamReader的?查看它的内容,您很可能会发现它是空的或不包含完整的 XML 文档。

【讨论】:

  • 像这样声明streamRader:使用(var streamReader = new StreamReader(response.GetResponseStream()))
  • 您的评论使我重新审视了我的声明方式。我已经用可行的解决方案更新了我的原始帖子。谢谢。
【解决方案2】:

首先,检查您的 XML 格式是否有效。从给定的异常来看,您似乎提供了无效的 xml 文档。如果您能发布您尝试反序列化的内容 (xml),将不胜感激。

【讨论】:

  • 在 StreamReader 上运行: var xdoc = XDocument.Parse(streamReader.ReadToEnd());返回我原始帖子中的字符串 (XML)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-06-14
  • 2020-12-09
  • 2015-09-12
相关资源
最近更新 更多