我在c#.net中使用nHapi,通过以下代码将hl7消息转换为xml格式。我的代码:
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();
}}}现在它显示:
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有人能告诉我为什么吗?这里面到底有什么问题呢?
发布于 2018-05-21 18:34:52
在您的代码中,由于您正在尝试使用尚未构造的XMLParser,因此在运行时会有一个NullReferenceException。
因此,考虑将您的代码更改为以下代码:
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();
}
}
}DefaultXMLParser是抽象的XMLParser的一种实现。此外,请考虑删除邮件末尾的圆点。
输出如下:
<ADT_A01 xmlns="urn:hl7-org:v2xml">
<MSH>
<MSH.1>|</MSH.1>
<MSH.2>^~\&</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>这本书可能会很有用:HL7 Integration with NHapi
发布于 2021-03-30 18:47:23
检查InnerException。很可能您遗漏了对System.Configuration.ConfigurationManager的引用。
https://stackoverflow.com/questions/49362899
复制相似问题