We help IT Professionals succeed at work.

Help with saving data to xml file

Hello,

I am trying to save data to Manufacturer_ID but for some reason the data does not save, any ideas what is wrong? I was able to verify the value of searchIdManufacturer exist by using  MsgBox(searchIdManufacturer) but the value is not save in LibkA.xml. Further below is an example of the LinkA.xml fiule.
 

Dim xda As New Xml.XmlDocument
   Dim xna As Xml.XmlNode


xda.Load(Server.MapPath("~/App_Data/LINKA.XML"))
            xna = xda.SelectSingleNode("/Root/LinkA[LinkAID='" & dtTable.Rows(CurrentIndex).Item("LinkID2") & "']")
            MsgBox(searchIdManufacturer)
            xna.SelectSingleNode("Manufacturer_ID").InnerText = searchIdManufacturer


Dim anadd As String
            anadd = MsgBox("Are you sure you want to save your changes?", MsgBoxStyle.YesNo, "Warning before Attempting to Save changes")
            If anadd = MsgBoxResult.Yes Then
                xda.Save(Server.MapPath("~/App_Data/LINKA.XML"))
         
        Dim Msg As String = "All Changes Have Been Saved"
                ScriptManager.RegisterStartupScript(Me.Page, Me.[GetType](), "temp", "<script type='text/javascript'>alert('" & Msg & "');</script>", False)
            End If


LinkA.xml

<LinkA>
    <LinkAID>1</LinkAID>
    <Manufacturer_ID>1</Manufacturer_ID>
    <Date_ID>1</Date_ID>
    <Image_ID>1</Image_ID>
  </LinkA>

The new value of Manufacture_ID is 20 but it doesn't replace the value of 1. I think something must be wrong with the following code:

 xna = xda.SelectSingleNode("/Root/LinkA[LinkAID='" & dtTable.Rows(CurrentIndex).Item("LinkID2") & "']")

But still can't figure it out.
Comment
Watch Question

Scott McDaniel (EE MVE )Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
SelectSingleNode returns an XMLNode. Assuming the XML file you posted above is the full XML file, then try this:

xna = xda.SelectSingleNode("LinkA/LinkAID")

If you need to get the data from that node, do this:

Dim s As String
s = xns.InnerText

Or to write the data, just do this:

xda.SelectSingleNode("LinkA/Manufacturer_ID").InnerText = searchIdManufacturer
Rajar AhmedConsultant
CERTIFIED EXPERT

Commented:
For this selection You should have root tag on your xml file .
 xna = xda.SelectSingleNode("/Root/LinkA[LinkAID='" & dtTable.Rows(CurrentIndex).Item("LinkID2") & "']")

Open in new window


<?xml version="1.0" encoding="utf-8"?>
<Root>
  <LinkA>
    <LinkAID>1</LinkAID>
    <Manufacturer_ID>70</Manufacturer_ID>
    <Date_ID>1</Date_ID>
    <Image_ID>1</Image_ID>
  </LinkA>
  <LinkA>
    <LinkAID>2</LinkAID>
    <Manufacturer_ID>60</Manufacturer_ID>
    <Date_ID>1</Date_ID>
    <Image_ID>1</Image_ID>
  </LinkA>
</Root>

Open in new window

Author

Commented:
Hi,

Sorry for the confusion . The Root tag is already included.

Victor

Author

Commented:
Hi,

I tried  xda.SelectSingleNode("Root/LinkA/Manufacturer_ID").InnerText = searchIdManufacturer

But it saved the data in the first record/Data element where LinkAID = 0, How do I save the data in the correct record/dtata element? for example where LinkAID = 1, which is the value of LinkID2.

Victor
Rajar AhmedConsultant
CERTIFIED EXPERT

Commented:
Check what you are receiving on this . it will be updated according to this value

MsgBox(dtTable.Rows(CurrentIndex).Item("LinkID2") ) 

Open in new window

Author

Commented:
No value is returned, not even a blank value. I will include the code for this function  in the sample project from earlier and send it for your evalution.
Scott McDaniel (EE MVE )Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
SelectSingleNode will return the first Node that matches, so you can't get to the "next" node with that construct.

If you have multiple Nodes defined with <LinkA>,<LinkAID>, then you'll have to load a NodeList that contains everything matchine your search pattern, then loop through that node list and examine the InnerText of /Root/LinkA/LinkAID to see which one you need to update.

Could you show the EXACT structure of your XML file? That would help a lot.

Author

Commented:
Problem is, it is selecting a node with the wrong ID. Working on a sample project with the complete code and xml filese, will send it shortly. Thanks.

Author

Commented:
Hi,

Below is the link to the sample project, when you select L112A1 fromt the combobox, a text box will show BMW, when you modify it to BMWXYZ and press save, a new record is created in manufacturer.xml to save the new data, which is correct, but the new ID created for BMWXYZ is not saved in LinkA.xml for LinkAID =1 or LinkAID = 2. For some reason my LinkID2 is =2 since two records match, but it should be 1. I think this is related to my previous issue where I am unable to view one recird at a time.

http://speedy.sh/EXrBA/WebApplication13.zip

Thanks,

Victor
Scott McDaniel (EE MVE )Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
You'll have to get the NodeList collection, I'd think, and then loop through them to determine which Node you want to update.
Scott McDaniel (EE MVE )Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
I don't see your code for working with the XML, nor did you include an XML file in the zip.

Can you post your XML file?

Author

Commented:
The xml  files are in the  App_Data folder. Below is another link to the zip file. the code is included in <part 2 (Save data in xml File) of the form.

http://speedy.sh/XnARs/WebApplication13.zip

V.
Scott McDaniel (EE MVE )Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
I see the xml now, thanks. I can't run the project, since I don't have the ComponentSource suite of controls.

My earlier comment still stands - you'd need to get an xmlNodeList collection of all those Nodes, and then loop through them to find the InnerText value you're lookin for, and then furtner set the Manufacturer_ID value for that NodeList. Something like:

            Dim xNodes As XmlNodeList = domFix.SelectNodes("Root/LinkID")
            Dim xNode As XmlNode
            For Each xNode In xNodes
                If xNode.InnerText = "1" Then
                    Dim xManuf As XmlNode = xNode.SelectSingleNode("Manufacturer_ID")
                    xManuf.InnerText = "YourValue"
                End If
            Next

Open in new window

I couldn't test this, of course, so it may have some syntax errors, but hopefully this will help.

Author

Commented:
Thanks, I will try it and get back to you. I think you can run the project when you replace the third party combobox with the one that comes with VS.

Author

Commented:
Hi,

I'm afraid it still did not work. I am getting the right value when I test the data with MsgBox(searchIdManufacturer) , but the data still did not save in my LinkA.xml file. Below is the code I  modified, any ideas why it is not working?

    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim xyz() As String = New String() {"Manufacturer", "NSN"}
        dtTable = Session("dtTable")
        CurrentIndex = Session("currentIndex")
        Dim xda As New Xml.XmlDocument
        Dim xna As Xml.XmlNode
        Dim searchIdManufacturer As String = Nothing

        'Retreive Data from control
        For i = 0 To 1
            Dim curXYZ = xyz(i)
            Dim MyFix As XDocument = XDocument.Load(Server.MapPath("~/App_Data/" & curXYZ & ".xml"))
            Dim xid As String
            xid = curXYZ & "_ID"
            Dim LargestFixID = (From el In MyFix.Descendants(curXYZ & "Table") Select CInt(el.<xid>.Value)).ToList
            LargestFixID.Sort()
            Dim xtable As String
            xtable = curXYZ & "Table"
            On Error Resume Next
            Dim ctrl As Control = MultiView1.FindControl("C1" & curXYZ)
            Dim Txt As String = Nothing
            If TypeOf (ctrl) Is C1ComboBox Then
                Txt = CType(ctrl, C1ComboBox).Text
            ElseIf TypeOf (ctrl) Is TextBox Then
                Txt = CType(ctrl, TextBox).Text
            End If

            Dim CheckForItemFix = (From el In MyFix.Descendants(xtable).Elements(curXYZ) Select el.Value).ToList


            'Check if value is already in xml files, if not add it to the xml files
            'If manufacturer already exist in XML file (Working OK!!!)
            If Not CheckForItemFix.Contains(Txt) Then
                Dim xNew As XElement = New XElement(xtable)
                xNew.Add(New XElement(xid, (LargestFixID.Count() + 1)))
                xNew.Add(New XElement(curXYZ, Txt))
                MyFix.Root.Add(xNew)
                MyFix.Save(Server.MapPath("~/App_Data/" & curXYZ & ".xml"))

                'Prepare variable to add to Link.xml file

                Select curXYZ

                    Case "Manufacturer"
                        searchIdManufacturer = LargestFixID.Count() + 1
                End Select

            End If

            'If manufacturer already exist in XML file (Working OK!!!)
            Dim searchFix As String = Txt
            Dim domFix As New XmlDocument()
            domFix.Load(Server.MapPath("~/App_Data/" & curXYZ & ".xml"))
            Dim listFix As XmlNodeList = domFix.SelectNodes("//" & xtable)
            If CheckForItemFix.Contains(Txt) Then
                For Each node As XmlNode In listFix
                    If node(curXYZ).InnerText.Equals(searchFix) Then
                        Select Case curXYZ
                            Case "Manufacturer"
                                searchIdManufacturer = node(xid).InnerText
                        End Select
                        Exit For
                    End If
                Next
            End If
            Exit For
        Next
        MsgBox(searchIdManufacturer)  ***** Value = 4 changed BMW to BMWXYZ

        Dim xNodes As XmlNodeList = xdManufacturer.SelectNodes("Root/LinkA")
        Dim xNode As XmlNode
        For Each xNode In xNodes
            If xNode.InnerText = searchIdManufacturer Then
                Dim xManuf As XmlNode = xNode.SelectSingleNode("Manufacturer_ID")
                xManuf.InnerText = searchIdManufacturer
            End If
        Next

******How do I save data to LinkA.xml, there is no connection with modified code with
code below to save LinkA.xml

        xda.Load(Server.MapPath("~/App_Data/LINKA.XML"))
        '   xna = xda.SelectSingleNode("/Root/LinkA[LinkAID='" & dtTable.Rows(CurrentIndex).Item("LinkID2") & "']")
        '  xda.SelectSingleNode("LinkA/Manufacturer_ID").InnerText = searchIdManufacturer
        xda.Save(Server.MapPath("~/App_Data/LINKA.XML")) ' (Not Working, SearchID data not saved)

Author

Commented:
Hi,

Insteasd of using the third party combobox to obtain the Text for Manufacturer and modifying the data to obatain a new ID, can you please hard code a new ID for searchIdManufacturer (i.e searchIdManufacturer = 4) and see if it saves the data to LinkA.xml using your code.
Thanks.

Author

Commented:
Hi,

The code below works in an old project,  Any ideas why my currrent code is not working? They are exactly the same. Could it be because of my previous issue, not viewing one record at a time? or perhaps somehow in this project postback is causing me to lose the value.


If RadioButton3.Checked Then
            'MsgBox("AMEND")
            dtTable = Session("dtTable")
            CurrentIndex = Session("currentIndex")
            Dim xd As New Xml.XmlDocument
            Dim xn As Xml.XmlNode

            Dim xda As New Xml.XmlDocument
            Dim xna As Xml.XmlNode

            Dim xdb As New Xml.XmlDocument
            Dim xnb As Xml.XmlNode

            ' load the data file
            xd.Load(Server.MapPath("~/App_Data/LINKSSADB.XML"))
            xn = xd.SelectSingleNode("/Root/LinkA[LinkAID='" & dtTable.Rows(CurrentIndex).Item("LinkID2") & "']")

            ' copy all of the new items
            xn.SelectSingleNode("NSN_ID").InnerText = searchIdNSN
            xn.SelectSingleNode("CountryOrigin_ID").InnerText = searchIdCountryOrigin

            xda.Load(Server.MapPath("~/App_Data/LINKA.XML"))
            xna = xda.SelectSingleNode("/Root/LinkA[LinkAID='" & dtTable.Rows(CurrentIndex).Item("LinkID2") & "']")

            xna.SelectSingleNode("Manufacturer_ID").InnerText = searchIdManufacturer

            Dim anadd As String
            anadd = MsgBox("Are you sure you want to save your changes?", MsgBoxStyle.YesNo, "Warning before Attempting to Save changes")
            If anadd = MsgBoxResult.Yes Then

                xd.Save(Server.MapPath("~/App_Data/LINKSSADB.XML"))
                xda.Save(Server.MapPath("~/App_Data/LINKA.XML"))
                MsgBox("All Changes Have Been Saved")
            End If
            If anadd = MsgBoxResult.No Then
                MsgBox("Changes were not Saved")
            End If
        End If

Author

Commented:
Hi,

To clarify. In my old project my LinkSSADB  file only had a single entry for each element, for this project the LinkSSADB file has multiple entries for NSN (i.e. 1,3,4)
Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014
Commented:
Apparently this works to find the right value?

For Each node As XmlNode In listFix
                    If node(curXYZ).InnerText.Equals(searchFix) Then
                        Select Case curXYZ
                            Case "Manufacturer"
                                searchIdManufacturer = node(xid).InnerText
                        End Select
                        Exit For
                    End If
                Next

If so, just do this:

If searchIDManufacturer = node(xid).InnerText Then
  Node(xid).InnerText=YourNewValue
End If

in the Case "Manufacturer" section

Author

Commented:
Hi,

Problem is Node(xid) is from manufacturer.xm, I need to update LinkA.xml.

Somehow I need to set that value to xna  for my LinkA.xml file.

Thanks,

V.
Scott McDaniel (EE MVE )Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
As I said, I can't run your code, so I can't test. However, if you simply iterate through the XML nodes in LinkA.xml, you should be easily able to find the correct LinkID value, and then you can update the ManufID as needed.

Your old project is different from this, it would seem. Looks like the older xml files had an ID Attribute for that node element, and you searched for that. In that case, your earlier code would work. It won't for this, since the LinkID value is being stored as Text.

In other words, the XML structure for each element is identical (of course), and the only way to differentiate between "Nodes" is to examine the InnerText of the LinkID. So you'd have to loop through the Nodes and check that for each LinkID element.

Author

Commented:
All is working ok now.

Thanks,

V.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.