?
Solved

Help with saving data to xml file

Posted on 2012-08-14
22
Medium Priority
?
871 Views
Last Modified: 2012-08-21
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.
0
Comment
Question by:vcharles
  • 13
  • 7
  • 2
22 Comments
 
LVL 85
ID: 38295821
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
0
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38295923
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

0
 

Author Comment

by:vcharles
ID: 38296421
Hi,

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

Victor
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:vcharles
ID: 38296521
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
0
 
LVL 18

Expert Comment

by:Rajar Ahmed
ID: 38296580
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

0
 

Author Comment

by:vcharles
ID: 38296759
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.
0
 
LVL 85
ID: 38296851
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.
0
 

Author Comment

by:vcharles
ID: 38296890
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.
0
 

Author Comment

by:vcharles
ID: 38297166
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
0
 
LVL 85
ID: 38297305
You'll have to get the NodeList collection, I'd think, and then loop through them to determine which Node you want to update.
0
 
LVL 85
ID: 38297307
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?
0
 

Author Comment

by:vcharles
ID: 38297337
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.
0
 
LVL 85
ID: 38299769
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.
0
 

Author Comment

by:vcharles
ID: 38299941
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.
0
 

Author Comment

by:vcharles
ID: 38302631
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)
0
 

Author Comment

by:vcharles
ID: 38302652
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.
0
 

Author Comment

by:vcharles
ID: 38302929
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
0
 

Author Comment

by:vcharles
ID: 38302937
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)
0
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 total points
ID: 38303919
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
0
 

Author Comment

by:vcharles
ID: 38304121
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.
0
 
LVL 85
ID: 38307582
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.
0
 

Author Comment

by:vcharles
ID: 38315385
All is working ok now.

Thanks,

V.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
How can you see what you are working on when you want to see it while you to save a copy? Add a "Save As" icon to the Quick Access Toolbar, or QAT. That way, when you save a copy of a query, form, report, or other object you are modifying, you…
Suggested Courses
Course of the Month9 days, 12 hours left to enroll

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question