如何在vb.net中从XML文件中检索相应的值?
我需要从一个XML文件中检索值,我见过很多例子,但没有一个像我看到的XML。 我的XML看起来像这样如何在vb.net中从XML文件中检索相应的值?,xml,vb.net,oracle,Xml,Vb.net,Oracle,我需要从一个XML文件中检索值,我见过很多例子,但没有一个像我看到的XML。 我的XML看起来像这样 <title>TOSSICOLOGIA D'ABUSO</title> <component> <section> <code code="31011" codeSystemName="Codifica Interna
<title>TOSSICOLOGIA D'ABUSO</title>
<component>
<section>
<code code="31011" codeSystemName="Codifica Interna Laboratorio" displayName="Etanolo (urine)">
<!--TRASCODIFICA ANALISI NON DISPONIBILE-->
</code>
<text>
<paragraph>
</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>Etanolo (urine)</td>
<td>< 0,01 g/l</td>
<td></td>
<td></td>
<td>fino a 0,35</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="31011" codeSystemName="Codifica Interna Laboratorio" displayName="Etanolo (urine)">
<!--TRASCODIFICA ANALISI NON DISPONIBILE-->
</code>
<statusCode code="completed" />
<!--(INIZIO) GESTIONE MICROBIOLOGIA MONO RISULTATO -->
<!--(FINE) GESTIONE MICROBIOLOGIA MONO RISULTATO -->
<entryRelationship typeCode="COMP">
<observation classCode="OBS" moodCode="EVN">
<code code="31011" codeSystemName="Codifica Interna Laboratorio" displayName="Etanolo (urine)">
<!--TRASCODIFICA RISULTATI NON DISPONIBILE-->
<!--ANL_COMPLETED-->
</code>
<statusCode code="completed" />
<effectiveTime value="20170216131204" />
<value xsi:type="ST">< 0,01 g/l</value>
<!---->
<referenceRange typeCode="REFV">
<observationRange classCode="OBS" moodCode="EVN.CRT">
<value xsi:type="IVL_PQ">
<low value="0.00" />
<high value="0.35" />
</value>
<interpretationCode code="N" />
</observationRange>
</referenceRange>
<referenceRange typeCode="REFV">
<observationRange classCode="OBS" moodCode="EVN.CRT">
<value xsi:type="ST">fino a 0,35</value>
<interpretationCode code="N" />
</observationRange>
</referenceRange>
</observation>
</entryRelationship>
<!-- VAL USED -->
</act>
<!-- FINE MONO RISULTATO -->
</entry>
</section>
</component>
<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>
阿布索托西科尼亚酒店
以扫
埃西托
异常旗
米苏拉安盟
里弗里门托牧场
条理阵式
依他诺(尿)
0.01克/升
fino a 0,35
对于这部分代码,我可以使用
Dim path As String = "C:\Users\ShkelzenTarja\projekt\CDR_v3_1\CDR\test2.xml" ' "put path to xml file here"
Dim xe As XElement
xe = XElement.Load("C:\Users\ShkelzenTarja\projekt\CDR_v3_1\CDR\test2.xml")
Dim sectn As XElement = (From el In xe...<code> Select el Take 1).FirstOrDefault
If sectn IsNot Nothing Then 'did we get a code
'yes
sectn = sectn.Parent 'the section
'
End If
If sectn IsNot Nothing Then 'did we get a section
'yes
For Each tr As XElement In sectn...<table>.<tbody>.<tr>
For Each td As XElement In tr.<td>
Debug.WriteLine(td.Value)
Next
Next
End If
Dim path As String=“C:\Users\ShkelzenTarja\projekt\CDR\u v3\u 1\CDR\test2.xml”“将xml文件的路径放在此处”
弱xe-As-XElement
加载(“C:\Users\ShkelzenTarja\projekt\CDR\u v3\u 1\CDR\test2.xml”)
Dim sectn As XElement=(从xe中的el…选择el Take 1)。FirstOrDefault
如果sectn不算什么,那么“我们得到代码了吗?”
”“是的
sectn=sectn.Parent“节”
'
如果结束
如果sectn不算什么,那么“我们得到了section吗?”
”“是的
对于第节中的每个tr组件。。。。。
对于每个td作为tr中的元素。
Debug.WriteLine(td.Value)
下一个
下一个
如果结束
但我有一些问题:
1-路径和xe的差值=XElement.Load
2-选择el Take 1)。第一个默认值。。。如何更改此项以获取文档的所有值,而不仅仅是第一个值
3-如果我的xml有两个标题,我应该使用相同的sintax吗?因为事实上,正如您在照片上看到的那样。
感谢您的帮助@dbasnett
我所有的Xml都在这里:
XML示例的格式不正确。这可能会给你一个想法
用于生产代码使用
Dim path As String = "put path to xml file here"
Dim xe As XElement
xe = XElement.Load(path) 'load the xml from file
因为这是VB,所以您可以使用文本进行测试。我尽了最大努力修复XML
Dim xe As XElement
xe = <title>TOSSICOLOGIA D'ABUSO
<component>
<section>
<code code="31011" codeSystemName="Codifica Interna Laboratorio" displayName="Etanolo (urine)"></code>
<text>
<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>Etanolo (urine)</td>
<td>< 0,01 g/l</td>
<td></td>
<td></td>
<td>fino a 0,35</td>
<td/>
</tr>
</tbody>
</table>
<footnote></footnote>
<paragraph></paragraph>
</text>
</section>
</component>
<section>
<code code="32000" codeSystemName="Codifica Interna Laboratorio" displayName="Creatininuria"></code>
<text>
<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>
</text>
</section>
</title>
只是一些想法。希望能有帮助。
一旦我们有了一个部分,我们就可以迭代正文中的行
If sectn IsNot Nothing Then 'did we get a section
'yes
For Each tr As XElement In sectn...<table>.<tbody>.<tr>
For Each td As XElement In tr.<td>
Debug.WriteLine(td.Value)
Next
Next
'
End If
如果sectn不是空的,那么“我们得到了一个节了吗?”
”“是的
对于第节中的每个tr组件。。。。。
对于每个td作为tr中的元素。
Debug.WriteLine(td.Value)
下一个
下一个
'
如果结束
请看下面的链接
解决方案
私人分公司()
Const FILENAME As String=“C:\Users\ShkelzenTarja\projekt\CDR\u v3\u 1\CDR\test.xml”
作为XmlReaderSettings的Dim设置=新的XmlReaderSettings()
settings.ConformanceLevel=ConformanceLevel.Fragment
将读卡器设置为XmlReader=XmlReader.Create(文件名、设置)
作为数据表的Dim dt=新数据表()
添加(“显示名称”,GetType(字符串))
添加(“代码”,GetType(字符串))
Add(“Esame”,GetType(String))
添加(“Esito”,GetType(字符串))
dt.Columns.Add(“异常标志”,GetType(字符串))
Add(“unitadimisura”,GetType(String))
Add(“Range Di Riferimento”,GetType(String))
添加(“Metoda”,GetType(字符串))
添加(“低”,GetType(十进制))
添加(“高”,GetType(十进制))
Add(“Time”,GetType(DateTime))
Dim uri=“urn:hl7组织:v3”
While(不是reader.EOF)
如果是reader.Name“section”,则
reader.ReadToFollowing(“节”,uri)
如果结束
如果不是reader.EOF,则
尺寸部分为XElement=CType(XElement.ReadFrom(reader),XElement)
Dim xCode As XElement=section.subjections()。其中(函数(x)x.Name.LocalName=“code”).FirstOrDefault()
Dim displayName As String=CType(xCode.Attribute(“displayName”),字符串)
对于节.Elements()中作为XElement的每个xComponent,其中(Function(x)x.Name.LocalName=“component”)
Dim xe与XElement=xComponent.subjections()相同。其中(Function(x)x.Name.LocalName=“code”).FirstOrDefault()
作为字符串的Dim代码=CType(xEsame.Attribute(“代码”),字符串)
将xBody作为XElement=xComponent.subjections()进行调整,其中(函数(x)x.Name.LocalName=“tbody”).FirstOrDefault()
Dim数据作为新列表(字符串)
data.AddRange({displayName,code})
data.AddRange(xBody.subscriptions().Where(Function(x)x.Name.LocalName=“td”).Select(Function(x)CType(x,String)))
作为XElement=section.subjections()的Dim条目,其中(函数(x)x.Name.LocalName=“条目”).FirstOrDefault()
Dim low为XElement=entry.subjects()。其中(函数(x)x.Name.LocalName=“low”).FirstOrDefault()
如果低是什么,那么
data.Add(无)
其他的
data.Add(Decimal.Parse(CType(low.Attribute(“value”),String)))
如果结束
If sectn IsNot Nothing Then 'did we get a section
'yes
For Each tr As XElement In sectn...<table>.<tbody>.<tr>
For Each td As XElement In tr.<td>
Debug.WriteLine(td.Value)
Next
Next
'
End If
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))
'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