本文介绍了反序列化时使用httpclient的Xml命名空间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个返回结果的api: < 信封:信封 xmlns:信封 = http://xxx.com/data-hub/envelope > < 信封:标题 > < additionalInfo > < Rebates / > < 费用 > 930879969 < /费用 > < 月份 > 20 < / Months > < / additionalInfo > < / envelope:header > < / envelope > 我遇到问题: 第1行位置错误76.期望命名空间'http'中的元素'Envelope': //schemas.datacontract.org/2004/07/' ..遇到名为'envel'的'元素' ope',namespace'http://xxx.com/data-hub/envelope'。 我的尝试: 使用(HttpClient client = new HttpClient()) { var responseTask = await client.GetAsync(uri); responseTask.Wait(); var result = responseTask.Result; if (result.IsSuccessStatusCode) { var readTask = result.Content.ReadAsAsync< T>(); readTask.Wait(); response = readTask.Result; } 我在ReadAsynch收到错误< t> 我发送的T类是: [XmlRoot(ElementName = envelope,Namespace = http://xxx.com/data-hub/envelope)] public class Envelope { [XmlElement(ElementName = header,Namespace = http://xxx.com/data-hub/envelope) ] public 标题标题{ get ; set ; } [XmlAttribute(AttributeName = envelope,Namespace = http://www.w3.org/2000/xmlns/)] public string _envelope { get ; set ; } } public class 标题 { [XmlElement( ElementName = additionalInfo)] public AdditionalInfo AdditionalInfo { get ; set ; } } public class AdditionalInfo { [XmlElement(ElementName = 回扣)] public string 回扣{获取; set ; } [XmlElement(ElementName = 费用)] public string 费用{ get ; set ; } [XmlElement(ElementName = 月份)] public string 月份{ get ; set ; } } 解决方案 正如PIEBALDconsult所提到的,关闭时XML中至少有一个错误标签: < 信封:信封 xmlns:envelope = http://xxx.com/data-hub/envelope > < 信封:标题 > < additionalInfo > < Rebates / > < 费用 > 930879969 < /费用 > < 月份 > 20 < /月 > < / additionalInfo > < / envelope:header > < / envelope:envelope > 另外,你正在使用混合命名空间。有两种解决方案: 1. 在开头元素中添加一个额外的命名空间定义: < 信封:信封 xmlns:envelope = http:// xxx.com/data-hub/envelope\" xmlns = http://xxx.com/data-hub/信封 > < 信封:标题 > < additionalInfo > < Rebates / > < 费用 > 930879969 < /费用 > < 月份 > 20 < / Months > < / additionalInfo > < / envelope:header > < / envelope:envelope > 2。所有元素都具有相同的命名空间: < 信封:信封 xmlns:envelope = http://xxx.com/data-hub/envelope\"> < 信封:标题 > < envelope:additionalInfo > < envelope:Rebates / > < 信封:费用 > 930879969 < / envelope:费用 > < zh-CN velope:月 > 20 < / envelope:Months > < / envelope:additionalInfo > < / envelope:header > < / envelope:envelope > 最后, Envelope 类应该是: [XmlRoot(ElementName = envelope,Namespace = http://xxx.com/data-hub/envelope)] public 类信封 { [XmlElement(ElementName = header )] public 标题标题{ get ; set ; } } 以下是用于测试的其他类的一致性: public class 标题 { [XmlElement(ElementName = additionalInfo)] public AdditionalInfo AdditionalInfo {获取; set ; } } public class AdditionalInfo { [XmlElement(ElementName = Rebates)] public string 回扣{ get ; set ; } [XmlElement(ElementName = 费用)] public string 费用{ get ; set ; } [XmlElement(ElementName = 月份)] public string 月份{ get ; set ; } } I have a api which returns the result:<envelope:envelope xmlns:envelope="http://xxx.com/data-hub/envelope"><envelope:header><additionalInfo><Rebates/><Expenses>930879969</Expenses><Months>20</Months></additionalInfo></envelope:header></envelope>I am getting issue :Error in line 1 position 76. Expecting element 'Envelope' from namespace 'http://schemas.datacontract.org/2004/07/'.. Encountered 'Element' with name 'envelope', namespace 'http://xxx.com/data-hub/envelope'.What I have tried:using (HttpClient client = new HttpClient()){ var responseTask = await client.GetAsync(uri); responseTask.Wait(); var result = responseTask.Result; if (result.IsSuccessStatusCode) { var readTask = result.Content.ReadAsAsync<t>(); readTask.Wait(); response = readTask.Result; }I am getting error at ReadAsynch<t>The class T i am sending is:[XmlRoot(ElementName = "envelope", Namespace = "http://xxx.com/data-hub/envelope")]public class Envelope{ [XmlElement(ElementName = "header", Namespace = "http://xxx.com/data-hub/envelope")] public Header Header { get; set; } [XmlAttribute(AttributeName = "envelope", Namespace = "http://www.w3.org/2000/xmlns/")] public string _envelope { get; set; }}public class Header{ [XmlElement(ElementName = "additionalInfo")] public AdditionalInfo AdditionalInfo { get; set; }}public class AdditionalInfo{ [XmlElement(ElementName = "Rebates")] public string Rebates { get; set; } [XmlElement(ElementName = "Expenses")] public string Expenses { get; set; } [XmlElement(ElementName = "Months")] public string Months{ get; set; }} 解决方案 As PIEBALDconsult mentions, there is at least one error in your XML with the closing tag:<envelope:envelope xmlns:envelope="http://xxx.com/data-hub/envelope"><envelope:header><additionalInfo><Rebates/><Expenses>930879969</Expenses><Months>20</Months></additionalInfo></envelope:header></envelope:envelope>Also, you are using mixed namespaces. There are two solutions:1. add an additional namespace definition to the opening element:<envelope:envelope xmlns:envelope="http://xxx.com/data-hub/envelope" xmlns="http://xxx.com/data-hub/envelope"><envelope:header><additionalInfo><Rebates/><Expenses>930879969</Expenses><Months>20</Months></additionalInfo></envelope:header></envelope:envelope>2. All elements have the same namespace:<envelope:envelope xmlns:envelope="http://xxx.com/data-hub/envelope"><envelope:header><envelope:additionalInfo><envelope:Rebates/><envelope:Expenses>930879969</envelope:Expenses><envelope:Months>20</envelope:Months></envelope:additionalInfo></envelope:header></envelope:envelope>Lastly, the Envelope class should be:[XmlRoot(ElementName = "envelope", Namespace = "http://xxx.com/data-hub/envelope")]public class Envelope{ [XmlElement(ElementName = "header")] public Header Header { get; set; }}And here are the other classes, used for testing, for consistancy:public class Header{[XmlElement(ElementName = "additionalInfo")]public AdditionalInfo AdditionalInfo { get; set; }}public class AdditionalInfo{[XmlElement(ElementName = "Rebates")]public string Rebates { get; set; }[XmlElement(ElementName = "Expenses")]public string Expenses { get; set; }[XmlElement(ElementName = "Months")]public string Months { get; set; }} 这篇关于反序列化时使用httpclient的Xml命名空间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-21 23:50