【发布时间】:2022-01-18 16:02:11
【问题描述】:
我有一个 XML 文档,看起来像
<?xml version="1.0" encoding="utf-8"?>
<DataProviderConfiguration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<FileVersion>1</FileVersion>
<SupportedEventTypes>
<InvestigateEventType>TimeBasedRefresh</InvestigateEventType>
<InvestigateEventType>IdBasedRefresh</InvestigateEventType>
<InvestigateEventType>FileReceived</InvestigateEventType>
</SupportedEventTypes>
它被解析为一些 DTO:
[XmlArray(nameof(SupportedEventTypes))]
[XmlArrayItem("InvestigateEventType")]
public virtual string[] SupportedEventTypes { get; set; }
鉴于上述文档,SupportedEventTypes 将包含 3 个项目。
现在上面的片段包含属性
<SupportedEventTypes>
<InvestigateEventType>TimeBasedRefresh</InvestigateEventType>
<InvestigateEventType IsDefault="true">IdBasedRefresh</InvestigateEventType>
<InvestigateEventType IsDefault="true">FileReceived</InvestigateEventType>
</SupportedEventTypes>
让我们考虑更新的 DTO
public class InvestigateEventType
{
public string Name { get; set; }
public bool IsDefault { get; set; }
}
[XmlArray(nameof(SupportedEventTypes))]
[XmlArrayItem("InvestigateEventType")]
public virtual InvestigateEventType[] SupportedEventTypes { get; set; }
给定InvestigateEventType,XMLSerializer 应该将其内容读入Name 属性并将IsDefault 属性读入IsDefault 属性。
如何实现?
如何使IsDefault属性不区分大小写?
我还没有尝试过这段代码,因为构建它需要进行大量更改,我正在寻求帮助。
【问题讨论】:
-
您可以在一次性项目或测试项目中试用此代码,这样您就不会对现有代码库进行巨大更改。
-
SupportedEventTypes应该是InvestigateEventType[]而不是SupportedEventType[] -
@phuzi 谢谢,错字
-
@Crowcoder 我不是要求调试我的代码,也不是在寻找捷径。我在问是否有人解决了类似的问题或有想法
-
@thebugsdontwork 成功了!如果你愿意,也许你可以发布一个答案。
标签: c# xmlserializer