Help with Xml

T

Trevor

Hello
I need some help with code.
I have a small project where I am currently querying data and I get
back xml because the technology works on xml.
I save the xml file to disk then I load it into the xmldocument object
and parse the xml finding the values I require which I put into a
listview.
I have been told not to use the list view but to rather use a
datatable then bind the data to a datagrid or to serialize the data
then to bind to a datagrid. I am currently using the following code to
get my data into a listview:
Dim xnlHeader As XmlNodeList
xnlHeader = custqueryxml.SelectNodes("//HeaderDetails/Columns/
Column")

' Iterate through a collection
For Each xnNodeColumn As XmlNode In xnlHeader
ListView1.Columns.Add(xnNodeColumn.InnerText, 150)
Next

Dim xnlRow As XmlNodeList
xnlRow = custqueryxml.SelectNodes("//ArCustomerBal/Row")

For Each xnNodeColumn As XmlNode In xnlRow
Dim lvItem As New
ListViewItem(xnNodeColumn.SelectSingleNode("Customer").InnerText)

lvItem.SubItems.Add(xnNodeColumn.SelectSingleNode("CurrentBalance1").InnerText)
ListView1.Items.Add(lvItem)

What would I need to do change this accordingly to use either the
datatable or to serialize and what is the better option?

The second of my questions is:
I need to know take the values "customer" & "CurrentBalance1" and then
build xml for every instance so obviously I need to use the for each
command but I am not to sure of how to code it below is and example of
what I need to do:

Dim xml_elem As String
Dim item As ListViewItem
' Iterate through a collection
For Each item In ListView1.Items
Dim Document As New System.Text.StringBuilder
With Document
.Append("<Item>")
.Append("<Payment>")
.Append("<Customer>" & ???? & "</Customer>")
.Append("<PaymentValue>" & ???? & "</PaymentValue>")
.Append("</Payment>")
.Append("</Item>")
End With
xml_elem = "<PostArPayment>" & Document.ToString & "</
PostArPayment>"

Now the question marks are where the customer value and the
currentbalance1 should go but I am not sure how to get it there and
also if I use the code above the object xml_elem keeps getting
overwritten so it does not build up the xml.

Please help me if you can
 
R

rowe_newsgroups

I have been told not to use the list view but to rather use a
datatable then bind the data to a datagrid or to serialize the data
then to bind to a datagrid.

Who told you to "not to use the list view but to rather use a
datatable"?

Is this a homework assignment?


Thanks,

Seth Rowe
 
P

Phill W.

Trevor said:
I have a small project where I am currently querying data and I get
back xml because the technology works on xml.
I save the xml file to disk then I load it into the xmldocument object

Why?
If you have the raw Xml in, say, a String then use the LoadXml() method
(on the XmlDocument) to bypass the need for a disk file.
and parse the xml finding the values I require which I put into a
listview.
I have been told not to use the list view but to rather use a
datatable then bind the data to a datagrid or to serialize the data
then to bind to a datagrid.

Why? And by whom?
If a ListView's what you want, then /use/ one.
I need to know take the values "customer" & "CurrentBalance1" and then
build xml for every instance

This would be your "Save" processing, right?
so obviously I need to use the for each command but I am not to sure
of how to code it below is and example of what I need to do:

<snip>

Building Xml up using strings is a sure-fire way of giving yourself
headaches and really /weird/ problems later on. Use the XmlDocument again:

Dim newDoc as New XmlDocument
newDoc.LoadXml( "<file />" )

For Each item As ListViewItem _
In ListView1.Items
Dim arpayEle As XmlElement _
= newDoc.CreateElement("PostArPayment")
newDoc.DocumentElement.AppendChild(arpayEle)

Dim itemEle As XmlElement _
= newDoc.CreateElement("Item")
arpayEle.AppendChild(itemEle)

Dim payEle As XmlElement _
= newDoc.CreateElement("Payment")
itemEle.AppendChild(payEle)

Dim customerEle as XmlElement _
= newDoc.CreateElement("Customer")
customerEle.InnerText = item.Text ' ???
payEle.AppendChild(customerEle)

Dim valueEle as XmlElement _
= newDoc.CreateElement("PaymentValue")
valueEle.InnerText = item.SubItems(1) ' ???
payEle.AppendChild(payEle)

Next
newDoc.Save( .. )

I /think/ those are the right item properties, but this /is/ air-code so
give it a try.

HTH,
Phill W.
 
M

Martin Honnen

Trevor said:
Dim Document As New System.Text.StringBuilder
With Document
.Append("<Item>")
.Append("<Payment>")
.Append("<Customer>" & ???? & "</Customer>")
.Append("<PaymentValue>" & ???? & "</PaymentValue>")
.Append("</Payment>")
.Append("</Item>")
End With
xml_elem = "<PostArPayment>" & Document.ToString & "</
PostArPayment>"

Don't use a StringBuilder and string concatenation to build XML, use
XmlWriter to build XML as that ensures you get well-formed XML:
<URL:http://msdn2.microsoft.com/en-us/library/4d1k42hb(VS.80).aspx>
 
T

Trevor

Hello Seth

I am still learning how to write code and this is more like a pet
project to be used as a proof of concept and if the concept is
approved then it will become and actually application that needs to be
written.
The listview idea was what I wanted to use but or customer base
generally has at least 2000 customers and I was told by another
developer that the listview would take too long to load that many
customers.

Trevor
 
T

Trevor

Hi Phil thanks for the help I will try your code.
The reason for not using the listview was because I am going to need
to load at least 2000 customers into it and another developer told me
it would take too long and that I should not use it.

Trevor
 
R

rowe_newsgroups

Hello Seth

I am still learning how to write code and this is more like a pet
project to be used as a proof of concept and if the concept is
approved then it will become and actually application that needs to be
written.
The listview idea was what I wanted to use but or customer base
generally has at least 2000 customers and I was told by another
developer that the listview would take too long to load that many
customers.

Trevor

Just checking - whenever I see a statement like "I was told not to do
______ but to do _______" it usually is from a student trying to get
this newsgroup to do his homework for him/her.

Now that thats over - did you still need help or did you figure things
out?

Thanks,

Seth Rowe
 
T

Trevor

Hi Seth

Any help will be appreciated.....have made sense of the code posted
from the other guys but just one part of that code does not seem to
work and that is when I am trying to pick up the value from the
subitem.

Trevor
 
R

rowe_newsgroups

Hi Seth

Any help will be appreciated.....have made sense of the code posted
from the other guys but just one part of that code does not seem to
work and that is when I am trying to pick up the value from the
subitem.

Trevor

Could you post a small, but complete, snippet of the Xml you're
working on? Without having the something to work with it's a bit
difficult to show you what to do.

Thanks,

Seth Rowe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top