ASP.NET:尝试解析Google Base XML,无法访问“g:”标记

时间:2009-07-08 19:39:22

标签: asp.net xml linq namespaces

我的客户不想要数据库,但更愿意在XML文件中更新他们的数据。这一切都很好。但是,他们也希望将自己的商品提交给Google产品。这使事情变得复杂一些。我决定尝试将Google XML文件用于数据库,而不是创建和维护两个单独的文件,但我只是遇到了障碍。我的XML是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<feed xmlns:g="http://base.google.com/ns/1.0" 
xmlns:c="http://base.google.com/cns/1.0" xmlns="http://www.w3.org/2005/Atom">
<title>Company Product and Price Catalog</title>
<link rel="self" href="http://www.example.com" />
<author>
    <name>Some Company</name>
</author>
<id>tag:example.com:/App_Data/</id>
    <entry>
             <id>1</id>
        <title>Product Title</title>
        <g:price>100.00</g:price>
        <link href="http://www.example.com/product" />
        <g:image_link>http://www.example.com/images/product.jpg</g:image_link>
        <g:condition>new</g:condition>
        <g:brand>Brand Name</g:brand>
        <payment_accepted>cash,check,invoice,amex,discover,mastercard,
visa,googlecheckout</payment_accepted>
        <g:payment_notes>Google Checkout accepted</g:payment_notes>
    </entry>
....

我提取这些数据的代码是这样的:

Dim allItems As New List(Of CartItem)
        Dim productXML As XDocument = XDocument.Load( _
Current.Server.MapPath("/App_Data/products.xml"))


    Dim productsDoc = System.Xml.Linq.XDocument.Parse(productXML.ToString())
    Dim products = From entry In productsDoc...<entry> Select entry

    For Each entry In products
        Dim product As New CartItem
        Dim nameAndOptions() As String = Split(entry.<title>.Value, " - ")
        product.ProductName = nameAndOptions(0)
        If nameAndOptions.Length = 2 Then
            product.[Option] = nameAndOptions(1)
        End If
        product.Price = entry.<g:price>.Value 'problem here!'
        product._productid = entry.<id>.Value
        product._permalink = entry.<link>.Value
        allItems.Add(product)
    Next

    Return allItems

VS08中的“g:price”中的“g”下出现蓝色波浪线,错误为“xml名称空间前缀'g'未定义”。我该如何解决?或者这只是一个坏主意,我应该回去维护两个单独的文件?

谢谢!

1 个答案:

答案 0 :(得分:2)

尝试将以下行添加到文件顶部:

Imports <xmlns:g="http://base.google.com/ns/1.0">

(我从未做过VB 2008,因此语法可能会关闭)

编辑:请参阅here