Solved

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

Posted on 2015-02-09
8
67 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
 
LVL 62

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
DevOps Toolchain Recommendations

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

 

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 62

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 62

Expert Comment

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now