Hello I have an Xelement which shows this xml code:
<response xmlns="" state="ERROR" errormsg="Duplicates found" requestid="">
<address xmlns="" kgs="" corporateclientpc="" postofficeboxpc="" postofficeboxcity="" postofficebox="" nielsen="" building="" streetnumberaddition="" streetnumber="" fullstreet="" postalcodeaddition="" postalcode="71154" cityaddition="" city="Nufringen" streetaddition="" modifyuser="" createuser="" modifydate="" createdate="" agencyflag="" abo="" ustid="" webaddress="" email="" legalform="" fax="" phone="" name3="" name2="" name1="" systemid="" vogelid="" name4="">
<state key="" />
<country value="Deutschland" />
<addresssource key="" />
<sizeofenterprise key="" />
<addresslock key="" />
<branch key="" />
<language key="" />
</address>
<possibleduplicates>
<address name1="" name2="" name3="" name4="" fullname="Elsässer Industrietechnik GmbH" phone="" phone_country="" phone_areacode="" phone_callnumber="" fax="" fax_country="" fax_areacode="" fax_callnumber="" legalform="" email="info@filter-technik.de" webaddress="" ustid="" abo="false" openentries_avs3="" openentries_sap="false" agencyflag="false" createdate="28.06.2001 10:38:00" modifydate="" createuser="" modifyuser="DSB-ESCH_AL" street="Carl-Benz-Str." streetnumber="24" streetnumberaddition="" fullstreet="Carl-Benz-Str. 24" streetaddition="" city="Nufringen" cityaddition="" postalcode="71154" postalcodeaddition="" building="" postofficebox="" postofficeboxcity="" postofficeboxpc="" corporateclientpc="" kgs="000000008115037" vogelid="0002497680" type="Company" nielsen="Bezirk 3B" similarity="92.16">
<country key="DE" value="Deutschland" />
<state key="08" value="Baden-Württemberg" />
<addresssource key="06" value="Import" note="" counter="1" />
<sizeofenterprise key="1" value="unbekannt" counter="1" />
<branch key="194" value="Armaturen" counter="1" />
<language key="D" value="Deutsch" counter="1" />
<association fromvogelid="" tovogelid="" forwardrelation="Tochter" backrelation="Mutter" forwardnotes="" backnotes="Werksvertretung" />
<association fromvogelid="0002544053" tovogelid="0002497680" forwardrelation="Agentur" backrelation="" forwardnotes="" backnotes="Vorher:Kunde" />
</address>
</possibleduplicates>
<streetaddress state="AddressIsCorrect" />
</response>
As you the the Xelement text has at the <address>
tag the first row I want to display in the gridview. At <possibleduplicates>
tag are 1 to n duplicates which I want to add in the gridview too. The purpose is that the user can easily compare between duplicates and original datarow.
I found this code in the www, and tried to play a bit. I guess this is my basis. My first problem is that the not every row of the grid has the same columns. Then would it be better to create the columns in a specific chema and insert the xml attributes into them?
The code below crates a a state column in the gridview, but it is empty..
Dim query = From responses In xanswer...<state> _
Select New With {.state = responses.@state}
skmTabelle.DataGridView1.DataSource = query.ToList
Hop you Guys can help me out.
Cheers b101
EDIT: 1 (dear reader read the edits after reading comments)
Hello Markus, with this line: Dim address = xanswer.Descendants("address")
I have only the rows in xml format. now I need to get the rows into the gridview. What do I need to do to make this happen? My whish would be to loop through the xml and every attribute that is not "" , into gridview. but I dont know how, and I dont know of this is possible. The easy way would be to add the columns and headers at the loading event and then put the rows via gridview1.rows.add with attributes xanswer..@name1, ..... last attribute into the grid row for row.
But I dont want that. Well, If I dont get the dynamic way working I need to do that, but first I want to try to get it the dynamic way.
cheers
PS: I´ve you see any logical failure in my statement pls tell me, I am vb.net newbie
Edit2:
Dim i = skmTabelle.Skm_dublettenTableAdapter.ScalarQuery(15383)
Dim xanswer As XElement = XElement.Parse(i)
Dim address = xanswer.Descendants("address")
For i1 = 0 To address.Count - 1
Console.Write(address.ElementAt(i1).ToString())
Next
Last Edit
This is the code of the 3 methods.
Public Sub auswerten(pkey As Integer)
Dim i = skmTabelle.Skm_dublettenTableAdapter.ScalarQuery(15383)
Dim xanswer As XElement = XElement.Parse(i)
Dim address = xanswer.Descendants("address")
skmTabelle.DataGridView1.AutoGenerateColumns = True
Dim query = From x In xanswer.Descendants("address") Select GetMyDataItemFromXElement(x)
skmTabelle.DataGridView1.DataSource = query
End Sub
Public Function GetMyDataItemFromXElement(e As XElement) As MyDataItem
Dim item As New MyDataItem()
With item
.Name1 = GetValueFromAttribute(e, "name1")
.Name2 = GetValueFromAttribute(e, "name2")
.Name3 = GetValueFromAttribute(e, "name3")
.phone = GetValueFromAttribute(e, "phone")
.fax = GetValueFromAttribute(e, "fax")
.legalform = GetValueFromAttribute(e, "legalform")
.email = GetValueFromAttribute(e, "email")
.webaddress = GetValueFromAttribute(e, "webaddress")
.ustid = GetValueFromAttribute(e, "ustid")
.abo = GetValueFromAttribute(e, "abo")
.agencyflag = GetValueFromAttribute(e, "agencyflag")
.street = GetValueFromAttribute(e, "street")
.streetaddition = GetValueFromAttribute(e, "streetaddition")
.city = GetValueFromAttribute(e, "city")
.cityaddition = GetValueFromAttribute(e, "cityaddition")
.postalcode = GetValueFromAttribute(e, "postalcode")
.postalcodeaddition = GetValueFromAttribute(e, "postalcodeaddition")
.fullstreet = GetValueFromAttribute(e, "fullstreet")
.streetnumber = GetValueFromAttribute(e, "streetnumber")
.steetnumberaddition = GetValueFromAttribute(e, "streetnumberaddition")
.building = GetValueFromAttribute(e, "building")
.nielsen = GetValueFromAttribute(e, "nielsen")
.postofficebox = GetValueFromAttribute(e, "postofficebox")
.postofficeboxcity = GetValueFromAttribute(e, "postofficeboxcity")
.postofficeboxpc = GetValueFromAttribute(e, "postofficeboxpc")
.corporateclientpc = GetValueFromAttribute(e, "corporateclientpc")
.kgs = GetValueFromAttribute(e, "kgs")
.systemid = GetValueFromAttribute(e, "systemid")
.vogelid = GetValueFromAttribute(e, "vogelid")
End With
Return item
End Function
Private Function GetValueFromAttribute(e As XElement, attrName As String) As String
If e Is Nothing Then Return Nothing
Dim attr = e.Attribute(attrName)
If attr Is Nothing Then Return Nothing
Return attr.value
End Function
but at dim query It doesnt goes into the method it just jumps over it and goes directly into skmTabelle.DataGridView1.DataSource = query and ends the sub. I believe it has something to do with "x" at the arguement. But i dont know what that x means.
You can select all the <address>
nodes like this:
Dim address = xanswer.Descendants("address")
In order to show the rows in the GridView, you need a common structure as all the rows in the GridView have the same columns. The best way is to create a class that contains all the relevant properties:
Public Class MyDataItem
Public Property Name1 As String
Public Property Name2 As String
Public Property Name3 As String
' ...
Public Property StateKey As String
' ...
End Class
Then create a function that takes an XElement
as input and creates an instance of this class, e.g.:
Public Function GetMyDataItemFromXElement(e As XElement) As MyDataItem
Dim item As New MyDataItem()
With item
.Name1 = GetValueFromAttribute(e, "Name1")
.Name2 = GetValueFromAttribute(e, "Name2")
.Name3 = GetValueFromAttribute(e, "Name3")
' ...
.StateKey = GetValueFromAttribute(e.Element("state"), "key")
' ...
End With
Return item
End Function
Private Function GetValueFromAttribute(e As XElement, attrName As String) As String
If e Is Nothing Then Return Nothing
Dim attr = e.Attribute(attrName)
If attr Is Nothing Then Return Nothing
Return attr.Value
End Function
From this, you can select the relevant values like this:
Dim query = From x in xanswer.Descendants("address") Select GetMyDataItemFromXElement(x)
Afterwards, assign the result of the Query to the DataSource of the GridView and call DataBind(). Use the Properties of the MyDataItem
class when setting up the GridView.
For a good overview on how to use Linq to Xml in VB.NET see this link or this link for a more general description.