Resolved element.Add check if it exist before add

SigsterSal

New member
Joined
Oct 7, 2024
Messages
3
Programming Experience
Beginner
Hi

How can I check if Test exist before add


VB.NET:
  Function UpdateXmlElement(ByVal element As XElement) As XElement

        If element.Name.LocalName.Equals("ContentSources") Then
            element.Add(New XElement("ContentSource", "Test"))
            Return element
        Else
            For Each child As XElement In element.Elements()
                UpdateXmlElement(child)
            Next
        End If

        Return element

    End Function



    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim cuixFile As String = "C:\map\CUI\ORGINAL.cuix"

        If Not File.Exists(cuixFile) Then Return

        Using zip As ZipArchive = ZipFile.Open(cuixFile, ZipArchiveMode.Update)
            'the Header.cui, which is an XML file
            Dim entry As ZipArchiveEntry = zip.GetEntry("Header.cui")

            If entry.FullName.Equals("Header.cui") Then
                'We need to append our custom xaml file to ContentSources element available in Header CUI
                Dim fn As String = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName())
                entry.ExtractToFile(fn)

                Dim element As XElement = XElement.Load(fn)
                ' MsgBox(element.ToString)
                Dim contentSource As XElement = UpdateXmlElement(element)
                contentSource.Save(fn)
                entry.Delete()
                zip.CreateEntryFromFile(fn, "Header.cui")
                File.Delete(fn)
            End If
        End Using
    End Sub
 
Last edited:
Solution
Something wrong with my test yesterday, this should work:
VB.NET:
If Not element.<ContentSource>.Any(Function(el) el.Value = "Test") Then 'add it
VB.NET:
If Not element.<ContentSource>.Value = "Test" Then
 
Thanks, it still add Test More than once

this is the XML file

VB.NET:
      If element.Name.LocalName.Equals("ContentSources") Then

            If Not element.<ContentSource>.Value = "Test" Then
                element.Add(New XElement("ContentSource", "Test"))
            Else
            End If

        Else
            For Each child As XElement In element.Elements()
                UpdateXmlElement(child)

            Next
        End If

        Return element


VB.NET:
<?xml version="1.0" encoding="utf-8"?>
<CustSection xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <FileVersion MajorVersion="0" MinorVersion="6" IncrementalVersion="1" UserVersion="1" />
  <Header>
    <FileReferences>
      <ToolTipFileReferences>
        <ContentSources>
          <ContentSource>AcCommandToolTips;component/CommandToolTips.xaml</ContentSource>
          <ContentSource>VerticalTips;component/CommandToolTips.xaml</ContentSource>
        </ContentSources>
      </ToolTipFileReferences>
    </FileReferences>
    <CommonConfiguration>
      <CommonItems>
        <ModifiedRev MajorVersion="18" MinorVersion="0" UserVersion="0" />
        <PartialMenuFile>custom.cuix</PartialMenuFile>
      </CommonItems>
    </CommonConfiguration>
  </Header>
</CustSection>
 
Something wrong with my test yesterday, this should work:
VB.NET:
If Not element.<ContentSource>.Any(Function(el) el.Value = "Test") Then 'add it
 
Solution
Back
Top