【问题标题】:How to create an xml element in a specific position using vb.net?如何使用 vb.net 在特定位置创建 xml 元素?
【发布时间】:2020-03-24 15:33:09
【问题描述】:

我正在尝试在另一个元素之后在特定位置创建一个 xml 元素,但该元素被添加到另一个元素中并添加了 xmlns 部分。我也不想要'xmlns'。 我需要补充的是:

Where I need the element to be added

我试过的是这样的:

        Dim cda As New XmlDocument
        Dim refChild As XmlNode = cda.SelectSingleNode("//cr:recordTarget/cr:patientRole/cr:id", NS)
        Dim newChild As XmlElement = cda.CreateElement("id")
        newChild.SetAttribute("root", "2.16.840.1.113883.2.9.4.3.1")
        newChild.SetAttribute("extension", "DLCVCN48S05L049B")
        refChild.InsertBefore(newChild, refChild.FirstChild)

会发生什么:

 <id root="2.16.840.1.113883.2.9.4.3.2" extension="PTRFMN46E69D171X" 
 assigningAuthorityName="Ministero Economia e Finanze">
 <id root="2.16.840.1.113883.2.9.4.3.1" extension="DLCVCN48S05L049B" xmlns="" />
  </id>

【问题讨论】:

    标签: xml vb.net xsd cda


    【解决方案1】:

    根据您的照片,这似乎是您想要做的(Basdandosi nella tua immagine sembra sia questo quello che stai cercando):

        refChild.AppendChild(newChild) 
    

    但是。根据您的问题,您似乎想这样做(Ma basandosi nella domanda):

        Dim newChild As Xml.XmlElement = cda.CreateElement("id", cda.DocumentElement.NamespaceURI)
        newChild.SetAttribute("root", "2.16.840.1.113883.2.9.4.3.1")
        newChild.SetAttribute("extension", "DLCVCN48S05L049B")
        newChild.InnerText = " "
    
    
        If refChild.ParentNode IsNot Nothing Then
            refChild.ParentNode.InsertAfter(newChild, refChild)
        End If
    

    【讨论】:

    • 谢谢!在特定位置添加 xml 元素效果很好! xmlns 再次显示,试图找到另一个选择。 :)
    • Ti ho aggiornato la risposta (fai attenzione alla creazione del nodo) per quanto riguarda l'attributo “xmlns”。法米萨佩尔。祝你好运;)
    【解决方案2】:

    使用 Xml Linq。找到 patientRole ,然后将新的 id 添加到 patientRole :

    Imports System.Xml
    Imports System.Xml.Linq
    Module Module1
        Const FILENAME As String = "c:\temp\test.xml"
        Sub Main()
            Dim doc As XDocument = XDocument.Load(FILENAME)
            Dim patientRole = doc.Descendants().Where(Function(x) x.Name.LocalName = "patientRole").FirstOrDefault()
    
            Dim newPatient As XElement = New XElement("id", New Object() {New XAttribute("root", "2.16.840.1.113883.2.9.4.3.2"), New XAttribute("extension", "PTRFMN46E69D171X"), New XAttribute("assigningAuthorityName", "Ministero Economia e Finanze")})
            patientRole.Add(newPatient)
        End Sub
    
    End Module
    

    【讨论】:

      猜你喜欢
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多