Victor Charles
asked on
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(searchIdManufacture r) 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("/Roo t/LinkA[Li nkAID='" & dtTable.Rows(CurrentIndex) .Item("Lin kID2") & "']")
MsgBox(searchIdManufacture r)
xna.SelectSingleNode("Manu facturer_I D").InnerT ext = 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.RegisterStar tupScript( Me.Page, Me.[GetType](), "temp", "<script type='text/javascript'>ale rt('" & Msg & "');</script>", False)
End If
LinkA.xml
<LinkA>
<LinkAID>1</LinkAID>
<Manufacturer_ID>1</Manufa cturer_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("/Roo t/LinkA[Li nkAID='" & dtTable.Rows(CurrentIndex) .Item("Lin kID2") & "']")
But still can't figure it out.
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(searchIdManufacture
Dim xda As New Xml.XmlDocument
Dim xna As Xml.XmlNode
xda.Load(Server.MapPath("~
xna = xda.SelectSingleNode("/Roo
MsgBox(searchIdManufacture
xna.SelectSingleNode("Manu
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("~
Dim Msg As String = "All Changes Have Been Saved"
ScriptManager.RegisterStar
End If
LinkA.xml
<LinkA>
<LinkAID>1</LinkAID>
<Manufacturer_ID>1</Manufa
<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("/Roo
But still can't figure it out.
For this selection You should have root tag on your xml file .
xna = xda.SelectSingleNode("/Root/LinkA[LinkAID='" & dtTable.Rows(CurrentIndex).Item("LinkID2") & "']")
<?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>
ASKER
Hi,
Sorry for the confusion . The Root tag is already included.
Victor
Sorry for the confusion . The Root tag is already included.
Victor
ASKER
Hi,
I tried xda.SelectSingleNode("Root /LinkA/Man ufacturer_ ID").Inner Text = 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
I tried xda.SelectSingleNode("Root
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
Check what you are receiving on this . it will be updated according to this value
MsgBox(dtTable.Rows(CurrentIndex).Item("LinkID2") )
ASKER
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.
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.
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.
ASKER
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.
ASKER
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
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
You'll have to get the NodeList collection, I'd think, and then loop through them to determine which Node you want to update.
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?
Can you post your XML file?
ASKER
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.
http://speedy.sh/XnARs/WebApplication13.zip
V.
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:
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
I couldn't test this, of course, so it may have some syntax errors, but hopefully this will help.
ASKER
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.
ASKER
Hi,
I'm afraid it still did not work. I am getting the right value when I test the data with MsgBox(searchIdManufacture r) , 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.MapP ath("~/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)).ToLi st
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(c urXYZ) 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(T xt) 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_Dat a/" & 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_Da ta/" & curXYZ & ".xml"))
Dim listFix As XmlNodeList = domFix.SelectNodes("//" & xtable)
If CheckForItemFix.Contains(T xt) Then
For Each node As XmlNode In listFix
If node(curXYZ).InnerText.Equ als(search Fix) Then
Select Case curXYZ
Case "Manufacturer"
searchIdManufacturer = node(xid).InnerText
End Select
Exit For
End If
Next
End If
Exit For
Next
MsgBox(searchIdManufacture r) ***** Value = 4 changed BMW to BMWXYZ
Dim xNodes As XmlNodeList = xdManufacturer.SelectNodes ("Root/Lin kA")
Dim xNode As XmlNode
For Each xNode In xNodes
If xNode.InnerText = searchIdManufacturer Then
Dim xManuf As XmlNode = xNode.SelectSingleNode("Ma nufacturer _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("/Roo t/LinkA[Li nkAID='" & dtTable.Rows(CurrentIndex) .Item("Lin kID2") & "']")
' xda.SelectSingleNode("Link A/Manufact urer_ID"). InnerText = searchIdManufacturer
xda.Save(Server.MapPath("~ /App_Data/ LINKA.XML" )) ' (Not Working, SearchID data not saved)
I'm afraid it still did not work. I am getting the right value when I test the data with MsgBox(searchIdManufacture
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.MapP
Dim xid As String
xid = curXYZ & "_ID"
Dim LargestFixID = (From el In MyFix.Descendants(curXYZ & "Table") Select CInt(el.<xid>.Value)).ToLi
LargestFixID.Sort()
Dim xtable As String
xtable = curXYZ & "Table"
On Error Resume Next
Dim ctrl As Control = MultiView1.FindControl("C1
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).
'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(T
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(
'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
Dim listFix As XmlNodeList = domFix.SelectNodes("//" & xtable)
If CheckForItemFix.Contains(T
For Each node As XmlNode In listFix
If node(curXYZ).InnerText.Equ
Select Case curXYZ
Case "Manufacturer"
searchIdManufacturer = node(xid).InnerText
End Select
Exit For
End If
Next
End If
Exit For
Next
MsgBox(searchIdManufacture
Dim xNodes As XmlNodeList = xdManufacturer.SelectNodes
Dim xNode As XmlNode
For Each xNode In xNodes
If xNode.InnerText = searchIdManufacturer Then
Dim xManuf As XmlNode = xNode.SelectSingleNode("Ma
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("~
' xna = xda.SelectSingleNode("/Roo
' xda.SelectSingleNode("Link
xda.Save(Server.MapPath("~
ASKER
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.
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.
ASKER
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/L INKSSADB.X ML"))
xn = xd.SelectSingleNode("/Root /LinkA[Lin kAID='" & dtTable.Rows(CurrentIndex) .Item("Lin kID2") & "']")
' copy all of the new items
xn.SelectSingleNode("NSN_I D").InnerT ext = searchIdNSN
xn.SelectSingleNode("Count ryOrigin_I D").InnerT ext = searchIdCountryOrigin
xda.Load(Server.MapPath("~ /App_Data/ LINKA.XML" ))
xna = xda.SelectSingleNode("/Roo t/LinkA[Li nkAID='" & dtTable.Rows(CurrentIndex) .Item("Lin kID2") & "']")
xna.SelectSingleNode("Manu facturer_I D").InnerT ext = 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/L INKSSADB.X ML"))
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
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("~/
xn = xd.SelectSingleNode("/Root
' copy all of the new items
xn.SelectSingleNode("NSN_I
xn.SelectSingleNode("Count
xda.Load(Server.MapPath("~
xna = xda.SelectSingleNode("/Roo
xna.SelectSingleNode("Manu
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("~/
xda.Save(Server.MapPath("~
MsgBox("All Changes Have Been Saved")
End If
If anadd = MsgBoxResult.No Then
MsgBox("Changes were not Saved")
End If
End If
ASKER
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)
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)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
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.
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.
ASKER
All is working ok now.
Thanks,
V.
Thanks,
V.
xna = xda.SelectSingleNode("Link
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/M