1. <i id='jHDVp'><tr id='jHDVp'><dt id='jHDVp'><q id='jHDVp'><span id='jHDVp'><b id='jHDVp'><form id='jHDVp'><ins id='jHDVp'></ins><ul id='jHDVp'></ul><sub id='jHDVp'></sub></form><legend id='jHDVp'></legend><bdo id='jHDVp'><pre id='jHDVp'><center id='jHDVp'></center></pre></bdo></b><th id='jHDVp'></th></span></q></dt></tr></i><div id='jHDVp'><tfoot id='jHDVp'></tfoot><dl id='jHDVp'><fieldset id='jHDVp'></fieldset></dl></div>
      <legend id='jHDVp'><style id='jHDVp'><dir id='jHDVp'><q id='jHDVp'></q></dir></style></legend>
      • <bdo id='jHDVp'></bdo><ul id='jHDVp'></ul>

      <small id='jHDVp'></small><noframes id='jHDVp'>

      1. <tfoot id='jHDVp'></tfoot>

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

        XML deserializing only works with namespace in xml(XML 反序列化仅适用于 xml 中的命名空间)
          <bdo id='Btvdr'></bdo><ul id='Btvdr'></ul>
        • <tfoot id='Btvdr'></tfoot>

          <small id='Btvdr'></small><noframes id='Btvdr'>

        • <legend id='Btvdr'><style id='Btvdr'><dir id='Btvdr'><q id='Btvdr'></q></dir></style></legend>

          <i id='Btvdr'><tr id='Btvdr'><dt id='Btvdr'><q id='Btvdr'><span id='Btvdr'><b id='Btvdr'><form id='Btvdr'><ins id='Btvdr'></ins><ul id='Btvdr'></ul><sub id='Btvdr'></sub></form><legend id='Btvdr'></legend><bdo id='Btvdr'><pre id='Btvdr'><center id='Btvdr'></center></pre></bdo></b><th id='Btvdr'></th></span></q></dt></tr></i><div id='Btvdr'><tfoot id='Btvdr'></tfoot><dl id='Btvdr'><fieldset id='Btvdr'></fieldset></dl></div>

                  <tbody id='Btvdr'></tbody>

                1. 本文介绍了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 中的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  C# namespace alias - what#39;s the point?(C# 命名空间别名 - 有什么意义?)
                  Using Xpath With Default Namespace in C#(在 C# 中使用具有默认命名空间的 Xpath)
                  Generating an EDMX from a DB2 Database(从 DB2 数据库生成 EDMX)
                  IBM .NET Data Provider Connection String issue with Library List(库列表的 IBM .NET 数据提供程序连接字符串问题)
                  .NET DB2 OLEDB pre-requisites(.NET DB2 OLEDB 先决条件)
                  Referring to Code in IBM.Data.DB2 makes that Assembly Unavailable to the rest of my Solution(引用 IBM.Data.DB2 中的代码使该程序集对我的解决方案的其余部分不可用)
                  • <i id='Tw8vP'><tr id='Tw8vP'><dt id='Tw8vP'><q id='Tw8vP'><span id='Tw8vP'><b id='Tw8vP'><form id='Tw8vP'><ins id='Tw8vP'></ins><ul id='Tw8vP'></ul><sub id='Tw8vP'></sub></form><legend id='Tw8vP'></legend><bdo id='Tw8vP'><pre id='Tw8vP'><center id='Tw8vP'></center></pre></bdo></b><th id='Tw8vP'></th></span></q></dt></tr></i><div id='Tw8vP'><tfoot id='Tw8vP'></tfoot><dl id='Tw8vP'><fieldset id='Tw8vP'></fieldset></dl></div>

                        <tbody id='Tw8vP'></tbody>
                          <tfoot id='Tw8vP'></tfoot><legend id='Tw8vP'><style id='Tw8vP'><dir id='Tw8vP'><q id='Tw8vP'></q></dir></style></legend>

                          <small id='Tw8vP'></small><noframes id='Tw8vP'>

                          • <bdo id='Tw8vP'></bdo><ul id='Tw8vP'></ul>