简体   繁体   中英

NHapi using C#.net

I am using nHapi in c#.net to convert hl7 messages to xml format using the following code. My code:

using System;
using NuGet;
using NHapi.Model;
using NHapi.Model.V231;
using NHapi.Model.V231.Message;
using NHapi.Base.Parser;
using NHapi.Base.Model;

namespace HL7parser
{
    class Program
    {
      static void Main(string[] args)
          {
             String msg = 
         "MSH|^~\\&|HIS|RIH|EKG|EKG|199904140038||ADT^A01||P|2.2\r........."
         PipeParser parser = new PipeParser();
        try {

            IMessage mssg =parser.Parse(msg);
            XMLParser xMLParser=null;
            String str=xMLParser.Encode(mssg);
            Console.WriteLine(str);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.Message);
            Console.WriteLine(e.GetType().Name);
            Console.WriteLine(e.StackTrace);
        }
        Console.WriteLine("\n Press Enter to continue...");
        Console.Read();
           }}}

Now it shows:

The type initializer for 'NHapi.Base.PackageManager' threw an exception.
TypeInitializationException
   at NHapi.Base.Parser.ParserBase.Parse(String message)
   at HL7parser.Program.Main(String[] args) in C:\Users\Administrator\source\repos\HL7parser\HL7parser\Program.cs:line 28

Can anyone please tell me why? what is exactly wrong in this?

In your code, at run-time, there will be a NullReferenceException since you are trying to use an XMLParser not yet constructed.

So, consider changing your code to the following:

using System;
using NHapi.Model;
using NHapi.Model.V231;
using NHapi.Model.V231.Message;
using NHapi.Base.Parser;
using NHapi.Base.Model;
using System.Diagnostics;

namespace HL7parser
{
    class Program
    {
        static void Main(string[] args)
        {
            String msg =
            "MSH|^~\\&|HIS|RIH|EKG|EKG|199904140038||ADT^A01||P|2.2\r";
            PipeParser parser = new PipeParser();
            try
            {
                IMessage mssg = parser.Parse(msg);
                XMLParser xMLParser = null;

                xMLParser = new DefaultXMLParser(new DefaultModelClassFactory());


                String str = xMLParser.Encode(mssg);
                Debug.Print(str);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(e.GetType().Name);
                Console.WriteLine(e.StackTrace);
            }
            Console.WriteLine("\n Press Enter to continue...");
            Console.Read();
        }
    }
}

The DefaultXMLParser is an implementation of XMLParser which is Abstract. Also, consider removing the dots at the end of your message.

the output is the following:

<ADT_A01 xmlns="urn:hl7-org:v2xml">
  <MSH>
    <MSH.1>|</MSH.1>
    <MSH.2>^~\&amp;</MSH.2>
    <MSH.3>HIS</MSH.3>
    <MSH.4>RIH</MSH.4>
    <MSH.5>EKG</MSH.5>
    <MSH.6>EKG</MSH.6>
    <MSH.7>
      <TS.1>199904140038</TS.1>
    </MSH.7>
    <MSH.9>
      <CM_MSG.1>ADT</CM_MSG.1>
      <CM_MSG.2>A01</CM_MSG.2>
    </MSH.9>
    <MSH.11>P</MSH.11>
    <MSH.12>2.2</MSH.12>
  </MSH>
</ADT_A01>

This book may be useful: HL7 Integration with NHapi

Check the InnerException . Most likely you are missing a reference to System.Configuration.ConfigurationManager .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM