XML/HL7 - extract segments for review

Categories: .Net   hl7   mirth   Mirth   Technology   XML

A situation I run into regularly is needing to parse a bunch (10 to 10,000 or more) HL7 records to review a particular segment for trends.  For example, review all of the messages received on a particular day to see how many different patient types (PV1.18) we received.)

Here’s how I do it:

  1. I use Mirth Connect as my receiver, and save messages to files in version 3.  I save a copy of the original v2.x message I get, in case I need to resend any messages, but I like v3 better.  It’s XML, and in >net, that’s easy to pull out.

  2. The segments in the XML are addressed like so:  <PID.1><PID.1.1>1</PID.1.1></PID.1>  So, if I know what I want, say the account numbers, I just need to look for the <PV1.18.1> element in the XML.

  3. In C#, reading the nodes in XML can be something like this:

public string ReadXMLNode(XmlDocument xml, string Node)
{
  StringBuilder result = new StringBuilder();

  XmlElement root = xml.DocumentElement;
  XmlNodeList nodes = root.SelectNodes(string.Format("//{0}", Node));

  foreach (XmlNode item in nodes)
  {
    if (result.Length > 0)
      result.Append("|");

    result.Append(item.InnerText);
  }
  return result.ToString();
}
  1. From here, it’s pretty simple…go through every file in the directory, read in the file as XML, and then pull out the info needed:
public void ReadFiles()
{ 
  List<string> l = new List<string>(); //This is a list of string. HTML parsing is making it funky
  l.Add("Account Number");

  foreach (FileInfo item in new DirectoryInfo(sourceDirectory).GetFiles("*.xml"))
  {
    XmlDocument xml = new XmlDocument();
    using (StreamReader r = new StreamReader(item.FullName))
    {
      string hl7 = r.ReadToEnd();
      xml.LoadXml(hl7); }

     StringBuilder record = new StringBuilder();
     record.AppendFormat("{0},", rc.ReadXMLNode(xml, "PID.18.1"));

     l.Add(record.ToString());
    }
  }
}
  1. Do something with the data. As we used to say in math, I’ll leave that as an exercise for the reader.

Hopefully you find this useful. I can post a full solution, if anyone would find it useful

Written on January 17, 2013