我有一个XML文件,可以从中获取值,并将它们放在数据库的一个表中,我拥有的代码可以正常工作,但是我需要一种动态的方式,这种方式要比我现有的方式效率更高。
My xml file很长,下面是显示她的结构的一部分:
<component>
<section>
<code code="32000" codeSystemName="Codifica Interna Laboratorio" displayName="Creatininuria">
<!--TRASCODIFICA ANALISI NON DISPONIBILE-->
</code>
<text>
<paragraph>
<content ID="ANLNOTE---2-2">Prova autenticità campione droghe</content>
</paragraph>
<table>
<thead>
<tr>
<th>Esame</th>
<th>Esito</th>
<th>Abnormal Flag</th>
<th>Unita di misura</th>
<th>Range di riferimento</th>
<th>Metodo</th>
</tr>
</thead>
<tbody>
<tr>
<td>Creatininuria</td>
<td>193.0</td>
<td></td>
<td>mg/dL</td>
<td>fino a 20: campione non idoneo
(non utilizzabile ai fini medico legali)
20 - 40: campione dubbio
sup. a 40: campione idoneo
</td>
<td />
</tr>
</tbody>
</table>
<footnote></footnote>
<paragraph>
</paragraph>
<!--Inizio Microbiologia sezione humane readable-->
<!--Fine Microbiologia sezione humane readable-->
</text>
<entry typeCode="DRIV">
<!-- INIZIO MONO RISULTATO -->
<act classCode="ACT" moodCode="EVN">
<code code="32000" codeSystemName="Codifica Interna Laboratorio" displayName="Creatininuria">
<!--TRASCODIFICA ANALISI NON DISPONIBILE-->
</code>
<statusCode code="completed" />
<!--(INIZIO) GESTIONE MICROBIOLOGIA MONO RISULTATO -->
<!--(FINE) GESTIONE MICROBIOLOGIA MONO RISULTATO -->
<entryRelationship typeCode="SUBJ">
<act classCode="ACT" moodCode="EVN">
<code code="48767-8" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" displayName="Annotation Comment" />
<text>
<reference value="ANLNOTE---2-2" />
</text>
</act>
</entryRelationship>
<entryRelationship typeCode="COMP">
<observation classCode="OBS" moodCode="EVN">
<code code="32000" codeSystemName="Codifica Interna Laboratorio" displayName="Creatininuria">
<!--TRASCODIFICA RISULTATI NON DISPONIBILE-->
<!--ANL_COMPLETED-->
</code>
<statusCode code="completed" />
<effectiveTime value="20170216121035" />
<value xsi:type="PQ" value="193.0" unit="mg/dL" />
<referenceRange typeCode="REFV">
<observationRange classCode="OBS" moodCode="EVN.CRT">
<value xsi:type="IVL_PQ">
<low value="40.0" unit="mg/dL" />
<high value="99999.0" unit="mg/dL" />
</value>
<interpretationCode code="N" />
</observationRange>
</referenceRange>
<referenceRange typeCode="REFV">
<observationRange classCode="OBS" moodCode="EVN.CRT">
<value xsi:type="ST">fino a 20: campione non idoneo
(non utilizzabile ai fini medico legali)
20 - 40: campione dubbio
sup. a 40: campione idoneo
</value>
<interpretationCode code="N" />
</observationRange>
</referenceRange>
</observation>
</entryRelationship>
<!-- VAL USED -->
</act>
<!-- FINE MONO RISULTATO -->
</entry>
</section>
</component>
我用来获取值的代码是:
Private Sub GetContinents()
Const FILENAME As String = "C:\Users\ShkelzenTarja\projekt\CDR_v3_1\CDR\test.xml"
Dim settings As XmlReaderSettings = New XmlReaderSettings()
settings.ConformanceLevel = ConformanceLevel.Fragment
Dim reader As XmlReader = XmlReader.Create(FILENAME, settings)
Dim dt As DataTable = New DataTable()
dt.Columns.Add("Display Name", GetType(String))
dt.Columns.Add("Code", GetType(String))
dt.Columns.Add("Esame", GetType(String))
dt.Columns.Add("Esito", GetType(String))
dt.Columns.Add("Abnormal Flag", GetType(String))
dt.Columns.Add("Unita Di Misura", GetType(String))
dt.Columns.Add("Range Di Riferimento", GetType(String))
dt.Columns.Add("Metoda", GetType(String))
dt.Columns.Add("Low", GetType(Decimal))
dt.Columns.Add("High", GetType(Decimal))
dt.Columns.Add("Time", GetType(DateTime))
Dim uri = "urn:hl7-org:v3"
While (Not reader.EOF)
If reader.Name <> "section" Then
reader.ReadToFollowing("section", uri)
End If
If Not reader.EOF Then
Dim section As XElement = CType(XElement.ReadFrom(reader), XElement)
Dim xCode As XElement = section.Descendants().Where(Function(x) x.Name.LocalName = "code").FirstOrDefault()
Dim displayName As String = CType(xCode.Attribute("displayName"), String)
For Each xComponent As XElement In section.Elements().Where(Function(x) x.Name.LocalName = "component")
Dim xEsame As XElement = xComponent.Descendants().Where(Function(x) x.Name.LocalName = "code").FirstOrDefault()
Dim code As String = CType(xEsame.Attribute("code"), String)
Dim xBody As XElement = xComponent.Descendants().Where(Function(x) x.Name.LocalName = "tbody").FirstOrDefault()
Dim data As New List(Of String)
data.AddRange({displayName, code})
data.AddRange(xBody.Descendants().Where(Function(x) x.Name.LocalName = "td").Select(Function(x) CType(x, String)))
Dim entry As XElement = section.Descendants().Where(Function(x) x.Name.LocalName = "entry").FirstOrDefault()
Dim low As XElement = entry.Descendants().Where(Function(x) x.Name.LocalName = "low").FirstOrDefault()
If low Is Nothing Then
data.Add(Nothing)
Else
data.Add(Decimal.Parse(CType(low.Attribute("value"), String)))
End If
Dim high As XElement = entry.Descendants().Where(Function(x) x.Name.LocalName = "high").FirstOrDefault()
If high Is Nothing Then
data.Add(Nothing)
Else
data.Add(Decimal.Parse(CType(high.Attribute("value"), String)))
End If
Dim effectiveTime As XElement = entry.Descendants().Where(Function(x) x.Name.LocalName = "effectiveTime").FirstOrDefault()
Dim dateStr As String = CType(effectiveTime.Attribute("value"), String)
data.Add(DateTime.ParseExact(dateStr, "yyyyMMddHHmmss", System.Globalization.CultureInfo.InvariantCulture))
Insert_CDA_Data(data)
'dt.Rows.Add(data.ToArray())
'Debug.WriteLine(dt)
Next xComponent
End If
End While
End Sub
但是这种方式在标签更改名称时会显示问题...因此要解决此问题,我需要修复代码,每次标签更改其名称都很难做到这一点,因此我找到了一种逻辑解决方案,但是我无法实现该解决方案。
我的解决方案是,对于需要获取值的标记,我将其路径保存在数据库的表中。当执行执行时,我们从数据库获取所有路径,然后获取值。
我找到了一个示例,但我还是一个初学者,我不太了解代码。
我要遵循的代码:
Public Function GetTagValue(ByVal RootNode As XmlElement, ByVal XPath As String, ByVal Ns As XmlNamespaceManager,
Optional ByVal IsCodPresc As Boolean = False) As Object
Dim Value As Object
Dim SelectedNode As XmlNode
Dim SelectedNodes As XmlNodeList
Try
If RootNode IsNot Nothing Then
If RootNode.ChildNodes.Count > 0 Or RootNode.Attributes.Count > 0 Then
If IsCodPresc Then
SelectedNodes = RootNode.SelectNodes(XPath, Ns)
Value = New List(Of String)
If SelectedNodes IsNot Nothing Then
For Each Item As XmlNode In SelectedNodes
Value.Add(Item.InnerText.Trim)
Next
End If
Else
SelectedNode = RootNode.SelectSingleNode(XPath, Ns)
If SelectedNode IsNot Nothing Then
Value = SelectedNode.InnerText.Trim
Else
Value = ""
End If
End If
End If
End If
Catch Ex As Exception
Value = ""
End Try
Return Value
End Function
感谢您尝试帮助我。因此,我尝试建立一个示例,但我不知道,但它似乎比第一个复杂。在这里,我还将值“ static”放置。
Imports System.IO
Imports System.Xml
Module CdaParser
Sub Main()
Try
Console.Write("Enter CDA path: ")
Dim FilePath = Console.ReadLine()
If Not File.Exists(FilePath)
throw New Exception("Error! File does not exist.")
End If
'Load CDA
Dim Document = New XmlDocument()
Document.Load(FilePath)
'Load CDA
'Fetch and load namespaces
Dim DocumentNamespaces = New XmlNamespaceManager(Document.NameTable)
DocumentNamespaces.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
Dim XPathNavigator = Document.CreateNavigator()
Dim Namespaces = XPathNavigator.GetNamespacesInScope(XmlNamespaceScope.All)
If Namespaces IsNot Nothing
For Each [Namespace] In Namespaces
DocumentNamespaces.AddNamespace([Namespace].Key, [Namespace].Value)
Next
End If
'Fetch and load namespaces
'Rule definitions
Dim Rules = New Dictionary(Of String, String) From {
{"First Name", "/*[local-name()='ClinicalDocument'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='recordTarget'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='patientRole'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='patient'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='name'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='given'][namespace-uri()='urn:hl7-org:v3']"},
{"Last Name", "/*[local-name()='ClinicalDocument'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='recordTarget'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='patientRole'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='patient'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='name'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='family'][namespace-uri()='urn:hl7-org:v3']"},
{"Test", "/*[local-name()='ClinicalDocument'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='component'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='structuredBody'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='component'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='section'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='component'][namespace-uri()='urn:hl7-org:v3'][4]/*[local-name()='section'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='entry'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='act'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='entryRelationship'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='observation'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='value'][namespace-uri()='urn:hl7-org:v3'][1]/@xsi:type"},
{"Code", "/*[local-name()='ClinicalDocument'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='component'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='structuredBody'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='component'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='section'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='component'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='section'][namespace-uri()='urn:hl7-org:v3'][1]/*[local-name()='code'][namespace-uri()='urn:hl7-org:v3'][1]/@code"},
{"Esame", "/*[local-name()='ClinicalDocument'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='component'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='structuredBody'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='component'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='section'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='component'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='section'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='text'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='table'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='thead'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='tr'][namespace-uri()='urn:hl7-org:v3']/*[local-name()='th'][namespace-uri()='urn:hl7-org:v3']"}}
'Rule definitions
For Each Rule In Rules
Dim Node = Document.SelectSingleNode(Rule.Value, DocumentNamespaces)
Dim Value = ""
If Node IsNot Nothing Then
Value = Node.InnerText
End If
Console.WriteLine(Rule.Key + ": " + Value)
Next
'For Each rule In Rules
' Dim node = Document.SelectNodes(rule.Value, DocumentNamespaces)
' Dim value = ""
' If node IsNot Nothing Then
' value = node.innertext
' End If
'Next
Console.ReadLine()
Catch Exception As Exception
Console.WriteLine(Exception.Message)
Console.ReadLine()
End Try
End Sub
End Module