【问题标题】:Set xml value using visual basic使用 Visual Basic 设置 xml 值
【发布时间】:2020-06-11 13:23:11
【问题描述】:

我需要动态设置 xml 元素值。我几乎完成了它,但我需要在特定部分添加一个条件,它不起作用。 我有一个组件标签,其中有 2 个部分,第一个部分的代码 =“62387-6”,第二个部分的代码 =“47045-0”。 我需要在其中添加特定的标题和文本。

我尝试过的是:

(Declared)
Private _titleReferto As String = "Title Referto"
Private _testoReferto As String = "Testo Referto"
Private _titlePrestazione As String = "Title Prestazione"
Private _testoPrestazione As String = "Testo Prestazione"


(In the function that I invoke)

            Me.setXmlValue(rootNode, Me._titleReferto, "//cr:component/cr:structuredBody/cr:component/cr:section/cr:code[@code='47045-0']/cr:title", NS)
            Me.setXmlValue(rootNode, Me._testoReferto, "//cr:component/cr:structuredBody/cr:component/cr:section[code/@code='" + "47045-0" + "']/cr:text", NS)
            Me.setXmlValue(rootNode, Me._titlePrestazione, "//cr:component/cr:structuredBody/cr:component/cr:section[code/@code='" + "62387-6" + "']/cr:title", NS)
            Me.setXmlValue(rootNode, Me._testoPrestazione, "//cr:component/cr:structuredBody/cr:component/cr:section[code/@code='" + "62387-6" + "']/cr:text", NS)

这就是我需要的xml:

 <component>
    <structuredBody>
            <component>
                <section>
                    <code code="62387-6" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" codeSystemVersion="2.64" displayName="Interventi" />    <!-- OBBL-->  
                    <title> Title Prestazione  </title>   <!-- OBBL-->  
                    <text>   <!-- OBBL-->  
                        <paragraph> 
                            <caption>Testo Prestazione</caption>   
                        </paragraph>
                    </text>
                </section>
            </component>
            <component>
                <section>
                    <code code="47045-0"  codeSystem="2.16.840.1.113883.6.1"  codeSystemName="LOINC"  codeSystemVersion="2.64" displayName="Referto" />    <!-- OBBL-->    
                    <title> Title Referto </title>   <!-- OBBL-->  
                    <text>      <!-- OBBL-->  
                        <paragraph>   
                            Testo Referto
                        </paragraph>   
                    </text> 
                </section>
            </component>
    </structuredBody>
  </component>

【问题讨论】:

    标签: asp.net xml vb.net


    【解决方案1】:

    XElement 似乎更容易使用

        Dim xe As XElement
        ' xe = XElement.Load("path here") 'for production
    
        'for testing use a literal, before the adds
        xe = <component>
                 <structuredBody>
                     <component>
                         <section>
                             <code code="62387-6" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" codeSystemVersion="2.64" displayName="Interventi"/><!-- OBBL-->
                         </section>
                     </component>
                     <component>
                         <section>
                             <code code="47045-0" codeSystem="2.16.840.1.113883.6.1" codeSystemName="LOINC" codeSystemVersion="2.64" displayName="Referto"/><!-- OBBL-->
                         </section>
                     </component>
                 </structuredBody>
             </component>
    
        Dim ie As IEnumerable(Of XElement)
    
        'select 62387-6
        ie = From el In xe...<section> Where el.<code>.@code = "62387-6" Select el Take 1
    
        If ie.Count = 1 Then
            Dim title As XElement = <title>Title Prestazione</title>
            ie(0).Add(title)
            Dim txt As XElement = <text><!-- OBBL-->
                                      <paragraph>
                                          <caption>Testo Prestazione</caption>
                                      </paragraph>
                                  </text>
    
            ie(0).Add(txt)
        End If
    
        'then do the same for 47045-0 changing the values
    
        ' finally 
        '  xe.Save("path here")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-26
      • 1970-01-01
      • 2018-03-13
      • 2021-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多