A
Amr_Aly
Guest
Hello everyone,
I have a good code to Save,Load and remove Duplicates from XML files .... This is my code
'Saving to XML file
xmldoc.Load(XMLpath + "ExData.xml")
With xmldoc.SelectSingleNode("/dataroot").CreateNavigator().AppendChild()
.WriteStartElement("Expenses")
.WriteElementString("Name", cmbExData.Text)
.WriteEndElement()
.Close()
End With
xmldoc.Save(XMLpath + "ExData.xml")
'Load From XML File
'' Read the XML file from disk only once
Dim xDoc = XElement.Load(XMLpath + "ExName.xml")
'' Parse the XML document only once
Dim cbElements = xDoc.<Expenses>.Select(Function n.Value).ToArray()
'' Now fill the ComboBox's
cmbExName.Items.AddRange(cbElements)
'Removing Duplicates
Dim fileName1 As String = XMLpath + "ExName.xml"
xdoc1 = XDocument.Load(fileName1)
' Find the duplicate nodes in the XML document
Dim results = (From n In xdoc1.Descendants("Expenses")
Group n By Item = n.Element("Name").Value.ToLower() Into itemGroup = Group
Where itemGroup.Count > 1
From i In itemGroup.Skip(1)
Select i).ToList()
' Remove the duplicates from xdoc
results.ForEach(Sub(d) d.Remove())
' Save the modified xdoc to the file system
xdoc1.Save(fileName1)
My problem is ( When i load the file again to use a new record after saving it , The list of the combobox appears twice )
in more details i have a combobox list with
(A
B
C) when i use the save code to add "D" and the code of loding XML , the combobox list will be
(A
B
C
A
B
C
D)
I don't know why , I tried to use a remove code in the same button of saving but i get the same results , I will show you the code in my button
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
SaveExData()
SaveExName()
SaveOurTel()
loadExName()
loadExData()
loadOurTel()
RDXmlExName()
RDXmlExData()
RDXmlOurTel()
End Sub
Any suggestions
Thanks
Regards From Egypt
Continue reading...
I have a good code to Save,Load and remove Duplicates from XML files .... This is my code
'Saving to XML file
xmldoc.Load(XMLpath + "ExData.xml")
With xmldoc.SelectSingleNode("/dataroot").CreateNavigator().AppendChild()
.WriteStartElement("Expenses")
.WriteElementString("Name", cmbExData.Text)
.WriteEndElement()
.Close()
End With
xmldoc.Save(XMLpath + "ExData.xml")
'Load From XML File
'' Read the XML file from disk only once
Dim xDoc = XElement.Load(XMLpath + "ExName.xml")
'' Parse the XML document only once
Dim cbElements = xDoc.<Expenses>.Select(Function n.Value).ToArray()
'' Now fill the ComboBox's
cmbExName.Items.AddRange(cbElements)
'Removing Duplicates
Dim fileName1 As String = XMLpath + "ExName.xml"
xdoc1 = XDocument.Load(fileName1)
' Find the duplicate nodes in the XML document
Dim results = (From n In xdoc1.Descendants("Expenses")
Group n By Item = n.Element("Name").Value.ToLower() Into itemGroup = Group
Where itemGroup.Count > 1
From i In itemGroup.Skip(1)
Select i).ToList()
' Remove the duplicates from xdoc
results.ForEach(Sub(d) d.Remove())
' Save the modified xdoc to the file system
xdoc1.Save(fileName1)
My problem is ( When i load the file again to use a new record after saving it , The list of the combobox appears twice )
in more details i have a combobox list with
(A
B
C) when i use the save code to add "D" and the code of loding XML , the combobox list will be
(A
B
C
A
B
C
D)
I don't know why , I tried to use a remove code in the same button of saving but i get the same results , I will show you the code in my button
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles PictureBox2.Click
SaveExData()
SaveExName()
SaveOurTel()
loadExName()
loadExData()
loadOurTel()
RDXmlExName()
RDXmlExData()
RDXmlOurTel()
End Sub
Any suggestions
Thanks
Regards From Egypt
Continue reading...