簡體   English   中英

如何在vb.net中解析一串xml

[英]How to parse a string of xml in vb.net

嗨,我有一個下面提到的 xml,它是來自數據庫的字符串值。 我想解析 xml 字符串,不想保存那些有空白的字符串,即這個語句意味着它存儲空白。 我需要在 if 條件中查找此語句,而不保存 if 空間。 請讓我知道如何做到這一點。 下面是xml后面的vb.net代碼

<DocumentElement>
  <TBLCustomizedCodes>
    <tblRowkey>-1</tblRowkey>
    <TBLRowCustomizedCodes>test</TBLRowCustomizedCodes>
    <TBLRowCompliance>N/A</TBLRowCompliance>
  </TBLCustomizedCodes>
  <TBLCustomizedCodes>
    <tblRowkey>-2</tblRowkey>
    <TBLRowCustomizedCodes xml:space="preserve">      </TBLRowCustomizedCodes>
    <TBLRowCompliance xml:space="preserve">      </TBLRowCompliance>
  </TBLCustomizedCodes>
  <TBLCustomizedCodes>
    <tblRowkey>-3</tblRowkey>
    <TBLRowCustomizedCodes xml:space="preserve">      </TBLRowCustomizedCodes>
    <TBLRowCompliance xml:space="preserve">      </TBLRowCompliance>
  </TBLCustomizedCodes>
</DocumentElement>
Dim ds As DataSet = New DataSet("DocumentElement")
        ds.Tables.Add(tempdataTable)

        Dim valueXML As String = ds.GetXml().ToString().Trim()
        SaveInspectionSupplementalLineItem(valueXML)

您可以使用String.IsNullOrWhiteSpace(string)在保存之前檢查它是否為空

Dim ds As DataSet = New DataSet("DocumentElement")
ds.Tables.Add(tempdataTable)

Dim valueXML As String = ds.GetXml().ToString().Trim()
If Not String.IsNullOrWhiteSpace(valueXML) Then
    SaveInspectionSupplementalLineItem(valueXML)
End If

也許這會有所幫助

    Dim xe As XElement
    Dim valueXML As String = ds.GetXml() '.ToString() '??? 
    xe = XElement.Parse(valueXML)
    Dim ie As IEnumerable(Of XElement)
    ie = From el In xe.Descendants Where el.Value.Trim = "" Select el
    If ie.Count > 0 Then
        'there WERE Elements with empty space
        Stop 'look at ie.Results
    Else
        'no empties
    End If

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM