[英]NHapi using C#.net
I am using nHapi in c#.net to convert hl7 messages to xml format using the following code.我在 c#.net 中使用 nHapi,使用以下代码将 hl7 消息转换为 xml 格式。 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.在您的代码中,在运行时,将出现
NullReferenceException
,因为您正在尝试使用尚未构造的XMLParser
。
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. DefaultXMLParser
是抽象的XMLParser
的实现。 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>^~\&</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这本书可能有用: HL7 Integration with NHapi
Check the InnerException
.检查
InnerException
。 Most likely you are missing a reference to System.Configuration.ConfigurationManager
.您很可能缺少对
System.Configuration.ConfigurationManager
的引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.