private void Button_Click(object sender, RoutedEventArgs e) { String myHL7string = "MSH|^~\\&|455755610_0100||0200||20110624160404|000|QRY^A19^QRY_A19|0123456001|P|2.6\nQRD|||||||||0001^张三^体检号^EQ^AND~0002^East^病区号^EQ^AND\nQRF||20110627|20110803\nPID|1||034123123^^^^MR||0992^张三||198298313000|男\nOBX|1|IS|08001^TakeModel^99MRC||O||||||F\nOBX|2|NM|30525-0^Age^LN||15|yr|||||F"; string sHL7asXml = HL7ToXmlConverter.ConvertToXml(myHL7string); XmlDocument xmlObject = HL7ToXmlConverter.ConvertToXmlObject(myHL7string); String value = HL7ToXmlConverter.GetText(xmlObject, "QRD/QRD.9/QRD.9.0", 0); String value2 = HL7ToXmlConverter.GetText(xmlObject, "QRD/QRD.9/QRD.9.0"); String nodeValue = xmlObject.DocumentElement.SelectSingleNode("MSH/MSH.9").InnerText; String nodeValue2 = xmlObject.DocumentElement.SelectSingleNode("QRD/QRD.9/QRD.9.0/QRD.9.0.2").InnerText; String patientName = xmlObject.DocumentElement.SelectSingleNode("PID/PID.5/PID.5.0/PID.5.0.1").InnerText; string obxValue = xmlObject.DocumentElement.SelectSingleNode("OBX/OBX.1").InnerText; foreach (XmlElement items in xmlObject.DocumentElement.SelectNodes("OBX")) { string checkValue = items.SelectSingleNode("OBX.3").InnerText; Console.WriteLine(checkValue); } }
public static XmlDocument ConvertToXmlObject(string sHL7) { _xmlDoc = CreateXmlDoc(); //把HL7分成段 string[] sHL7Lines = sHL7.Split('\n'); //去掉XML的关键字(LSH) for (int i = 0; i < sHL7Lines.Length; i++) { //sHL7Lines[i] = Regex.Replace(sHL7Lines[i], @"[^ -~]", ""); } for (int i = 0; i < sHL7Lines.Length; i++) { // 判断是否空行 if (sHL7Lines[i] != string.Empty) { string sHL7Line = sHL7Lines[i]; //通过/r 或/n 回车符分隔 string[] sFields = HL7ToXmlConverter.GetMessgeFields(sHL7Line); // 为段(一行)创建第一级节点 XmlElement el = _xmlDoc.CreateElement(sFields[0]); _xmlDoc.DocumentElement.AppendChild(el); // 循环每一行 for (int a = 0; a < sFields.Length; a++) { // 为字段创建第二级节点 XmlElement fieldEl = _xmlDoc.CreateElement(sFields[0] + "." + a.ToString()); //是否包括HL7的连接符 if (sFields[a] != @"^~\&") {//0:如果这一行有任何分隔符 //通过~分隔 string[] sComponents = HL7ToXmlConverter.GetRepetitions(sFields[a]); if (sComponents.Length >= 1) {//1:如果可以分隔 for (int b = 0; b < sComponents.Length; b++) { XmlElement componentEl = _xmlDoc.CreateElement(sFields[0] + "." + a.ToString() + "." + b.ToString()); //通过&分隔 string[] subComponents = GetSubComponents(sComponents[b]); if (subComponents.Length > 1) {//2.如果有字组,一般是没有的。。。 for (int c = 0; c < subComponents.Length; c++) { //修改了一个错误 string[] subComponentRepetitions = GetComponents(subComponents[c]); if (subComponentRepetitions.Length > 1) { for (int d = 0; d < subComponentRepetitions.Length; d++) { XmlElement subComponentRepEl = _xmlDoc.CreateElement(sFields[0] + "." + a.ToString() + "." + b.ToString() + "." + c.ToString() + "." + d.ToString()); subComponentRepEl.InnerText = subComponentRepetitions[d]; componentEl.AppendChild(subComponentRepEl); } } else { XmlElement subComponentEl = _xmlDoc.CreateElement(sFields[0] + "." + a.ToString() + "." + b.ToString() + "." + c.ToString()); subComponentEl.InnerText = subComponents[c]; componentEl.AppendChild(subComponentEl); } } fieldEl.AppendChild(componentEl); } else {//2.如果没有字组了,一般是没有的。。。 string[] sRepetitions = HL7ToXmlConverter.GetComponents(sComponents[b]); if (sRepetitions.Length > 1) { XmlElement repetitionEl = null; for (int c = 0; c < sRepetitions.Length; c++) { repetitionEl = _xmlDoc.CreateElement(sFields[0] + "." + a.ToString() + "." + b.ToString() + "." + c.ToString()); repetitionEl.InnerText = sRepetitions[c]; componentEl.AppendChild(repetitionEl); } fieldEl.AppendChild(componentEl); el.AppendChild(fieldEl); } else { componentEl.InnerText = sComponents[b]; fieldEl.AppendChild(componentEl); el.AppendChild(fieldEl); } } } el.AppendChild(fieldEl); } else {//1:如果不可以分隔,可以直接写节点值了。 fieldEl.InnerText = sFields[a]; el.AppendChild(fieldEl); } } else {//0:如果不可以分隔,可以直接写节点值了。 fieldEl.InnerText = sFields[a]; el.AppendChild(fieldEl); } } } } return(_xmlDoc); }