0

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)
Sri
  • 9
  • 2
  • Not at all clear what you are trying to do? – dbasnett Aug 24 '20 at 16:59
  • I am trying to read the xml string as mentioned above and look for which stores a empty space infact empty textbox in the front end. I donot want to call SaveInspectionSupplementalLineItem(valueXML) if empty textbox or space mentioned as in the above xml statement. Please let me know how to do that – Sri Aug 24 '20 at 17:13
  • @dbasnett - I am trying to read the xml string as mentioned above and look for which stores a empty space infact empty textbox in the front end. I donot want to call SaveInspectionSupplementalLineItem(valueXML) if empty textbox or space mentioned as in the above xml statement. Please let me know how to do that – Sri Aug 24 '20 at 18:59
  • see my answer. XElement and LINQ. – dbasnett Aug 24 '20 at 19:37

2 Answers2

0

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
Yoni Ziv
  • 166
  • 1
  • 10
  • Unfortunately it did not work because I am looking for this statement stores blank space means a blank textbox in the front end so I need to get hold of this tag in xml and when it is empty it should not save – Sri Aug 24 '20 at 16:51
  • If you wish to select a single node and check if the value is empty you could use `XmlDocument.SelectSingleNode({node path}).InnerText` – Yoni Ziv Aug 25 '20 at 07:05
0

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
dbasnett
  • 11,334
  • 2
  • 25
  • 33