- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试反序列化从 Web 服务接收到的 SoapException 消息。我收到以下异常:
System.InvalidOperationException was unhandled
Message="There is an error in XML document (2, 2)."
Source="System.Xml"
StackTrace:
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
at FullServiceServices.AddressCorrectionService.GetResponse(FullServiceAddressCorrectionQueryRequest request) in AddressCorrectionService.cs:line 169
at FullServiceServices.AddressCorrectionService.GenerateRequest(DateTime startDate, DateTime endDate) in AddressCorrectionService.cs:line 79
at FullServiceServices.Form1.Form1_Load(Object sender, EventArgs e) in \Form1.cs:line 33
InnerException: System.InvalidOperationException
Message="<Fault xmlns='http://idealliance.org/maildat/Specs/md091/mailxml81/mailxml'> was not expected."
Source="gn4ggxxc"
StackTrace:
at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderFault.Read3_Fault()
如何将此 XML 正确反序列化为提供的分部类 Fault?
下面是我如何将 XML 字符串读入 XMLSerializer:
XmlTextReader xr = new XmlTextReader(new StringReader(soapEx.Detail.FirstChild.InnerText));
XmlSerializer xs = new XmlSerializer(typeof(Fault));
Fault fault = xs.Deserialize(xr) as Fault;
这是 soap 异常 .Detail 属性的 FirstChild 的 InnerText
<?xml version="1.0" encoding="UTF-8"?>
<mailxml:Fault xmlns:mailxml="http://idealliance.org/maildat/Specs/md091/mailxml81/mailxml" xmlns:mailxml_base="http://idealliance.org/maildat/Specs/md091/mailxml81/base" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://idealliance.org/maildat/Specs/md091/mailxml81/mailxml mailxml_031910.xsd http://idealliance.org/maildat/Specs/md091/mailxml81/base mailxml_base_031910.xsd">
<mailxml:FaultCode>402</mailxml:FaultCode>
<mailxml:FaultDescription>402 Not Well Formed XML</mailxml:FaultDescription>
</mailxml:Fault>
这是通过 WSDL 自动生成的部分类。
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.3082")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://idealliance.org/maildat/Specs/md091/mailxml81/mailxml")]
public partial class Fault {
private string[] faultCodeField;
private string[] faultDescriptionField;
private string trackingIDField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("FaultCode")]
public string[] FaultCode {
get {
return this.faultCodeField;
}
set {
this.faultCodeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("FaultDescription")]
public string[] FaultDescription {
get {
return this.faultDescriptionField;
}
set {
this.faultDescriptionField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form=System.Xml.Schema.XmlSchemaForm.Qualified)]
public string TrackingID {
get {
return this.trackingIDField;
}
set {
this.trackingIDField = value;
}
}
}
谢谢!
最佳答案
在 RootElement 上设置 namespace 就可以了
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.Namespace = "http://namespacehere";
XmlReader xr = soapEx.Detail.FirstChild.CreateNavigator().ReadSubtree();
XmlSerializer xs = new XmlSerializer(typeof(Fault), xRoot);
Fault fault = xs.Deserialize(xr) as Fault;
关于c# - XmlSerializer - 反序列化 SoapException 详细元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4597368/
在我的 Visual Studio 2010 项目中,我使用以下 Post-Build 事件命令行使用 sgen 创建 XmlSerializers.dll。 构建后事件: "$(ProgramFil
我用 XMLSerializer 类做了一些事情。像大多数初学者一样,我在应用程序启动时遇到性能问题。我阅读了很多博客、文章,最后使用了 SGEN 工具。现在性能看起来还不错,但我还有一些不清楚的地方
过去 2 天我一直被这个问题困扰,但仍然没有解决,我正在寻求帮助。 我的 Listbox根据在 Combobox 中选择的项目,获取添加到其中的生成项目.当我点击按钮时 Create出现一个带有 We
我正在尝试解决如何生成一些 Xml,它使用 XmlSerializer 的一组类中的前缀不仅仅是微不足道的使用。我还希望能够读取相同的 Xml(这相当简单,但随着我使用更复杂的属性似乎消失了。 具体用
XmlSerializer 很难(= 不)反序列化包含表情符号字符的内容,例如 ��。我读过这样的字符在 XML 标准中实际上是非法的;但是,如果我想忠实地表示包含表情符号的聊天对话,则需要它们。如何
我将 XmlSerializer 用于 WCF 服务(就我的服务而言,这是有原因的)。但是最近我遇到了这个问题:我找不到一种简单的方法来使引用类型属性成为必需的,即使其在 XSD 中的定义如下所示:
假设您有两个类,一个继承另一个类,并且子类需要使用 XmlSerializer 进行序列化/反序列化。但是,父级包含一个不可序列化的成员,例如字典。 public class Parent {
我正在尝试使用 XmlSerializer 解析具有如下所示元素的 XML。 amount 元素下有相当多的货币类型,我想将它们反序列化为一个对象集合,这些对象具有保存货币类型的字符串属性和保存金额的
我正在用 Javascript 生成 KML 文档,并尝试使用 XMLSerializer生成 XML 文件,但它生成所有小写标签,即使我在 DOM 中以大写形式创建标签。 是 DOM 破坏了大小写还
我有一小段代码可以将 XML 解析为 JSON。它工作得很好,直到我引入 XMLSerializer 来获取 JSON。 public static String convertXMLFileToSt
假设您有两个类,一个继承另一个,子类需要使用 XmlSerializer 进行序列化/反序列化。但是,父级包含一个不可序列化的成员,比如字典。 public class Parent { pu
我正在尝试使用 XmlSerializer 解析具有如下所示元素的 XML。 amount 元素下有相当多的货币类型,我想将它们反序列化为一个对象集合,这些对象具有保存货币类型的字符串属性和保存金额的
我正在尝试将继承属性的所有成员序列化 (XmlSerializer) 作为特性。 两个类: public class Tree { public Point Location { get; s
我在使用 XmlSerializer 时遇到了修剪字符串的问题, 当使用 XmlReader 时。 IgnoreWhitespace 选项显示无效,元素字符串仍包含\n 和空格。 有什么方法可以“即时
我有以下扩展方法来序列化我的类.... public static string SerializeToXml(this object obj) {
我正在尝试反序列化以下 XML(摘录): Waking The Demon Bullet For My Valentine false B
我在使用 .NET 中的 XmlSerializer 时遇到了一些问题。 这里我有一个我刚刚建立的小例子。 (也可用 @ gist https://gist.github.com/2d84be9041
我从 DTD(通过 XSD 和 xsd.exe)为我的 C# 项目创建了类,因此我可以轻松地将它们反序列化为代码中的模型类。 我大致是这样的: XmlReaderSettings readerSett
我使用 XSD.EXE 将 XSD 转换为对象。这很好用,我可以很好地使用 XMLSerializer 反序列化,除了作为数组生成的子元素不会填充。 private SubElements[]
对不起我的英语。我有 XmlSerializer 的奇怪之处。这是我的代码 [Serializable] public class Radio { public bool hasSubWoof
我是一名优秀的程序员,十分优秀!