Xelement到gridview vb.net

时间:2014-04-16 09:38:40

标签: vb.net gridview xelement

您好我有一个Xelement,它显示了这个xml代码:

<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>

正如您在Xelement文本中<address>标记处我想要在gridview中显示的第一行一样。在<possibleduplicates>标签处有1到n个重复项,我想在gridview中添加它们。目的是用户可以轻松地比较重复项和原始数据流。 我在www中找到了这个代码,并尝试了一下。我想这是我的基础。我的第一个问题是并非网格的每一行都有相同的列。那么在特定的chema中创建列并将xml属性插入其中会更好吗? 下面的代码在gridview中创建了一个状态列,但它是空的..

Dim query = From responses In xanswer...<state> _
            Select New With {.state = responses.@state}
skmTabelle.DataGridView1.DataSource = query.ToList

跳跃你们伙计们可以帮助我。

干杯b101

编辑:1(亲爱的读者阅读评论后阅读编辑内容)

Markus,你好,这行:     Dim address = xanswer.Descendants(“address”)

我只有xml格式的行。现在我需要将行放入gridview。我需要做些什么来实现这一目标?我的想法是将xml和非“”的每个属性循环到gridview中。但我不知道怎么做,我也不知道这是可能的。简单的方法是在加载事件中添加列和标题,然后将行放入 gridview1.rows.add,属性为xanswer .. @ name1,..... last属性 进入行的网格行。

但我不想那样。好吧,如果我没有动态的方式工作,我需要这样做,但首先我想尝试以动态的方式。

欢呼声

PS:我在发言中看到任何逻辑上的失败请告诉我,我是vb.net新手

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

上次修改

这是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意味着什么。

1 个答案:

答案 0 :(得分:2)

您可以像这样选择所有<address>个节点:

Dim address = xanswer.Descendants("address")

为了显示GridView中的行,您需要一个通用结构,因为GridView中的所有行都具有相同的列。最好的方法是创建一个包含所有相关属性的类:

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

然后创建一个以XElement为输入的函数,并创建该类的实例,例如:

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)

然后,将Query的结果分配给GridView的DataSource并调用DataBind()。在设置GridView时使用MyDataItem类的属性。

有关如何在VB.NET中使用Linq to Xml的详细概述,请参阅此link或此link以获取更一般的说明。