[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with reorganizing data element in xml using VB.NET

Posted on 2014-08-26
7
Medium Priority
?
180 Views
Last Modified: 2014-08-28
Hi,

I'm exporting data from a PDF file in the following format:

<?xml version="1.0" encoding="UTF-8"?>
-<fields xmlns:xfdf="http://ns.adobe.com/xfdf-transition/">
-<ListBox1 xfdf:original="ListBox1">
<value>listbox1</value>
<value>listbox2</value>
<value>listbox3</value>
<value>listbox4</value>
</ListBox1>
<ItemType>A</ItemType>
<Text111>data1</Text111>
<TxtDate>data2</TxtDate>
<TxtFT>data3</TxtFT>
<TxtFuze>data4</TxtFuze>
<TxtMT>data5</TxtMT>
<TxtManu>data6</TxtManu>
</fields>

How do I rearrange the values in listbox1 control to appear as shown below?

<?xml version="1.0" encoding="UTF-8"?>
<fields>
<Lisbox1>listbox1,listbox2,listbox3,listbox4</Listbox1>
<ItemType>A</ItemType>
<Text111>data1</Text111>
<TxtDate>data2</TxtDate>
<TxtFT>data3</TxtFT>
<TxtFuze>data4</TxtFuze>
<TxtMT>data5</TxtMT>
<TxtManu>data6</TxtManu>
</fields>

Thanks,

Victor
0
Comment
Question by:vcharles
  • 4
  • 3
7 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40287974
Hi Victor;

This code snippet should do what you need.

// Load the XML document
var xdoc = XDocument.Load("From Path & filename");

// Get a reference to the ListBox1 node
var listBox = xdoc.Root.Element("ListBox1");
// Build the new value for the ListBox1 node
var listBoxValues = listBox.Elements().Select(vals => vals.Value).Aggregate((workingValues, nextValue) => workingValues + "," + nextValue);
// Remove all ListBox1 children nodes
listBox.Elements().Remove();
// Add the new Value to the ListBox1 node
listBox.Value = listBoxValues;

// Save the document
xdoc.Save("To Path & Filename");

Open in new window

0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 40287987
Sorry Victor here it is in VB .Net

Dim xdoc = XDocument.Load("From Path & filename")

Dim listBox = xdoc.Root.Element("ListBox1")
Dim listBoxValues = listBox.Elements().Select(vals => vals.Value).Aggregate((workingValues, nextValue) => workingValues + "," + nextValue)
listBox.Elements().Remove()
listBox.Value = listBoxValues

xdoc.Save("To Path & filename")

Open in new window

0
 

Author Comment

by:vcharles
ID: 40288226
Hi Fernando,

I will try it and get back to you.

Thanks,

Victor
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 

Author Comment

by:vcharles
ID: 40289560
Hi,

I received two errors on line:

  Dim listBoxValues = listBox.Elements().Select(vals => vals.Value).Aggregate((workingValues, nextValue) => workingValues + "," + nextValue)
       

on section "nextValue)" error message: End of statement expected

on section  "Aggregate((workingValues," error message: insert missing ')'


I tried inserting additional ")", but no luck.

Thanks,

Victor
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40289581
Sorry Victor I was mixing C# code with VB code. The line should be:

Dim listBoxValues = listBox.Elements().Select(Function(vals) vals.Value) _
                                      .Aggregate(Function(workingValues, nextValue) workingValues & "," & nextValue)

Open in new window


In C# a lambda expression looks something like this

vals => vals.Value

Where in VB it looks like this

Function(vals) vals.Value

The other code I posted may have the same issue or not.
0
 

Author Closing Comment

by:vcharles
ID: 40289625
Thank You for both solutions.
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40290562
Not a problem Victor, glad I was able to help.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Integration Management Part 2
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month17 days, 18 hours left to enroll

830 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