gpt4 book ai didi

c# - 如何在 C# 中反序列化具有前缀的 XML

转载 作者:行者123 更新时间:2023-12-04 16:54:06 25 4
gpt4 key购买 nike

这是我的 XML

<?xml version="1.0" encoding="UTF-8"?>
<ls:LeadResponse xmlns:ls="example.org/ls">
<ls:status>OK</ls:status>
<ls:code>approved</ls:code>
<ls:message>posted</ls:message>
</ls:LeadResponse>
它有前缀 ls到处。
我已经这样做了反序列化
创建的模型:
 [XmlRoot(ElementName = "LeadResponse",Namespace="example.org/ls")]
public class LeadProviderResponse
{
[XmlElement(Namespace = "example.org/ls")]
public string Status { get; set; }

[XmlElement(Namespace = "example.org/ls")]
public string Code { get; set; }

[XmlElement(Namespace = "example.org/ls")]
public string Message { get; set; }
}
和反序列化的代码
using var streamReader = new StringReader(xmlContent); 
using var xmlReader = XmlReader.Create(streamReader, null);
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(LeadProviderResponse));
var result = (LeadProviderRespone)serializer.Deserialize(xmlReader);
它无法正确反序列化。谁能告诉我我哪里做错了?

最佳答案

套管:

[XmlElement(Namespace = "example.org/ls", ElementName = "status")]
public string Status { get; set; }

[XmlElement(Namespace = "example.org/ls", ElementName = "code")]
public string Code { get; set; }

[XmlElement(Namespace = "example.org/ls", ElementName = "message")]
public string Message { get; set; }
或(相同的结果):
[XmlElement("status", Namespace = "example.org/ls")]
public string Status { get; set; }

[XmlElement("code", Namespace = "example.org/ls")]
public string Code { get; set; }

[XmlElement("message", Namespace = "example.org/ls")]
public string Message { get; set; }

关于c# - 如何在 C# 中反序列化具有前缀的 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64787100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com