XElement analytic xml string or xml file Method
xml file
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 > 527 827 374 || </ OEORIOrderItemID > 18 is < OEORIOEORIDR /> . 19 < OEORIARCItmMastCode > 310701001c </ OEORIARCItmMastCode > 20 is < OEORIARCItmMastDesc > ECG examination (twelve channels) </ OEORIARCItmMastDesc > 21 is < OEORIPriorityCode > NORM </ OEORIPriorityCode > 22 is < OEORIPriorityDesc > temporary orders </ 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 is < PAADMAdmWardDesc > neurosurgical wards </ PAADMAdmWardDesc > 72 < PAADMCurBedNo > 46 is </ PAADMCurBedNo > 73 is < diagnoses > skull fracture ^ parietal temporal bone fracture fracture ^ ^ ^ Traumatic Brain Injury Traumatic hernia ^ closed epidural hematoma </ 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 # program analysis
. 1 classProgram 2 { . 3 static voidthe Main (String[] args) . 4 { . 5 Stringpath =@ "C: \ the Users \ Kevin \ Desktop \ bookinfo.xml"; . 6 . 7 //the XElement XE = XElement.Parse (xmlStr); xml parsing a string transfer . 8 the XElement XE =XElement.Load (path); . 9 10 / *reserved* / . 11 12 is EcgBook eBook =new newEcgBook (); 13 is 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"); 20 isHeader) in(the XElement Itemthe foreach. 19traversal header node//18 is. 17 { 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 }
Note: XELEMENT object after instantiation, xe positioned directly to the root, to find elements that start looking at the root node from a node.