• C# XELEMENT 解析xml文件(字符串)


    XElement 解析xml字符串,或者xml文件方法

    xml文件

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <Request>
     3   <Header>
     4     <SourceSystem>02</SourceSystem>
     5     <MessageID>34</MessageID>
     6   </Header>
     7   <Body>
     8     <AddOrdersRt>
     9       <PATPatientID>0001235865</PATPatientID>
    10       <PATPatientIDS>1235871</PATPatientIDS>
    11       <PAADMVisitNumber>538477</PAADMVisitNumber>
    12       <PAADMEncounterTypeCode>I</PAADMEncounterTypeCode>
    13       <OEORIInfoList>
    14         <OEORIInfo>
    15           <BusinessFieldCode>00002</BusinessFieldCode>
    16           <HospitalCode>SDY</HospitalCode>
    17           <OEORIOrderItemID>527827||374</OEORIOrderItemID>
    18           <OEORIOEORIDR/>
    19           <OEORIARCItmMastCode>310701001c</OEORIARCItmMastCode>
    20           <OEORIARCItmMastDesc>常规心电图检查(十二通道)</OEORIARCItmMastDesc>
    21           <OEORIPriorityCode>NORM</OEORIPriorityCode>
    22           <OEORIPriorityDesc>临时医嘱</OEORIPriorityDesc>
    23           <OEORIStatusCode>V</OEORIStatusCode>
    24           <OEORIStatusDesc>核实</OEORIStatusDesc>
    25           <OEORIClass>检查</OEORIClass>
    26           <OEORIClassDesc>检查</OEORIClassDesc>
    27           <OEORIPrescNo/>
    28           <OEORIDoseFormsCode/>
    29           <OEORIDoseFormsDesc/>
    30           <OEORIDoseQty/>
    31           <OEORIDoseUnitCode/>
    32           <OEORIDoseUnitDesc/>
    33           <OEORIFreqCode/>
    34           <OEORIFreqDesc/>
    35           <OEORIInstrCode/>
    36           <OEORIInstrDesc/>
    37           <OEORIDurationCode/>
    38           <OEORIDurationDesc/>
    39           <OEORIOrderQty>1</OEORIOrderQty>
    40           <OEORIResultStatusCode>A</OEORIResultStatusCode>
    41           <OEORIResultStatusDesc>Awaiting</OEORIResultStatusDesc>
    42           <OEORIRemarks/>
    43           <OEORIEnterDocCode>101680</OEORIEnterDocCode>
    44           <OEORIEnterDocDesc>许晖</OEORIEnterDocDesc>
    45           <OEORIEnterDate>2019-12-26</OEORIEnterDate>
    46           <OEORIEnterTime>15:14:09</OEORIEnterTime>
    47           <OEORIEnterDeptCode>12802</OEORIEnterDeptCode>
    48           <OEORIEnterDeptDesc>神经外科</OEORIEnterDeptDesc>
    49           <OEORIExecDeptCode>15001</OEORIExecDeptCode>
    50           <OEORIExecDeptDesc>心电图室</OEORIExecDeptDesc>
    51           <OEORIRequireExecDate>2019-12-26</OEORIRequireExecDate>
    52           <OEORIRequireExecTime>15:14:00</OEORIRequireExecTime>
    53           <OEORIStopDate/>
    54           <OEORIStopTime/>
    55           <OEORIStopDocCode/>
    56           <OEORIStopDocDesc/>
    57           <OEORIIsSkinTest/>
    58           <OEORIISEmergency>Y</OEORIISEmergency>
    59           <OEORIParentOrderID/>
    60           <OEORISpecimenID/>
    61           <OEORISpecimenCode/>
    62           <MaterialBarcode/>
    63           <OEORIPrice>21</OEORIPrice>
    64           <OEORISpecification/>
    65           <AppointmentResources>动态心电图</AppointmentResources>
    66           <AppointmentNo>1</AppointmentNo>
    67           <AppointmentDTime>2019-12-27 08:00:00</AppointmentDTime>
    68         </OEORIInfo>
    69       </OEORIInfoList>
    70       <PAADMAdmWardCode>12803</PAADMAdmWardCode>
    71       <PAADMAdmWardDesc>神经外科病区</PAADMAdmWardDesc>
    72       <PAADMCurBedNo>46</PAADMCurBedNo>
    73       <Diagnoses>颅底骨折^顶骨骨折^颞骨骨折^脑挫伤^创伤性脑疝^创伤性闭合性硬膜外血肿</Diagnoses>
    74       <UpdateUserCode>demo</UpdateUserCode>
    75       <UpdateUserDesc>demo</UpdateUserDesc>
    76       <UpdateDate>2019-12-26</UpdateDate>
    77       <UpdateTime>15:30:30</UpdateTime>
    78     </AddOrdersRt>
    79   </Body>
    80 </Request>


    C#程序解析


    1
    class Program 2 { 3 static void Main(string[] args) 4 { 5 string path = @"C:UsersKevinDesktopookinfo.xml"; 6 7 //XElement xe = XElement.Parse(xmlStr); 解析xml字串传 8 XElement xe = XElement.Load(path); 9 10 /*预约*/ 11 12 EcgBook ebook = new EcgBook(); 13 14 var Header = xe.Elements("Header"); 15 var addOrdersRt = xe.Elements("Body").Elements("AddOrdersRt"); 16 var orderInfo = xe.Elements("Body").Elements("AddOrdersRt").Elements("OEORIInfoList").Elements("OEORIInfo"); 17 18 //遍历header节点 19 foreach (XElement item in Header) 20 { 21 ebook.sourceSystem = GetElementValue(item, "SourceSystem"); 22 ebook.messageID = GetElementValue(item, "MessageID"); 23 } 24 25 //遍历body/addOrdersRt节点 26 foreach (XElement item in addOrdersRt) 27 { 28 ebook.patientID = GetElementValue(item, "PATPatientID"); 29 ebook.visitNumber = GetElementValue(item, "PAADMVisitNumber"); 30 ebook.visitType = GetElementValue(item, "PAADMEncounterTypeCode"); 31 ebook.clinicDiag = GetElementValue(item, "Diagnoses"); 32 ebook.updateUserCode = GetElementValue(item, "UpdateUserCode"); 33 ebook.updateUserDesc = GetElementValue(item, "UpdateUserDesc"); 34 ebook.updateDate = GetElementValue(item, "UpdateDate"); 35 ebook.updateTime = GetElementValue(item, "UpdateTime"); 36 } 37 38 foreach (XElement item in orderInfo) 39 { 40 ebook.orderItemID = GetElementValue(item, "OEORIOrderItemID"); 41 ebook.orderItemMastCode = GetElementValue(item, "OEORIARCItmMastCode"); 42 ebook.orderItemMastDesc = GetElementValue(item, "OEORIARCItmMastDesc"); 43 44 ebook.orderStatusCode = GetElementValue(item, "OEORIStatusCode"); 45 ebook.orderStatusDesc = GetElementValue(item, "OEORIStatusDesc"); 46 47 ebook.orderEnterDocCode = GetElementValue(item, "OEORIEnterDocCode"); 48 ebook.orderEnterDocDesc = GetElementValue(item, "OEORIEnterDocDesc"); 49 ebook.orderEnterDeptCode = GetElementValue(item, "OEORIEnterDeptCode"); 50 ebook.orderEnterDeptDesc = GetElementValue(item, "OEORIEnterDeptDesc"); 51 ebook.orderExecDeptCode = GetElementValue(item, "OEORIExecDeptCode"); 52 ebook.orderExecDeptDesc = GetElementValue(item, "OEORIExecDeptDesc"); 53 ebook.orderExecDate = GetElementValue(item, "OEORIRequireExecDate"); 54 ebook.orderExecTime = GetElementValue(item, "OEORIRequireExecTime"); 55 56 ebook.orderTypeCode = GetElementValue(item, "orderTypeCode"); 57 ebook.orderTypeDesc = GetElementValue(item, "orderTypeDesc"); 58 ebook.orderClass = GetElementValue(item, "orderClass"); 59 ebook.orderClassDesc = GetElementValue(item, "orderClassDesc"); 60 61 } 62 63 Console.WriteLine(“成功”); 64 Console.ReadKey(); 65 }
    public static string GetElementValue(XElement item, string nodeName)
            {
                if (string.IsNullOrEmpty(nodeName)) return "";
                return item.Element(nodeName) == null ? "" : item.Element(nodeName).Value.ToString();
            }
    

      

    备注:XELEMENT对象,实例化之后,xe直接定位到根节点,查找元素是,从根节点下一级节点开始查找节点。

    对于不可控的事情,保持乐观; 对于可控的事情,保持谨慎
  • 相关阅读:
    windows类型
    网络编程socket、udp
    mem族函数与str族函数(待填)
    位运算符的用处(待填)
    c51较c比较,单片机最小系统
    数据结构之 顺序栈的操作
    [置顶] 数据结构之 顺序栈的操作
    java中常用的帮助类。加快开发速度
    php实现安装程序的 安装
    php压缩文件帮助类
  • 原文地址:https://www.cnblogs.com/baylor2019/p/12103476.html
Copyright © 2020-2023  润新知