Help with reorganizing data element in xml using VB.NET

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
vcharlesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
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
Fernando SotoRetiredCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vcharlesAuthor Commented:
Hi Fernando,

I will try it and get back to you.

Thanks,

Victor
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

vcharlesAuthor Commented:
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
Fernando SotoRetiredCommented:
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
vcharlesAuthor Commented:
Thank You for both solutions.
0
Fernando SotoRetiredCommented:
Not a problem Victor, glad I was able to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.