我有一个HL7文件,如:
MSH|^~\&|ABC|000|ABC|ABC|0000||ABC|000|A|00
PID|1|000|||ABC||000|A||||||||||
OBR|1|||00||00|00|||||||||||ABC|00|0|0||||A|||||00||ABC|7ABC||ABC
OBX|1|ABC|ABC|1|SGVsbG8=
OBX|2|ABC|ABC|1|XYTA
OBX|3|ABC|ABC|1|UYYA
我必须只读取OBX段并在第5个管道(|)之后获取文本。
目前我正在这样做:
StreamReader reader = new StreamReader(@"C:\Users\vemarajs\Desktop\HL7\Ministry\HSHS.txt");
string strTest = reader.ReadToEnd();
string OBX1 = strTest.Split('\n')[3];
File.AppendAllText(@"C:\Users\vemarajs\Desktop\Test\OBX.txt", OBX1 + Environment.NewLine);
List<string> list = new List<string>();
using (reader)
{
string line;
while ((line = reader.ReadLine()) != null)
{
list.Add(line);
if (line.StartsWith("OBX|") == true)
{
string txt = line.Split('|')[5];
File.AppendAllText(@"C:\Users\vemarajs\Desktop\Test\test.txt", txt+Environment.NewLine);
}
else
{
//string x = line + Environment.NewLine + OBX1.Distinct();
File.AppendAllText(@"C:\Users\vemarajs\Desktop\Test\newtest.txt", line + Environment.NewLine);
}
File.AppendAllText(@“C:\ Users \ vemarajs \ Desktop \ Test \ newtest.txt”,OBX1.Distinct()。ToList()+ Environment.NewLine);
这将在元素5(在5个管道之后)中提取每个OBX段的内容并写出一个名为test.text的文件,在我的else语句中我试图通过删除OBX | 2和OBX |来修改原始HL7文件。 3只有一个OBX | 1,因为我们期望HL7文件中的OBX段数达到40或更多,我们不希望在将消息返回到其消息框时保留这些段。
如何在不说行号为4的情况下首次出现OBX | 1,这可能会改变。
这是工作代码:
StreamReader reader = new StreamReader(@"C:\Users\vemarajs\Desktop\HL7\Ministry\HSHS.txt");
string strTest = reader.ReadToEnd();
reader.DiscardBufferedData();
reader.BaseStream.Seek(0, SeekOrigin.Begin);
reader.BaseStream.Position = 0;
string OBXstr = string.Empty;
string x = null;
string fileName = Guid.NewGuid().ToString() + ".txt";
StringBuilder sb = new StringBuilder();
List<string> list = new List<string>();
using (reader)
{
string line;
while ((line = reader.ReadLine()) != null)
{
list.Add(line);
if (line.StartsWith("OBX|") == true)
{
string txt = line.Split('|')[5];
File.AppendAllText(@"C:\Users\vemarajs\Desktop\Test\"+fileName, txt + Environment.NewLine);
}
else
{
sb.AppendLine(line);
}
}
int obx1Index = 0;
int obx2Index = 0;
var obx1IDR = "\r" + "OBX" + "|" + "1";
var obx1IDN = "\n" + "OBX" + "|" + "1";
var obx2IDR = "\r" + "OBX" + "|" + "2";
var obx2IDN = "\n" + "OBX" + "|" + "2";
obx1Index = strTest.IndexOf(obx1IDN);
if (obx1Index < 1)
obx1Index = strTest.IndexOf(obx1IDR);
obx2Index = strTest.IndexOf(obx2IDN);
if (obx2Index < 1)
obx2Index = strTest.IndexOf(obx2IDR);
if (obx1Index > 0)
{
OBXstr = strTest.Substring(obx1Index, obx2Index - obx1Index - 1);
OBXstr = OBXstr.Replace(strTest.Substring(obx1Index, obx2Index - obx1Index - 1).Split('|')[5], fileName);
}
}
sb.Append(OBXstr);
x = sb.ToString();
File.WriteAllText(@"C:\Users\vemarajs\Desktop\Test\newtest.txt", x);
reader.Close();
答案 0 :(得分:0)
您的代码存在许多缺陷。
1)这假设OBX1始终是第4行,你需要在循环中移动它,如果是逻辑。
string OBX1 = strTest.Split('\n')[3];
2)是的,你在else语句中为每一行都写了变量OBX1。
3)当你说你想要一个文件/消息时,你正在写多个文件。
你可能想要更像
的东西 StreamReader reader = new StreamReader(@"C:\Users\vemarajs\Desktop\HL7\Ministry\HSHS.txt");
File.AppendAllText(@"C:\Users\vemarajs\Desktop\Test\OBX.txt", OBX1 + Environment.NewLine);
List<string> list = new List<string>();
using (reader)
{
string line;
while ((line = reader.ReadLine()) != null)
{
list.Add(line);
if (line.StartsWith("OBX|1") == true)
{
File.AppendAllText(@"C:\Users\vemarajs\Desktop\Test\test.txt", line + Environment.NewLine);
}
else if (line.StartsWith("OBX|") == true)
{
// Don't write
}
else
{
File.AppendAllText(@"C:\Users\vemarajs\Desktop\Test\test.txt", line + Environment.NewLine);
}
答案 1 :(得分:0)