【问题标题】:XML serialization issue in C#C#中的XML序列化问题
【发布时间】:2020-08-08 02:37:35
【问题描述】:

我正在开展一个项目,我们正在从遗留代码迁移到新的 .net 核心代码。在 xml 序列化过程中遇到问题。

这是旧应用程序中显示的内容:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <Response xmlns="http://example.com/WebServices/">         
         <Type i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/>
         <UserId i:nil="true" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"/>
      </Response>
   </s:Body>
</s:Envelope>

在新代码中:

<?xml version="1.0" encoding="utf-8"?>
<s:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
        <Response xmlns="http://example.com/WebServices/"> 
            <xsi:Type>PROCESSED</xsi:Type>
            <xsi:UserId />
        </Response>
    </s:Body>
</s:Envelope>

这是类定义:

 [XmlType(Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        [XmlRoot(ElementName = "Envelope", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public class AResponse
        {
            [XmlElement(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
            public ABody body { get; set; }
            [XmlNamespaceDeclarations]
            public XmlSerializerNamespaces xmlns = new XmlSerializerNamespaces();
            public AResponse()
            {
                xmlns.Add("s", "http://schemas.xmlsoap.org/soap/envelope/");
            }
        }
        [XmlRoot(ElementName = "Body", Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
        public class ABody
        {
            [XmlElement(ElementName = "OAuthResponse", Namespace = "http://example.com/WebServices/")]
            public Response result { get; set; }
        }
        [XmlRoot(ElementName = "OAuthResponse", Namespace = "http://example.com/WebServices/")]
        public class Response
        {  
            [XmlElement(Namespace = "http://www.w3.org/2001/XMLSchema-instance", IsNullable = true)]
            public string Type { get; set; }
    
            
            [XmlElement(IsNullable = true, ElementName = "UserId", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
            public string UserId { get; set; } = "";       
        }

如果您注意到显示的新旧 xml 之间存在差异,新代码会自动添加 xsi 标记并将命名空间置于标头级别,并且我所做的任何更改都不会更改它。您能否建议如何进行这项工作?

【问题讨论】:

  • 您明确表示TypeUserId 应该在"http://www.w3.org/2001/XMLSchema-instance" 命名空间中
  • 是的克劳斯。我期待它在同一个节点中显示如下: w3.org/2001/XMLSchema-instance"> 但相反。它创建了 xsi 标记并在信封顶部添加了命名空间。是否可能像我提到的那样拥有它?
  • 从 xml 的角度来看,命名空间声明在树中的位置是完全不相关的
  • 谢谢克劳斯。这是我想确认的。我会进行一些测试,看看它是否按原样工作,并将你的答案标记为完成

标签: c# xml serialization


【解决方案1】:

要去除 Type 和 UserId 元素中的“xsi:”,只需从 XmlElementAtttribute 中删除 Namespace = "http://www.w3.org/2001/XMLSchema-instance",例如

[XmlElement(IsNullable = true)]
public string Type { get; set; }

[XmlElement(IsNullable = true)]
public string UserId { get; set; };       

IsNullable = true 应该保留,它使UserId = null 被序列化为&lt;UserId xsi:nil="true" /&gt;UserId = "" 将被序列化为&lt;UserId /&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多