Solved

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

Posted on 2015-02-09
8
72 Views
Last Modified: 2015-02-09
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
Comment
Question by:vcharles
  • 3
  • 3
  • 2
8 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40598314
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
 

Author Comment

by:vcharles
ID: 40598334
Hi,

Any ideas why my code does not work?

Thanks.
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40598362
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40598431
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
 

Author Comment

by:vcharles
ID: 40598485
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
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40598530
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
 

Author Comment

by:vcharles
ID: 40598562
Hi,
It works.
Thanks
Victor
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40598592
Not a problem Victor, glad to help.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 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