gpt4 book ai didi

c# - Linq to XML 未获取任何元素

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:29 25 4
gpt4 key购买 nike

正在尝试解析这个 xml 文件

<?xml version="1.0" encoding="UTF-8"?>
<feed gd:kind="shopping#products"
gd:etag="&quot;pUAQLMOCjdnNh1LcpqK05ByVY7g/Gsdf7eFzzI3Z-HW0Z-XItKMrp8c&quot;"
xmlns="http://www.w3.org/2005/Atom"
xmlns:gd="http://schemas.google.com/g/2005"
xmlns:s="http://www.google.com/shopping/api/schemas/2010">
<entry gd:kind="shopping#product">
<s:product>
<s:googleId>11111</s:googleId>
<s:author>
<s:name>ebay.de</s:name>
<s:accountId>11111</s:accountId>
</s:author>
<s:creationTime>2012-08-16T19:20:24.000Z</s:creationTime>
<s:modificationTime>2013-03-01T17:50:54.000Z</s:modificationTime>
<s:country>It</s:country>
<s:language>de</s:language>
<s:title>Blomus Rund-Magnete</s:title>
<s:brand>Blomus</s:brand>
<s:condition>new</s:condition>
<s:mpns>
<s:mpn>66742</s:mpn>
</s:mpns>
<s:inventories>
<s:inventory channel="online" availability="inStock">
<s:price shipping="4.9" currency="EUR">4.2</s:price>
</s:inventory>
</s:inventories>
<s:images>
</s:images>
</s:product>
</entry>
</feed>

林克:

XNamespace s = "http://www.google.com/shopping/api/schemas/2010";

var q = from product in XMLDoc.Root.Elements("entry").Elements(s+"product")
let inventory = product.Element(s + "inventories").Element(s + "inventory")
let price = (decimal)inventory.Element(s + "price")
let shipping = (decimal)inventory.Element(s +"price").Attribute("shipping")
let totalprice = price + shipping
select new
{
Name = (string)product.Element(s + "author").Element(s + "name"),
Shipping = shipping,
TotalPrice = totalprice,
Price = price+shipping
};

返回的 q 计数始终为 0,可能是哪里出了问题?!

最佳答案

您的条目 缺少 namespace 。我试图将其替换为 s:entry (和 XMLDoc.Root.Elements("entry")XMLDoc.Root.Elements(s + "entry")) 并且您的查询工作正常。

问题是您已经定义了默认命名空间 xmlns="http://www.w3.org/2005/Atom"。所以,对于没有前缀的元素,你也必须使用它:

XNamespace def = "http://www.w3.org/2005/Atom";

然后是 xDoc.Root.Elements(def + "entry")

关于c# - Linq to XML 未获取任何元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15256746/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com