[英]NHapi using C#.net
我在 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
谁能告诉我为什么? 这到底有什么问题?
在您的代码中,在运行时,将出现NullReferenceException
,因为您正在尝试使用尚未构造的XMLParser
。
因此,考虑将您的代码更改为以下内容:
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
检查InnerException
。 您很可能缺少对System.Configuration.ConfigurationManager
的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.