• xml序列化成对象处理


            static void ConvertToObj()
            {
                var path = AppDomain.CurrentDomain.BaseDirectory + "/XMLFile1.xml";
    
                if (File.Exists(path))
                {
                    string xmlContent = string.Empty;
                    using (var sr = new StreamReader(path))
                    {
                        var xml = new XmlDocument();
                        xml.Load(sr);
                        xmlContent = xml.OuterXml;
                    }
    
                    //去除xsi标志信息,不去除会引发转换错误
                    xmlContent = xmlContent.Replace("xmlns="urn:hl7-org:v3"", "");
                    xmlContent = xmlContent.Replace("xsi:type=", "type=");
    
                    using (StringReader sr1 = new StringReader(xmlContent))
                    {
                        XmlSerializer xmldes = new XmlSerializer(typeof(BloodTransAppInfoAddObj.BloodTransAppInfoAdd));
                        var obj = (BloodTransAppInfoAddObj.BloodTransAppInfoAdd)xmldes.Deserialize(sr1);
                    }
                }
            }

    转换方法。

    这边是转换hl7v3文档,需要将一些hl7v3信息替换掉,不然转换失败!

     对象信息:

      基础信息:

      1     public partial class XmlObj
      2     {
      3         #region 通用基础类
      4 
      5         public class CodeValueData
      6         {
      7             [XmlElement("code")]
      8             public CodeAttributeData code { get; set; }
      9 
     10             [XmlElement("value")]
     11             public ValueAttributeData value { get; set; }
     12         }
     13 
     14 
     15         public class NameItemPartData
     16         {
     17             [XmlElement("item")]
     18             public PartData item { get; set; }
     19         }
     20 
     21         #region CodeSystemNameData
     22         public class CodeSystemNameDisplayNameData : ClassSystemNameAttributeData
     23         {
     24             [XmlElement("displayName")]
     25             public ValueAttributeData displayName { get; set; }
     26         }
     27 
     28         public class ClassSystemNameAttributeData
     29         {
     30             [XmlAttribute("codeSystemName")]
     31             public string codeSystemName { get; set; }
     32         }
     33         #endregion
     34 
     35         #region TypeCodeAttributeData
     36         public class Sender : TypeCodeAttributeData
     37         {
     38             [XmlElement("device")]
     39             public Device device { get; set; }
     40         }
     41 
     42 
     43         public class Receiver : TypeCodeAttributeData
     44         {
     45             [XmlElement("device")]
     46             public Device device { get; set; }
     47         }
     48 
     49         public class TypeCodeAttributeData
     50         {
     51             [XmlAttribute("typeCode")]
     52             public string typeCode { get; set; }
     53         }
     54         #endregion
     55 
     56         #region ClassCodeAttributeData
     57         public class Device : ClassCodeAttributeData
     58         {
     59             [XmlElement("id")]
     60             public IdWithItemData id { get; set; }
     61 
     62 
     63             [XmlAttribute("determinerCode")]
     64             public string determinerCode { get; set; }
     65 
     66         }
     67 
     68         public class ClassCodeAttributeData
     69         {
     70             [XmlAttribute("classCode")]
     71             public string classCode { get; set; }
     72         }
     73         #endregion
     74 
     75         #region ValueDataAttribute
     76         public class DisplayData
     77         {
     78             [XmlElement("displayName")]
     79             public ValueAttributeData displayName { get; set; }
     80         }
     81 
     82         public class UnitValueAttributeData : ValueAttributeData
     83         {
     84             [XmlAttribute("unit")]
     85             public string unit { get; set; }
     86         }
     87 
     88         public class PartData
     89         {
     90             [XmlElement("part")]
     91             public ValueAttributeData part { get; set; }
     92         }
     93 
     94         public class TelecomData
     95         {
     96             [XmlElement("item")]
     97             public ValueAttributeData item { get; set; }
     98         }
     99 
    100         public class ValueAttributeData
    101         {
    102             [XmlAttribute("value")]
    103             public string value { get; set; }
    104         }
    105         #endregion
    106 
    107 
    108 
    109         public class TypeValueAttributeData
    110         {
    111             [XmlAttribute("value")]
    112             public string value { get; set; }
    113 
    114             [XmlAttribute("type")]
    115             public string type { get; set; }
    116         }
    117 
    118         public class CodeAttributeData
    119         {
    120             [XmlAttribute("code")]
    121             public string code { get; set; }
    122         }
    123 
    124         #region Id
    125         public class TargetMessageData
    126         {
    127             [XmlElement("id")]
    128             public IdAttributeData id { get; set; }
    129         }
    130 
    131         public class IdWithItemData
    132         {
    133             [XmlElement("item")]
    134             public IdAttributeData item { get; set; }
    135         }
    136 
    137         public class IdAttributeData : ExtensionAttributeData
    138         {
    139             [XmlAttribute("root")]
    140             public string root { get; set; }
    141         }
    142 
    143         public class ExtensionAttributeData
    144         {
    145             [XmlAttribute("extension")]
    146             public string extension { get; set; }
    147         }
    148         #endregion
    149 
    150         public class IdData: ExtensionAttributeData
    151         {
    152             [XmlElement("id")]
    153             public string id { get; set; }
    154         }
    155 
    156         #endregion
    157 
    158     }
    View Code

      基本信息:

      1     public partial class XmlObj
      2     {
      3         public class BaseXmlObj
      4         {
      5             [XmlElement("id")]
      6             public IdAttributeData id { get; set; }
      7 
      8             [XmlElement("creationTime")]
      9             public ValueAttributeData creationTime { get; set; }
     10 
     11             [XmlElement("interactionId")]
     12             public IdAttributeData interactionId { get; set; }
     13 
     14             [XmlElement("processingCode")]
     15             public CodeAttributeData processingCode { get; set; }
     16 
     17             [XmlElement("acceptAckCode")]
     18             public CodeAttributeData acceptAckCode { get; set; }
     19 
     20             [XmlElement("receiver")]
     21             public Receiver receiver { get; set; }
     22 
     23             [XmlElement("sender")]
     24             public Sender sender { get; set; }
     25 
     26             [XmlElement("acknowledgement")]
     27             public Acknowledgement acknowledgement { get; set; }    
     28         }
     29 
     30         #region 基础类(细项类)
     31         public class BaseControlActProcess : ClassCodeAttributeData
     32         {
     33             [XmlAttribute("moodCode")]
     34             public string moodCode { get; set; }
     35 
     36             [XmlElement("queryAck")]
     37             public QueryAck queryAck { get; set; }
     38         }
     39 
     40         public class DownLocationData :ClassCodeAttributeData
     41         {
     42             [XmlElement("id")]
     43             public IdWithItemData id { get; set; }
     44 
     45             [XmlElement("name")]
     46             public NameItemPartData name { get; set; }
     47         }
     48 
     49         [XmlType("item")]
     50         public class Item
     51         {
     52             [XmlAttribute("root")]
     53             public string root { get; set; }
     54             [XmlAttribute("extension")]
     55             public string extension { get; set; }
     56         }
     57 
     58         public class QueryAck
     59         {
     60             [XmlElement("queryId")]
     61             public ExtensionAttributeData queryId { get; set; }
     62 
     63             [XmlElement("queryResponseCode")]
     64             public CodeAttributeData queryResponseCode { get; set; }
     65 
     66             [XmlElement("resultTotalQuantity")]
     67             public ValueAttributeData resultTotalQuantity { get; set; }
     68         }
     69 
     70         #region Acknowledgement
     71         public class Acknowledgement : TypeCodeAttributeData
     72         {
     73             [XmlElement("targetMessage")]
     74             public TargetMessageData targetMessage { get; set; }
     75 
     76             [XmlElement("acknowledgementDetail")]
     77             public AcknowledgementDetail acknowledgementDetail { get; set; }
     78         }
     79 
     80         public class AcknowledgementDetail
     81         {
     82             [XmlElement("text")]
     83             public ValueAttributeData text { get; set; }
     84         } 
     85         #endregion
     86 
     87         #region AssignedEntity
     88         public class AssignedEntity
     89         {
     90             [XmlElement("id")]
     91             public IdWithItemData id { get; set; }
     92 
     93             [XmlElement("assignedPerson")]
     94             public AssignedPerson assignedPerson { get; set; }
     95         }
     96 
     97         public class AssignedPerson
     98         {
     99             [XmlElement("name")]
    100             public NameItemPartData name { get; set; }
    101         }
    102         #endregion
    103 
    104         #region Patient
    105         public class Patient
    106         {
    107             [XmlArray("id")]
    108             public List<XmlObj.Item> id { get; set; }
    109 
    110             [XmlElement("statusCode")]
    111             public StatusCode statusCode { get; set; }
    112 
    113             [XmlElement("effectiveTime")]
    114             public EffectiveTime effectiveTime { get; set; }
    115 
    116             [XmlElement("patientPerson")]
    117             public PatientPerson patientPerson { get; set; }
    118 
    119             [XmlElement("providerOrganization")]
    120             public ProviderOrganization providerOrganization { get; set; }
    121 
    122             [XmlElement("subjectOf1")]
    123             public SubjectOf1 subjectOf1 { get; set; }
    124 
    125             [XmlElement("subjectOf2")]
    126             public SubjectOf2 subjectOf2 { get; set; }
    127 
    128             [XmlElement("coveredPartyOf")]
    129             public CoveredPartyOf coveredPartyOf { get; set; }
    130         }
    131 
    132         #region CoveredPartyOf
    133 
    134         public class CoveredPartyOf
    135         {
    136             [XmlElement("coverageRecord")]
    137             public CoverageRecord coverageRecord { get; set; }
    138         }
    139 
    140         public class CoverageRecord
    141         {
    142             [XmlElement("beneficiary")]
    143             public Beneficiary beneficiary { get; set; }
    144         }
    145 
    146         public class Beneficiary : XmlObj.TypeCodeAttributeData
    147         {
    148             [XmlElement("beneficiary")]
    149             public Beneficiary1 beneficiary { get; set; }
    150         }
    151 
    152         public class Beneficiary1 : XmlObj.ClassCodeAttributeData
    153         {
    154             [XmlElement("code")]
    155             public XmlObj.CodeSystemNameDisplayNameData code { get; set; }
    156         }
    157 
    158 
    159         #endregion
    160 
    161         public class SubjectOf2
    162         {
    163             [XmlElement("administrativeObservation")]
    164             public XmlObj.CodeValueData administrativeObservation { get; set; }
    165         }
    166 
    167         public class SubjectOf1
    168         {
    169             [XmlElement("queryMatchObservation")]
    170             public XmlObj.CodeValueData queryMatchObservation { get; set; }
    171         }
    172 
    173         public class ProviderOrganization
    174         {
    175             [XmlArray("id")]
    176             public List<XmlObj.Item> id { get; set; }
    177 
    178             [XmlElement("name")]
    179             public XmlObj.NameItemPartData name { get; set; }
    180 
    181             [XmlElement("contactParty")]
    182             public XmlObj.ClassSystemNameAttributeData contactParty { get; set; }
    183         }
    184 
    185         #region PatientPerson
    186         public class PatientPerson
    187         {
    188             [XmlArray("id")]
    189             public List<XmlObj.Item> id { get; set; }
    190 
    191             [XmlElement("idCategory")]
    192             public XmlObj.CodeSystemNameDisplayNameData idCategory { get; set; }
    193 
    194             [XmlElement("name")]
    195             public Name name { get; set; }
    196 
    197             [XmlElement("telecom")]
    198             public XmlObj.TelecomData telecom { get; set; }
    199 
    200             [XmlElement("administrativeGenderCode")]
    201             public XmlObj.CodeSystemNameDisplayNameData administrativeGenderCode { get; set; }
    202 
    203             [XmlElement("birthTime")]
    204             public XmlObj.ValueAttributeData birthTime { get; set; }
    205 
    206             [XmlElement("addr")]
    207             public Addr addr { get; set; }
    208 
    209             [XmlElement("maritalStatusCode")]
    210             public XmlObj.CodeSystemNameDisplayNameData maritalStatusCode { get; set; }
    211 
    212             [XmlElement("ethnicGroupCode")]
    213             public EthnicGroupCode maritalethnicGroupCodeStatusCode { get; set; }
    214 
    215             [XmlElement("asEmployee")]
    216             public AsEmployee asEmployee { get; set; }
    217 
    218             [XmlElement("asOtherIDs")]
    219             public List<AsOtherIDs> asOtherIDs { get; set; }
    220 
    221             [XmlElement("personalRelationship")]
    222             public PersonalRelationship personalRelationship { get; set; }
    223         }
    224 
    225         #region Name
    226         public class Name
    227         {
    228             [XmlElement("item")]
    229             public PatientItem item { get; set; }
    230         }
    231 
    232         public class PatientItem
    233         {
    234             [XmlElement("part")]
    235             public XmlObj.ValueAttributeData part { get; set; }
    236         }
    237         #endregion
    238 
    239         #region PersonalRelationship
    240         public class PersonalRelationship
    241         {
    242             [XmlElement("id")]
    243             public XmlObj.IdWithItemData id { get; set; }
    244 
    245             [XmlElement("code")]
    246             public XmlObj.CodeSystemNameDisplayNameData code { get; set; }
    247 
    248             [XmlElement("telecom")]
    249             public XmlObj.TelecomData telecom { get; set; }
    250 
    251             [XmlElement("relationshipHolder1")]
    252             public RelationshipHolder1 relationshipHolder1 { get; set; }
    253         }
    254 
    255         public class RelationshipHolder1
    256         {
    257             [XmlElement("name")]
    258             public XmlObj.NameItemPartData name { get; set; }
    259         }
    260         #endregion
    261 
    262         #region AsOtherIDs
    263         [XmlType("asOtherIDs")]
    264         public class AsOtherIDs
    265         {
    266             [XmlElement("id")]
    267             public XmlObj.IdWithItemData id { get; set; }
    268 
    269             [XmlElement("scopingOrganization")]
    270             public ScopingOrganization scopingOrganization { get; set; }
    271         }
    272 
    273         public class ScopingOrganization
    274         {
    275             [XmlElement("id")]
    276             public XmlObj.IdWithItemData id { get; set; }
    277         }
    278         #endregion
    279 
    280         #region AsEmployee
    281         public class AsEmployee
    282         {
    283             [XmlElement("occupationCode")]
    284             public XmlObj.CodeSystemNameDisplayNameData occupationCode { get; set; }
    285 
    286             [XmlElement("employerOrganization")]
    287             public EmployerOrganization employerOrganization { get; set; }
    288         }
    289 
    290         public class EmployerOrganization
    291         {
    292             [XmlElement("name")]
    293             public XmlObj.NameItemPartData name { get; set; }
    294 
    295             [XmlElement("contactParty")]
    296             public ContactParty contactParty { get; set; }
    297 
    298         }
    299 
    300         public class ContactParty
    301         {
    302             [XmlElement("telecom")]
    303             public XmlObj.TelecomData telecom { get; set; }
    304         }
    305         #endregion
    306 
    307         public class EthnicGroupCode
    308         {
    309             [XmlElement("item")]
    310             public XmlObj.CodeSystemNameDisplayNameData item { get; set; }
    311 
    312         }
    313 
    314         #region Addr
    315         public class Addr
    316         {
    317             [XmlArray("item")]
    318             public List<AddrItem> item { get; set; }
    319         }
    320 
    321         [XmlType("part")]
    322         public class AddrItem
    323         {
    324             [XmlAttribute("value")]
    325             public string value { get; set; }
    326 
    327             [XmlAttribute("type")]
    328             public string type { get; set; }
    329         }
    330         #endregion
    331 
    332         #endregion
    333 
    334         public class EffectiveTime
    335         {
    336             [XmlElement("low")]
    337             public XmlObj.ValueAttributeData low { get; set; }
    338         }
    339 
    340         public class StatusCode
    341         {
    342             [XmlAttribute("code")]
    343             public string code { get; set; }
    344         }
    345         #endregion
    346 
    347         #endregion
    348     }
    View Code

      需要转换的信息:

        public class BloodTransAppInfoAddObj
        {
            [XmlType("MCCI_IN000002UV01")]
            public class BloodTransAppInfoAdd: XmlObj.BaseXmlObj
            {
                
            }
        }

    这里单独建立一个对象以适用于XmlType特性修改。

    这里特别注意数组

    有两种转换方式:

    1.

                [XmlArray("id")]
                public List<XmlObj.Item> id { get; set; }

    适用于xml内容为:

    <id>
        <!-- @extension域ID,root为固定值 -->
        <item root="2.16.156.10011.2.5.1.5" extension="域ID(必填)"/>
        <!--@extension患者ID标识,root为固定值-->
        <item root="2.16.156.10011.2.5.1.4" extension="患者ID标识(必填)"/>
        <!--@extension门(急)诊号标识,root为固定值 -->
        <item root="2.16.156.10011.1.11" extension="门(急)诊号标识"/>
        <!--@extension住院号标识,root为固定值-->
        <item root="2.16.156.10011.1.12" extension="住院号标识"/>
    <!-- @extension病案号, root为固定值 -->
         <item root="2.16.156.10011.1.13" extension="病案号"/>
    </id>

    2.

                [XmlElement("asOtherIDs")]
                public List<AsOtherIDs> asOtherIDs { get; set; }

    适用于xml内容为:

    <asOtherIDs classCode="PAT">
        <!--城乡居民健康档案编号@extension-->
        <id>
            <item root="2.16.156.10011.1.2" extension="XXXXX" />
        </id>
        <scopingOrganization classCode="ORG" determinerCode="INSTANCE">
            <!--建档医疗机构组织机构代码@extension-->
            <id>
                <item root="2.16.156.10011.1.5" extension="XXXX" />
            </id>
        </scopingOrganization>
    </asOtherIDs>
    <asOtherIDs classCode="PAT">
        <!--就诊卡@extension-->
        <id>
            <item root="2.16.156.10011.2.5.1.6" extension="DXXXX" />
        </id>
    </asOtherIDs>
    <asOtherIDs classCode="PAT">
        <!--住院号extension-->
        <id>
            <item root="2.16.156.10011.1.12" extension="XXXXX" />
        </id>
    </asOtherIDs>
  • 相关阅读:
    python 3.5下用户登录验证,三次锁定的编码
    Python之面向对象
    Python基础之模块
    Python基础之yield,匿名函数,包与re模块
    Python基础之函数
    Python基础之字符编码,文件操作流与函数
    Python基础之字符串,布尔值,整数,列表,元组,字典,集合
    Python基础之(判断,循环,列表,字典)
    mysql学习
    linux 下的 正则表达式(awk,sed,awk)学习
  • 原文地址:https://www.cnblogs.com/FlyStupidBird/p/14520275.html
Copyright © 2020-2023  润新知