XML 反序列化仅适用于 xml 中的命名空间

XML deserializing only works with namespace in xml(XML 反序列化仅适用于 xml 中的命名空间)
本文介绍了XML 反序列化仅适用于 xml 中的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让 ServiceStack xml 反序列化工作的最简单方法是当 xml 包含命名空间时.但是,我收到的 xml 不包含命名空间.最简单的工作示例:

The most simple way I get ServiceStack xml deserialization to work is when the xml contains a namespace. However, the xml I receive do not contain namespaces. The most simple working example:

[Serializable]
public class test
{

}

class Program
{
   static void Main(string[] args)
   {
       string xml="<test xmlns="http://schemas.datacontract.org/2004/07/"></test>";
       var result = ServiceStack.Text.XmlSerializer.DeserializeFromString<test>(xml);
   }
}

然而,这不是我想要的.我希望反序列化以下内容,因为这是我从多个服务中获得的 xml:

However, that is not what I want. I want the following to deserialize, since that is the xml I get from several services:

string xml="<test></test>";

但这给了我以下错误:

DeserializeDataContract: Error converting type: Error in line 1 position 7. 
Expecting element 'test' from namespace 
'http://schemas.datacontract.org/2004/07/'.. 
Encountered 'Element'  with name 'test', namespace ''.

我试过了:

[Serializable]
[XmlRoot("test", Namespace = "")]
public class test

我无法创建新的序列化器,因为 ServiceStack.Text.XmlSerializer 是静态的.我需要选择 Microsoft XmlSerializer 或 ServiceStack(不是两者).含义:如果我不能让这个简单的例子工作,我需要跳过 ServiceStack 包中一个非常有用的部分.我想要的最后一件事是在传入的 xml 中注入一些虚拟命名空间.

I can't create a new Serializer, because ServiceStack.Text.XmlSerializer is static. I need to choose for either Microsoft XmlSerializer OR ServiceStack (not both). Meaning: if I can't get this simple example to work I need to skip an otherwise very useful part of the ServiceStack package. The last thing I want is to inject some dummy namespace in the incoming xml.

推荐答案

ServiceStack 使用 .NET 的 Xml DataContractSerializer 来序列化 XML 以删除命名空间,您需要将命名空间设置为空字符串:

ServiceStack uses .NET's Xml DataContractSerializer to serialize XML to remove Namespaces you need to either set the Namespace to an empty string with:

[DataContract(Namespace="")]
public class test { ... }

但是,您必须使用 [DataMember] 属性标记要序列化的每个属性.更好的选择是通过在 Assembly.cs 文件中添加和 Assembly 属性来为 C# 命名空间下的所有类型指定一个空命名空间,例如:

But then you'll have to mark each property you want serialized with [DataMember] attributes. A better option is to specify an empty namespace for all types under a C# namespace by adding and Assembly attribute in your Assembly.cs file, e.g:

[assembly: ContractNamespace("", ClrNamespace = "MyServiceModel.DtoTypes")]

注意:您可以删除 [Serializable] 属性 - ServiceStack 的任何序列化程序都不会使用它.此外,像 [XmlRoot] 这样的所有 XmlSerializer 属性都是无用的,因为 ServiceStack 使用 .NET 的 DataContractSerializer 而不是 Microsoft 早期的 XmlSerializer.

Note: you can remove the [Serializable] attribute - it's not used by any of ServiceStack's serializers. Also all XmlSerializer attributes like [XmlRoot] are useless since ServiceStack uses .NET's DataContractSerializer not Microsoft's earlier XmlSerializer.

这篇关于XML 反序列化仅适用于 xml 中的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)