Solved

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

Posted on 2015-02-09
8
74 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 34

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 34

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 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