How to parse a string of xml in vb.net

80 views Asked by At

Hi I have an xml mentioned below that comes as a string value from the database. I want to parse the xml string and do not want to save the ones that has empty space i.e this statement means it stores empty space. I need to look for this statement in if condition and not save if empty space. Please let me know how to do that. Below is the vb.net code after the xml

<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)
2

There are 2 answers

2
Yoni Ziv On

You can use String.IsNullOrWhiteSpace(string) to check if it's empty before saving it

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
1
dbasnett On

Maybe this will help

    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