【问题标题】:ASP.NET/XML/LINQ: Extract Attribute from ElementASP.NET/XML/LINQ:从元素中提取属性
【发布时间】:2009-07-08 20:56:07
【问题描述】:

我在一个 XML 文件中有以下内容:

<entry>
...
    <link href="http://www.example.com/somelink" />
...
</entry>

我想提取“href”属性。我一直在获取我的元素列表,如下所示:

Dim productsDoc = XDocument.Parse(productXML.ToString())
Dim feed = From entry In productsDoc...<entry> Select entry

For Each entry In feed
    Dim product As New CartItem
    ...
    product._productid = entry.<id>.Value
    product._permalink = entry.<link>.Value 'HOW DO I GET THE ATTRIBUTE?

    allItems.Add(product)
Next

如何提取&lt;link&gt; href 属性?

谢谢!

【问题讨论】:

    标签: asp.net xml linq


    【解决方案1】:

    您好,要获取链接,您应该使用“XML Attribute Axis Property”。这使您可以轻松访问属性。

    你可以这样使用它:

    'this is how you get the href string
    product._permalink = entry.<link>.@href
    

    这是一个完整的使用示例:

    Module Module1
    
        ' some products xml to use for this example
        Dim productsXml As XElement = <Xml>
                                          <Product>
                                              <Name>Mountain Bike</Name>
                                              <Price>59.99</Price>
                                              <Link href="http://example.com/bike">Click here for a Mountain Bike</Link>
                                          </Product>
                                          <Product>
                                              <Name>Arsenal Football</Name>
                                              <Price>9.99</Price>
                                              <Link href="http://example.com/arsenalfootball">Click here for a Arsenal Football</Link>
                                          </Product>
                                          <Product>
                                              <Name>Formula One Cap</Name>
                                              <Price>14.99</Price>
                                              <Link href="http://example.com/f1cap">Click here for a Formula One Cap</Link>
                                          </Product>
                                          <Product>
                                              <Name>Robin Hood Bow</Name>
                                              <Price>8.99</Price>
                                              <Link href="http://example.com/robinhoodbow">Click here for a Robin Hood Bow</Link>
                                          </Product>
                                      </Xml>
    
        Sub Main()
    
            ' get all <Product> elements from the XDocument
            Dim products = From product In productsXml...<Product> _
                           Select product
    
            ' go through each product
            For Each product In products
                ' output the value of the <Name> element within product
                Console.WriteLine("Product name is {0}", product.<Name>.Value)
                ' output the value of the <Price> element within product
                Console.WriteLine("Product price is {0}", product.<Price>.Value)
                ' output the value of the <Link> element within product
                Console.WriteLine("Product link text is {0}", product.<Link>.Value)
                ' output the value of the <Link> href attribute within product
                Console.WriteLine("Product href is {0}", product.<Link>.@href)
            Next
    
        End Sub
    
    End Module
    

    【讨论】:

    • product._permalink = entry..@href 是我需要的,谢谢!
    • 没问题,我很高兴能帮上忙 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多