Xelement到gridview vb.net
您好,我有一个Xelement,显示以下xml代码:Xelement到gridview vb.net,vb.net,gridview,xelement,Vb.net,Gridview,Xelement,您好,我有一个Xelement,显示以下xml代码: <response xmlns="" state="ERROR" errormsg="Duplicates found" requestid=""> <address xmlns="" kgs="" corporateclientpc="" postofficeboxpc="" postofficeboxcity="" postofficebox="" nielsen="" building="" stree
<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>
最后编辑
这是3种方法的代码
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
但在模糊的疑问
它没有进入方法,只是跳过它直接进入
skmTabelle.DataGridView1.DataSource=query
最后是潜艇。
我相信这和辩论中的“x”有关。但是我不知道x是什么意思。你可以选择所有的
,或者选择这个来获得更一般的描述。@steve:你从哪里获得xml?它存储在一个文件中吗?@steve:我已经更新了我的答案,以便它符合你的要求。一个真正的动态方法更复杂。thx Markus,我现在将研究它,我暂停了,但现在我回来了,我准备好了MyDataItem类和2个函数。但是我有一个问题,它说attr.value->value不是system.collections.generic.IEnumerable(of system.xml.link.xelemnt)的成员@steve:修复了一些错误:元素(“状态”)到元素(“状态”),属性(attrName)到属性(attrName)。希望它能有所帮助。
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
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
Dim address = xanswer.Descendants("address")
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
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
Dim query = From x in xanswer.Descendants("address") Select GetMyDataItemFromXElement(x)