【问题标题】:How to deal with the XMLNS headers when deserializing反序列化时如何处理 XMLNS 标头
【发布时间】:2016-12-07 15:31:17
【问题描述】:

<?xml version="1.0" encoding="UTF-8"?>
<EfxTransmit
	xmlns="http://www.....abc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www....def http://www....ghi">
	<EfxReport requestNumber="1" reportId="CNCONSUMERCREDITFILE">
		<CNConsumerCreditReports>
			....I only care about these
			....I only care about these
			....I only care about these
		</CNConsumerCreditReports>
	</EfxReport>
</EfxTransmit>

我创建了一个类似这样的对象架构:

[XmlRoot("CNConsumerCreditReports")]
public class Data
{
    [XmlElement("CNConsumerCreditReport")]
    public CNConsumerCreditReports CNConsumerCreditReports { get; set; }
}

但为了使该架构正常工作,我必须手动从 XML 字符串中删除以下内容及其结束标记

<EfxTransmit
	xmlns="http://www.....abc"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www....def http://www....ghi">
	<EfxReport requestNumber="1" reportId="CNCONSUMERCREDITFILE">

但是,我发现手动删除这些标签很麻烦,我希望找到一种解决方法。我只是不知道是什么或如何。但我相信还有更好的方法。

感谢任何建议或截断的代码!

谢谢。

【问题讨论】:

标签: c# xml


【解决方案1】:

鉴于几乎没有其他内容,反序列化整个文档比尝试提取所需的片段更容易。

您还需要将模型属性中的命名空间与 XML 中的命名空间相匹配。

比如:

[XmlRoot(Namespace="http://www.....abc")]
public class EfxTransmit
{
    public EfxReport EfxReport { get; set; }
}

public class EfxReport
{
    public CNConsumerCreditReports CNConsumerCreditReports { get; set; }
}

public class CNConsumerCreditReports
{
    public CNConsumerCreditReport CNConsumerCreditReport { get; set; }
}

public class CNConsumerCreditReport
{
    // ...
}

【讨论】:

  • 谢谢。您的回答简单明了。喜欢它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-28
  • 1970-01-01
相关资源
最近更新 更多