Solved

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

Posted on 2015-02-09
8
65 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 32

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 32

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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)…
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

746 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

10 Experts available now in Live!

Get 1:1 Help Now