Help with saving data in multiple elements of xml file

Hello,

below is part of my code that I'm using to save data to one xml elementy using (xNew.Add(New XElement(curXYZ, Txt))), Now I have Text1, Text2...Textn and multple elents in my xml file. How do I modify the code to enter save data in multiple elements for the same record?

  'Check if value is already in xml files, if not add it to the xml files
            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"))


Whai is the proper syntax in clode below with ???

                Dim xNew As XElement = New XElement(xtable)

                xNew.Add(New XElement(xid, (LargestFixID.Count() + 1)))

                xNew.Add(New XElement(receiver, Txt1; Donor Txt2, Item, Text3......))'????

                MyFix.Root.Add(xNew)

                MyFix.Save(Server.MapPath("~/App_Data/" & curXYZ & ".xml"))

vcharlesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

grayeCommented:
Normally, you would not write "multiple elements" in a single "record".  Instead, you'd create a loop that writes one item to one record.

You you have a schema file that we can look at (or perhaps just a short sample of the XML file with some data in it?)   With this information, we can easily help you write the code to do what you need.
0
vcharlesAuthor Commented:
Hi,

Below is an example of the format of the XML file:

<itemTable>
<linkId>1</linkID>
<Receiver>Bel</Receiver>
<Donor>USA</>
<Model>M987</>
</ItemTable>

The values are coming from Text strings. LinkID is incremented for each
New record. My other issue is, when the user updates the data using the form's control,
When Viewing a particular record, how do I save
It to the appropriate record

Thanks,
0
grayeCommented:
If I'm following you correctly, you need to adjust the schema of the XML file to allow for both the original method (with just one next node) and the new method with a series of elements.   If so, then I'd recommend something like this:

<itemTable>
<linkId>1</linkID>
<SomeText></SomeText>
<Receiver>Bel</Receiver>
<Donor>USA</>
<Model>M987</>
</ItemTable>

That way you'd populate the <SomeText> element using the original method (and leave the others empty or missing), and then for the new method you'd populate the <Reciever>, <Donor>, and <Model> (and leave the SomeText empty or missing).

                Dim xNew As XElement = New XElement(xtable)
                xNew.Add(New XElement(xid, (LargestFixID.Count() + 1)))
                xNew.Add(New XElement("Receiver", Txt1))
                xNew.Add(New XElement("Donor", Txt2))
                xNew.Add(New XElement("Model", Text3))
                MyFix.Root.Add(xNew)
                MyFix.Save(Server.MapPath("~/App_Data/" & curXYZ & ".xml"))
0
Angular Fundamentals

Learn the fundamentals of Angular 2, a JavaScript framework for developing dynamic single page applications.

vcharlesAuthor Commented:
Hi,

I don't quiet follow the part related to some text. I understand using the code you sent me will add a new ItemTable along with the 4 elements, but when for example I am viewing a record with LinkID = 4, if a cjhange is made to Donor which means a new value for Text2, how would I replace the Donor element with the new Text2 value? I don't see how including the <sometext> element would accomplish replacing the old Donor value with the new Text2.

 
0
grayeCommented:
OK, ignore the <SomeText> and look at the code sample that populates the Receiver, Donor, and Model elements
0
vcharlesAuthor Commented:
Ok, I can replace all the old values with same code, but how do
Do that to the current record I'm modifying?
0
grayeCommented:
Here is some example code from an old project of mine.   Take a look at the "flow" of the example....

1) First you must find the correct node to edit (assuming that you've got lots of nodes)
2) Then overwrite the existing values
3) And lastly, save the changes

In this case I have an ID attribute (not an ID element, as in your case).   BTW:  A a "real" ID attribute can't start with a number.
For completeness, here is a sample of the XML file:

<Computers>
  <Computer id="PC_1" recordDate="2011-04-12T17:15:14">
    <Name>GRAYTABLET</Name>
    <Comment>Emmet's Tablet PC</Comment>
    <Model>Dell Latitude XT2</Model>
    <BIOS>A10</BIOS>
    <SerialNum>xxxxxxx</SerialNum>
    <User isAdmin="false">Emmet.Gray</User>
    <IP isDHCP="true">010.001.050.001</IP>
    <OS>
      <OS_Name>Windows 7 Ultimate</OS_Name>
      <ServicePack>1</ServicePack>
      <Bits>64</Bits>
    </OS>
    <CPU>
      <CPU_Type>Intel Core2 Duo U9400 @ 1.40Ghz</CPU_Type>
      <Speed>1400</Speed>
      <Cores>2</Cores>
    </CPU>
    <Memory>5120</Memory>
    <Disks>
      <Disk>
        <DriveLetter>C:</DriveLetter>
        <DiskType>Local Fixed Disk</DiskType>
        <Capacity>244093</Capacity>
        <FreeSpace>140588</FreeSpace>
      </Disk>
      <Disk>
        <DriveLetter>D:</DriveLetter>
        <DiskType>CD-ROM Disc</DiskType>
      </Disk>
    </Disks>
  </Computer>
</Computers>
''' <summary>
    ''' Update a computer entry in the XML file
    ''' </summary>
    ''' <param name="comp">A computer instance with new values</param>
    ''' <remarks></remarks>
    Public Sub UpdateComputer(ByVal comp As Computer)
        Dim xd As New Xml.XmlDocument
        Dim xn, xtemp, xdisks, xdisk As Xml.XmlNode

        ' load the data file
        xd.Load(_XML_Path)

        ' do an XPath query to find the correct node
        xn = xd.SelectSingleNode("Computers/Computer[@id='" & ID_PREFIX & comp.id & "']")

        ' did we find the correct node?
        If xn Is Nothing Then
            Throw New ApplicationException("ID " & comp.id & " not found")
        End If
        comp.recordDate = Now

        ' copy all of the new items 
        xn.Attributes("recordDate").InnerText = comp.recordDate.ToString("s")
        xn.SelectSingleNode("Name").InnerText = comp.Name
        xn.SelectSingleNode("Comment").InnerText = comp.Comment
        xn.SelectSingleNode("Model").InnerText = comp.Model
        xn.SelectSingleNode("BIOS").InnerText = comp.BIOS
        xn.SelectSingleNode("SerialNum").InnerText = comp.SerialNum
        xn.SelectSingleNode("User").InnerText = comp.User
        xn.SelectSingleNode("User").Attributes("isAdmin").InnerText = comp.isAdmin.ToString.ToLower
        xn.SelectSingleNode("IP").InnerText = comp.IP
        xn.SelectSingleNode("IP").Attributes("isDHCP").InnerText = comp.isDHCP.ToString.ToLower
        xn.SelectSingleNode("OS/OS_Name").InnerText = comp.OS_Name
        xtemp = xn.SelectSingleNode("OS/ServicePack")
        ' if there wasn't an existing node, but now we need one
        If xtemp Is Nothing And comp.ServicePack <> "" Then
            xtemp = xn.SelectSingleNode("OS").AppendChild(xd.CreateElement("ServicePack"))
        End If
        ' if we currently have a node, but no longer need it
        If xtemp IsNot Nothing And comp.ServicePack = "" Then
            xtemp.ParentNode.RemoveAll()
        End If
        If comp.ServicePack <> "" Then
            xtemp.InnerText = comp.ServicePack
        End If
        xn.SelectSingleNode("OS/Bits").InnerText = comp.Bits.ToString
        xn.SelectSingleNode("CPU/CPU_Type").InnerText = comp.CPU_Type
        xn.SelectSingleNode("CPU/Speed").InnerText = comp.CPU_Speed.ToString
        xn.SelectSingleNode("CPU/Cores").InnerText = comp.CPU_Cores.ToString
        xn.SelectSingleNode("Memory").InnerText = comp.Memory.ToString
        ' we can't just update the disks directly, since the number of items might have 
        ' changed.  So, we just whack the existing list and start over
        xdisks = xn.SelectSingleNode("Disks")
        xdisks.RemoveAll()
        For Each d As Disk In comp.Disks
            xdisk = xdisks.AppendChild(xd.CreateElement("Disk"))
            xdisk.AppendChild(xd.CreateElement("DriveLetter")).InnerText = d.DriveLetter
            xdisk.AppendChild(xd.CreateElement("DiskType")).InnerText = d.DiskType
            ' capacity and freespace are optional
            If d.Capacity > 0 Then
                xdisk.AppendChild(xd.CreateElement("Capacity")).InnerText = d.Capacity.ToString
                xdisk.AppendChild(xd.CreateElement("FreeSpace")).InnerText = d.FreeSpace.ToString
            End If
        Next

        xd.Save(_XML_Path)
    End Sub

Open in new window

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:
Thanks, will try to follow your example and get back to you.
0
vcharlesAuthor Commented:
Hello,

I used the code in Part A to search for records, the data is filtered in Step 6 and all worked well. I am now using the same code in Part A with your code in Part B to update changes selected in the Receiver and Donor control and save new selections (Receiver_ID, Donor_ID) to LinkSSADB.xml. How do I modify the code in Step 6 in order to obtain the correct values for DonorID2 and receiverID2? I need help with changing the filter in Part 6 because I don’t need to repeat the same search command when trying to save my changes. Currently values for ReceiverID2 and DonorID2 gives me the wrong values because of the filter command in Part 6.  

Thanks,

Victor

Partr A:

‘Step 1
Dim xdDonor As New Xml.XmlDocument
Dim xdLink As New Xml.XmlDocument
Dim xdReceiver As New Xml.XmlDocument
Dim xdDonor As New Xml.XmlDocument

‘Step 2
xdLink.Load(Server.MapPath("~/App_Data/LinkSSADB.xml"))
xdDonor.Load(Server.MapPath("~/App_Data/CountryOrigin.xml"))
xdReceiver.Load(Server.MapPath("~/App_Data/CountryUser.xml"))

‘ Step 3
Dim DonorID As String
Dim ReceiverID As String
Dim DonorID2 As String
Dim ReceiverID2 As String
Dim Link_ID As String
Dim LinkID2 As String

‘Step 4
dtReceiver = New DataTable
dtReceiver.Columns.Add("CountryUser_ID", GetType(String))
dtReceiver.Columns.Add("CountryUser", GetType(String))

dtDonor = New DataTable
dtDonor.Columns.Add("CountryOrigin_ID", GetType(String))
dtDonor.Columns.Add("CountryOrigin", GetType(String))

dtLinkID = New DataTable
dtLinkID.Columns.Add("LinkID", GetType(String))

‘Step 5
Session("CurrentIndex") = 0
Session("dtDonor") = dtDonor
Session("dtReceiver") = dtReceiver
Session("dtLinkID") = dtLinkID

‘Step 6
For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/LinkA[Item_ID='" & C1Items.SelectedItem.Value & "']")

Link_ID = xnLink.SelectSingleNode("LinkAID").InnerText
ReceiverID = xnLink.SelectSingleNode("CountryUser_ID").InnerText
DonorID = xnLink.SelectSingleNode("CountryOrigin_ID").InnerText

‘Step 7
For Each xnDonor As Xml.XmlNode In xdDonor.SelectNodes("/Root/CountryOriginTable[CountryOrigin_ID='" & DonorID & "']")
DonorID2 = xnDonor.SelectSingleNode("CountryOrigin_ID").InnerText
Name = xnDonor.SelectSingleNode("CountryOrigin").InnerText
dtDonor.Rows.Add({DonorID2, Name})
Next
For Each xnReceiver As Xml.XmlNode In xdReceiver.SelectNodes("/Root/CountryUserTable[CountryUser_ID='" & ReceiverID & "']")
ReceiverID2 = xnReceiver.SelectSingleNode("CountryUser_ID").InnerText
Name = xnReceiver.SelectSingleNode("CountryUser").InnerText
dtReceiver.Rows.Add({ReceiverID2, Name})
Next

LinkID2 = xnLink.SelectSingleNode("LinkAID").InnerText
dtLinkID.Rows.Add({LinkID2})

Part B:

‘Step 8 (Using code from Expert Exchange)

Dim xd As New Xml.XmlDocument
Dim xn As Xml.XmlNode
' load the data file
xd.Load(Server.MapPath("~/App_Data/LINKSSADB.XML"))
' do an XPath query to find the correct node
xn = xd.SelectSingleNode("/Root/LinkA[LinkAID='" & LinkID2 & "']")

' did we find the correct node?
 If xn Is Nothing Then
 Throw New ApplicationException("not found")
 Exit Sub
 End If
       
' copy all of the new items
xn.SelectSingleNode("CountryOrigin_ID").InnerText = DonorID2
xn.SelectSingleNode("CountryUser_ID").InnerText = ReceiverID2
xd.Save(Server.MapPath("~/App_Data/LINKSSADB.XML"))
0
vcharlesAuthor Commented:
Hello again,

I tried to set the filter in step 6 to retreive the exact node I need to modify using LinkID2:
For Each xnLink As Xml.XmlNode In xdLink.SelectNodes("/Root/LinkA[Item_ID='" & LinkID2 & "']")
But for some reason the value of LinkID2 which was created from the Search button is now blank, eventhough it was declared as public.

Basically users, will use the search button before they modify the data, the value of LinkID2 for the first reord found should also be available for the Amend button, when multiple records are found and users click on the Next button, the latest LinkID2 should also be available for the Amend button.

I hope this helps clarify my issue.

Thanks,

Victor

Victor
0
vcharlesAuthor Commented:
I just realised I don't need a filter in Part A, I just need help with poiting to the current node of LinkSSADB.xml and my changes with be replaced with the code in Part B. How do I point to the latest node of LinkSSADD,xml? Although I don't need a filter< I still need the code the retreive ReceiverID and DonorID for case or case 2, how do I modify the codee in Part A without a filter?

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
ASP.NET

From novice to tech pro — start learning today.