Help with updating xml file using LinQ

Hi

I'm using the code below to update data elements based on if  already exist or add new country if does not already exist. But I'm getting error message:

Newvalues is not a member of 'System.EventArgs"

On line: For Each item As DictionaryEntry In e.NewValues

How do I solve this issue?

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        Try
            Dim doc As XDocument = XDocument.Load(Application.StartupPath + "\CountryOrigin.xml")
            Dim elemt As XElement = doc.Elements().Descendants().Where(Function(c) c.Attribute("CountryOrigin_ID").Value = e.NewValues(0).ToString().First())
            For Each item As DictionaryEntry In e.NewValues
                elemt.Attribute(item.Key.ToString()).Value = item.Value.ToString()
            Next item
            doc.Save(Application.StartupPath + "\CountryOrigin.xml")
        Catch ex As Exception
            MsgBox("Error")
        End Try
    End Sub

xml file:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Root>
  <CountryOriginTable>
    <CountryOrigin_ID>1</CountryOrigin_ID>
    <CountryOrigin>BEL</CountryOrigin>
  </CountryOriginTable>
  <CountryOriginTable>
    <CountryOrigin_ID>2</CountryOrigin_ID>
    <CountryOrigin>USA</CountryOrigin>
  </CountryOriginTable>
  <CountryOriginTable>
    <CountryOrigin_ID>3</CountryOrigin_ID>
    <CountryOrigin>ESP</CountryOrigin>
  </CountryOriginTable>
  <CountryOriginTable>
    <CountryOrigin_ID>4</CountryOrigin_ID>
    <CountryOrigin>DNK</CountryOrigin>
  </CountryOriginTable>
</Root>
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

David Johnson, CD, MVPOwnerCommented:
not real helpful code but it reads in the xml file.
Imports System.Xml
Imports System.Windows.Forms

Public Class Form1


    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim Filename As String
        Dim CoutryCode As String
        Filename = Application.StartupPath & "\CountryOrigin.xml"
        Try
          Dim doc As New XmlDocument()
            doc.Load(Filename)

            'Display all the book titles. 
            Dim elemList As XmlNodeList = doc.GetElementsByTagName("CountryOrigin")
            Dim i As Integer
            For i = 0 To elemList.Count - 1

                CoutryCode = (elemList(i).InnerXml)
                Label1.Text = CoutryCode
                '
            Next i
            doc.Save(Application.StartupPath + "\CountryOrigin.xml")
        Catch ex As Exception
            MsgBox("Error")
        End Try
    End Sub


    Private Function doc() As Object
        Throw New NotImplementedException
    End Function

End Class

Open in new window

0
vcharlesAuthor Commented:
I'm afraid the code didn't help me solve the issue, below is the code I posted that was initially in C#, I'm trying to get it to work in VB.NET from the button click event.

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)    
{    
try    
{    
XDocument doc = XDocument.Load(Server.MapPath("sample.xml"));    
XElement elemt = doc.Elements().Descendants().Where(c => c.Attribute("Equip_ID").Value == e.NewValues[0].ToString()).First();    
foreach (DictionaryEntry item in e.NewValues)    
{    
elemt.Attribute(item.Key.ToString()).Value = item.Value.ToString();    
}    
doc.Save(Server.MapPath("sample.xml"));    
}    
catch (Exception ex)    
{    
Response.Write(ex.Message);    
}    
}
0
Gary DavisDir Internet SvcsCommented:
The e parameter should be defined as GridViewUpdateEventArgs instead of System.EventArgs.

I did not try this but looking at the C# code, this was an obvious difference.

Gary Davis
0
SAMIR BHOGAYTAFreelancer and IT ConsultantCommented:
hi.. Please try this example it works..

XElement doc = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/XMLFile.xml");
        string quantity = doc.Descendants("items")
        .Select(y => y.Element("quantity").Value)
        .Where(x => x.Element("productname").Value.Equals(cbProduct.text) && x.Element("brandname").Value.Equals(cbBrandName.text))
        .Single().Element("quantity").Value;

UPDATING XML VALUE

 doc.Descendants("items")
    .Where(x => x.Element("productname").Value.Equals(cbProduct.Text) && x.Element("brandname").Value.Equals(cbBrandName.Text))
    .Single().Element("quantity").SetValue(quantity);
        doc.Save(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/XMLFile.xml");
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Hi,

Can you please send me the complete code. I tried it in the button click event but getting lots of errors.

Code:

XElement doc = XElement.Load(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/XMLFile.xml");
        string quantity = doc.Descendants("items")
        .Select(y => y.Element("quantity").Value)
        .Where(x => x.Element("productname").Value.Equals(cbProduct.text) && x.Element("brandname").Value.Equals(cbBrandName.text))
        .Single().Element("quantity").Value;

UPDATING XML VALUE

 doc.Descendants("items")
    .Where(x => x.Element("productname").Value.Equals(cbProduct.Text) && x.Element("brandname").Value.Equals(cbBrandName.Text))
    .Single().Element("quantity").SetValue(quantity);
        doc.Save(AppDomain.CurrentDomain.BaseDirectory + "/App_Data/XMLFile.xml");

Thanks,

Victor
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.