[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 78
  • Last Modified:

Help with entering data in data element in sequential order using vb.net

Hi,

How do you modify the code below to add numeric values in <ID> data element, code below does not include any values in the data element.
 
        Dim xdoc As XDocument = XDocument.Load(Application.StartupPath + "\AP40B.xml")
        Dim ii As Integer = 0        
For Each item As XElement In xdoc.Elements("AP40")
            item.Element("ID").Value = ii
            ii = ii + 1
        Next

        ' Save the modified xdoc to the file system                                                
        xdoc.Save(Application.StartupPath + "\AP40B.xml")

Open in new window

0
vcharles
Asked:
vcharles
  • 3
  • 3
  • 2
1 Solution
 
ste5anSenior DeveloperCommented:
There are some glitches:

Module Module1

    Sub Main()

        Const SIMPLE_XML As String = "<root><element>123</element></root>"
        Const ELEMENT_NAME As String = "element"

        Dim Document As XDocument = XDocument.Parse(SIMPLE_XML)

        For Each Element As XElement In Document.Root.Elements(ELEMENT_NAME)
            Console.WriteLine("Element '{0}' found.", ELEMENT_NAME)
            Element.Value = "666"
        Next

        Console.WriteLine(Document.ToString())
        Console.WriteLine("Done.")
        Console.ReadLine()

    End Sub

End Module

Open in new window

0
 
vcharlesAuthor Commented:
Hi,

Any ideas why my code does not work?

Thanks.
0
 
ste5anSenior DeveloperCommented:
Sure. That's why I've posted a working example. Here's an example closer to your original source:

Module Module1

    Sub Main()

        Const SIMPLE_XML As String = "<root><element>123</element></root>"
        Const ELEMENT_NAME As String = "root"

        Dim Document As XDocument = XDocument.Parse(SIMPLE_XML)

        For Each Element As XElement In Document.Elements(ELEMENT_NAME)
            Console.WriteLine("Element '{0}' found.", ELEMENT_NAME)
            Element.Value = "666"
        Next

        Console.WriteLine(Document.ToString())
        Console.WriteLine("Done.")
        Console.ReadLine()

    End Sub

End Module

Open in new window

0
Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

 
Fernando SotoCommented:
Hi Victor;

I modified your document to give values to ID nodes.

'' Open and load the XML document
Dim xdoc As XDocument = XDocument.Load(Application.StartupPath + "\AP40B.xml")
Dim ii As Integer = 0        
'' Iterate through all the ID nodes and assign a ID value
For Each item As XElement In xdoc.Root.Elements("ID")
    item.Value = ii
    ii = ii + 1
Next



'' Save the modified xdoc to the file system                                                
xdoc.Save(Application.StartupPath + "\AP40B.xml")

Open in new window

0
 
vcharlesAuthor Commented:
Hi,

I am still not getting  in values in ID, could it be cause it is the last data element  in the table? Table name is AP40.
0
 
Fernando SotoCommented:
Hi Victor;

Not knowing the document layout make this hard. Try changing this line in the code:

For Each item As XElement In xdoc.Root.Elements("ID")

to this.

For Each item As XElement In xdoc.Root. Descendants("ID")

see if that works for you.
0
 
vcharlesAuthor Commented:
Hi,
It works.
Thanks
Victor
0
 
Fernando SotoCommented:
Not a problem Victor, glad to help.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now