I need to use a dinamic way to get the value from My XML file.
The code i use to get the value was provide by @jdweng and i works...but i need a dynamic way.. because the problem with this way is if i change the name of tags .. i will need also to go at code to change the name.... but not if i have save the path in a table ..i just go at that table and i change here. That is also the solution
The code that i have for the moment :
Private Sub GetContinents()
Const FILENAME As String = "C: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))
'For i = 0 To data.Count - 1
Insert_CDA_Data(data)
' i = i + 11
'Next
'dt.Rows.Add(data.ToArray())
'Debug.WriteLine(dt)
Next xComponent
End If
End While
End Sub
Private Function Insert_CDA_Data(ByVal data As List(Of String))
Dim esito As Boolean = True
Try
Using scope As New TransactionScope()
operations.INSERT_CDA(ucDataCapture(data))
scope.Complete()
End Using
Catch ex As Exception
esito = False
Dim log As New Log
log.writeLog("Cda", ex)
End Try
Return esito
End Function
Private Function ucDataCapture(ByVal data As List(Of String)) As CDA
Dim CDA_D As New CDA
Try
CDA_D.codeProperty = data(1)
CDA_D.nameProperty = data(0)
CDA_D.esameProperty = data(2)
CDA_D.esitoProperty = data(3)
CDA_D.abnormalFlagProperty = data(4)
CDA_D.unitaDiMisuraProperty = data(5)
CDA_D.rangeDiRiferimentoProperty = data(6)
CDA_D.metodaProperty = data(7)
If data(8) IsNot Nothing Then
CDA_D.lowProperty = data(8)
Else
CDA_D.lowProperty = ""
End If
If data(9) IsNot Nothing Then
CDA_D.highProperty = data(9)
Else
CDA_D.highProperty = ""
End If
CDA_D.effectiveTimeProperty = data(10)
Catch ex As Exception
Dim log As New Log
log.writeLog("Cda ", ex)
End Try
Return CDA_D
End Function
Thank you for your help .
So the table should look like that or tables, because the first title does not have a low or high value, so this value will be take only where there is one .
So i have try this way....
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 that must be take from db.
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
But also i have one like this second Xml
THE problem here it his that the table has more than rows the usually so for them i want to put one database like they were only separed for each row i wanna get -name-code-td value-low-hight-datetime.: