删除xml元素和父节点

时间:2019-03-07 12:16:06

标签: xml vb.net visual-studio

由于某种原因,当我在文本框中输入“ SDB0000013E”或“ SDB0000012E”以外的任何其他值时,不会删除xml代码。如果我想删除我的xml(SDB0000012E)的第一位,则可以正常工作。我不明白这种情况正在发生。任何帮助都会很棒。谢谢

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 
    Dim xdoc As XmlDocument
    Dim nodelist As XmlNodeList

    xdoc = New XmlDocument
    Dim xmlData As String = path & FileXml
    xdoc.Load(xmlData)
    nodelist = xdoc.GetElementsByTagName("DataTable")

    Dim intNdeCnt As Integer = 0
    Dim lstNodeErrs As New List(Of String)

    For Each node1 As XmlElement In nodelist

        intNdeCnt += 1
        lstNodeErrs.Add("------ Checking node: " & intNdeCnt)
        REFNO = node1("REFNO ").InnerText.Trim
        MEMO_NO = node1("MEMO_NO").InnerText.Trim
        Code = node1("Code").InnerText.Trim

       If CheckBox2.Checked Then
                Dim DocPath As String = IO.Path.Combine(path, "data.xml")
                Dim doc As XElement
                doc = XElement.Load(DocPath) 'to load from file

                'use TextBox1 to match ASI_REF
                Dim ie As IEnumerable(Of XElement)
                ie = From el In doc...<ASI_REF>
                     Where el.Value = TextBox1.Text
                     Select el

                For idx As Integer = ie.Count - 1 To 0 Step -1
                    Dim xe As XElement = ie(idx)

                    xe.Parent.Remove() 'this removes the DataTable node
                    MessageBox.Show("Deleted: " & REFNO.ToString)
                Next
                doc.Save(DocPath)
        End If

        ' -- Test data

        If String.IsNullOrEmpty(REFNO) Then
            lstNodeErrs.Add("REFNO Is blank.")
        END If


        If lstNodeErrs.Count > 1 Then

                For Each strNodErr As String In lstNodeErrs
                    lstErrs.Add(strNodErr)
                Next
                lstErrs.Add("")

                If CheckBox1.Checked Then
                    lstErrs.Add(vbCrLf & node1("REFNO").ParentNode.OuterXml)

                End If

            End If

            lstNodeErrs.Clear()

        Next
  

xml代码

<?xml version="1.0" encoding="us-ascii" standalone="yes"?>
<data_xml>
    <DataTable>
        <REFNO>SDB0000012E</REFNO>
        <MEMO_NO>5191647556</MEMO_NO>
        <Code>0145191647556</Code>
    </DataTable>
    <DataTable>
        <REFNO>SDB0000013E</REFNO>
        <MEMO_NO>5191647504</MEMO_NO>
        <Code>0145191647504</Code>
    </DataTable>
    <DataTable>
        <REFNO>SDB0000014E</REFNO>
        <MEMO_NO>5191647504</MEMO_NO>
        <Code>0145191647504</Code>
    </DataTable>
</data_xml>
  

即使我在文本框中键入“ SDB0000013E”,但我仍然设置断点时,它仍在在线上读取SDB0000012E:

If elem.InnerText = TextBox1.Text Then 
  

在InnerText位上读取SDB0000012E,但在文本上方读SDB0000013E。

2 个答案:

答案 0 :(得分:0)

这适用于文字

    Dim DocPath As String = IO.Path.Combine(Path, "data.xml")
    If CheckBox2.Checked Then
        Dim doc As XElement
        'doc = XElement.Load(DocPath) 'to load from file
        'if not Testing i.e. XElement.Load(DocPath)
        'remove the following doc = through 'end for Testing
        'for Testing
        doc = <data_xml>
                  <DataTable>
                      <REFNO>SDB0000012E</REFNO>
                      <MEMO_NO>5191647556</MEMO_NO>
                      <Code>0145191647556</Code>
                  </DataTable>
                  <DataTable>
                      <REFNO>SDB0000013E</REFNO>
                      <MEMO_NO>5191647504</MEMO_NO>
                      <Code>0145191647504</Code>
                  </DataTable>
                  <DataTable>
                      <REFNO>SDB0000012E</REFNO>
                      <MEMO_NO>5191647556</MEMO_NO>
                      <Code>0145191647556</Code>
                  </DataTable>
              </data_xml>
        'end for Testing

        'use TextBox1 to match REFNO
        Dim ie As IEnumerable(Of XElement)
        ie = From el In doc...<REFNO>
             Where el.Value = TextBox1.Text
             Select el

        ''??? is there only one possible match?
        'If ie.Count > 0 Then
        '    'not sure what you want here
        '    'ie(0).Remove() 'this removes the REFNO node
        '    'ie(0).Parent.Remove() 'this removes the DataTable node

        '    ' doc.Save(DocPath)
        'End If

        'if multiple REFNO node matches TextBox1.Text
        For idx As Integer = ie.Count - 1 To 0 Step -1
            Dim xe As XElement = ie(idx)
            'not sure what you want here
            'xe.Remove() 'this removes the REFNO node
            xe.Parent.Remove() 'this removes the DataTable node
        Next
        ' doc.Save(DocPath) ' save once if doing multiple
    End If

编辑:这可以使用文件

    If CheckBox2.Checked Then
        Dim DocPath As String = IO.Path.Combine(Path, "data.xml")
        Dim doc As XElement
        doc = XElement.Load(DocPath) 'to load from file

        'use TextBox1 to match REFNO
        Dim ie As IEnumerable(Of XElement)
        ie = From el In doc...<REFNO>
             Where el.Value = TextBox1.Text
             Select el

        For idx As Integer = ie.Count - 1 To 0 Step -1
            Dim xe As XElement = ie(idx)
            'not sure what you want here
            'xe.Remove() 'this removes the REFNO node
            xe.Parent.Remove() 'this removes the DataTable node
        Next
        doc.Save(DocPath) ' save once if doing multiple
    End If

答案 1 :(得分:0)

这是我测试的内容。它还有一些注释,包括文件的前后。

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    If CheckBox2.Checked Then
        Dim DocPath As String = IO.Path.Combine(FolderNM, "data.xml")
        Dim DOC As XElement
        DOC = XElement.Load(DocPath) 'to load from file
        'DOC contents before
        ''   <data_xml>
        ''     <DataTable>
        ''       <REFNO>SDB0000012E</REFNO>
        ''       <MEMO_NO>5191647556</MEMO_NO>
        ''       <Code>0145191647556</Code>
        ''     </DataTable>
        ''     <DataTable>
        ''       <REFNO>SDB0000013E</REFNO>
        ''       <MEMO_NO>5191647504</MEMO_NO>
        ''       <Code>0145191647504</Code>
        ''     </DataTable>
        ''     <DataTable>
        ''       <REFNO>SDB0000012E</REFNO>
        ''       <MEMO_NO>5191647556</MEMO_NO>
        ''       <Code>0145191647556</Code>
        ''     </DataTable>
        ''   </data_xml>

        'use TextBox1 to match REFNO
        ' Test TextBox1 = SDB0000012E
        Dim ie As IEnumerable(Of XElement)
        ie = From el In DOC...<REFNO>
             Where el.Value = TextBox1.Text
             Select el

        For idx As Integer = ie.Count - 1 To 0 Step -1
            Dim xe As XElement = ie(idx)
            xe.Parent.Remove() 'this removes the DataTable node
        Next

        'DOC contents after
        ''  <data_xml>
        ''    <DataTable>
        ''      <REFNO>SDB0000013E</REFNO>
        ''      <MEMO_NO>5191647504</MEMO_NO>
        ''      <Code>0145191647504</Code>
        ''    </DataTable>
        ''  </data_xml>
        DOC.Save(DocPath) ' save once if doing multiple
    End If
End Sub

唯一不起作用的方法是XML不同。运行此命令会得到什么?