mono多层级xml文件结构及序列化实现
【摘要】 xml文件结构如下:入口文件:Adaptor.xmlC:\Program Files\dotnet\success.xml<?xml version="1.0" encoding="utf-8" ?><Root> <Case Target="AAA">NoFailureInfo.xml</Case> <Case Target="BBB">SuccessAll.xml</Case> <C...
xml文件结构如下:
入口文件:Adaptor.xml
<?xml version="1.0" encoding="utf-8" ?> <Root> <ConfigFile>C:\Program Files\dotnet\success.xml</ConfigFile> <AdaptorType> <CaseValue Type ="AAA">Simulator</CaseValue> <CaseValue Type ="BBB">Hal</CaseValue> <CaseValue Type ="CCC">Simulator</CaseValue> </AdaptorType> </Root>
C:\Program Files\dotnet\success.xml
<?xml version="1.0" encoding="utf-8" ?> <Root> <Case Target="AAA">NoFailureInfo.xml</Case> <Case Target="BBB">SuccessAll.xml</Case> <Case Target="CCC">GetCCC.xml</Case> </Root>
C:\Program Files\dotnet\AAA\NoFailureInfo.xml
<?xml version="1.0" encoding="utf-8" ?> <Root Type="AAA"> <Granule Target="Init">SuccessVariable.xml</Granule> </Root>
C:\Program Files\dotnet\AAA\Init\SuccessVariable.xml
<?xml version="1.0" encoding="utf-8" ?> <Granule> <DataGroup> <Data Key="winkle">555555</Data> </DataGroup> <EventGroup> <Event Key="winklec2">2</Event> </EventGroup> <Return>success</Return> </Granule>
对应实体类定义如下:
[Serializable] [XmlRoot("Root")] public class SimulatorConfigModel { [XmlElement("ConfigFile")] public string ConfigFile { get; set; } [XmlElement("AdaptorType")] public AdaptorType AdaType { get; set; } } [Serializable] public class AdaptorType { [XmlElement("CaseValue")] public List<CaseValue> TypeList { get; set; } } [Serializable] public class CaseValue { [XmlAttribute("Type")] public string Type { get; set; } [XmlText] public string ValueType { get; set; } } public class HalAdaptorConfigModel { public string ModuleName { get; set; } public List<HalMethodModel> MethodList { get; set; } } [Serializable] [XmlRoot("Granule")] public class HalMethodModel { [XmlIgnore] public string MethodName { get; set; } [XmlElement("Return")] public string StrReturn { get; set; } [XmlElement("DataGroup")] public DataGroup DataList { get; set; } [XmlElement("EventGroup")] public EventGroup EventList { get; set; } } [Serializable] public class DataGroup { [XmlElement("Data")] public List<KeyValue> DataKeys { get; set; } } [Serializable] public class EventGroup { [XmlElement("Event")] public List<KeyValue> EventKeys { get; set; } } [Serializable] public class KeyValue { [XmlAttribute("Key")] public string Key { get; set; } [XmlText] public string Value { get; set; } }
实际序列化代码如下:
public static T DeSerializer<T>(string strXml) where T : class { try { using (StreamReader st = new StreamReader(strXml)) { XmlSerializer serializer = new XmlSerializer(typeof(T)); return serializer.Deserialize(st) as T; } } catch (Exception ex) { CustomExportLog.ExportExceptionLog(ex); } return default(T); }
只把开始入口文件和最终的配置xml文件序列化为实体类,其余文件使用XElement类方法读取
值得一提的是,在实际运用中,自定义命名空间[xmlns:......]的方法是使用XmlSerializer类的重载方法:
public void Serialize(Stream stream, object o, XmlSerializerNamespaces namespaces);
进行序列化,其中“namespaces”参数可由以下代码创建:
XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces( new[] { new XmlQualifiedName(string.Empty, "") });
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)