【问题标题】:Xelement to gridview vb.netXelement到gridview vb.net
【发布时间】:2014-04-16 09:38:39
【问题描述】:

你好,我有一个 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 文本在&lt;address&gt; 标记处是我想在gridview 中显示的第一行。 &lt;possibleduplicates&gt; 标签是 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

EDIT:1(亲爱的读者在阅读cmets后阅读编辑)

你好马库斯,这行: 暗地址 = xanswer.Descendants("address")

我只有 xml 格式的行。现在我需要将行放入gridview。我需要做什么才能做到这一点?我的愿望是遍历 xml 和每个不是 "" 的属性,进入 gridview。但我不知道如何,而且我不知道这是可能的。简单的方法是在加载事件中添加列和标题,然后通过 gridview1.rows.add 带有属性 xanswer..@name1, ..... last 属性 逐行进入网格行。

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

干杯

PS:我的陈述中有任何逻辑错误,请告诉我,我是 vb.net 新手

编辑2:

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 = 查询 并结束子。 我相信这与争论中的“x”有关。但我不知道那个 x 是什么意思。

【问题讨论】:

    标签: vb.net gridview xelement


    【解决方案1】:

    您可以像这样选择所有&lt;address&gt; 节点:

    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 以获得更一般的说明。

    【讨论】:

    • @steve:你从哪里得到 xml?它是否存储在文件中?
    • @steve:我已经更新了我的答案,使其符合您的要求。真正动态的方法更复杂。
    • 谢谢马库斯,我现在会调查一下,我在暂停,但现在我回来了
    • 我准备好了 MyDataItem 类和 2 个函数。但我有一个问题,它说 attr.value -> value is no member from system.collections.generic.IEnumerable(of system.xml.link.xelemnt)
    • @steve:修正了一些拼写错误:Elements("state") 到 Element("state"),Attributes(attrName) 到 Attribute(attrName)。希望对您有所帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-16
    • 2012-11-07
    相关资源
    最近更新 更多