Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq到XML没有获取任何元素_C#_Xml_Linq_Xml Parsing_Xml Namespaces - Fatal编程技术网

C# Linq到XML没有获取任何元素

C# Linq到XML没有获取任何元素,c#,xml,linq,xml-parsing,xml-namespaces,C#,Xml,Linq,Xml Parsing,Xml Namespaces,正在尝试解析此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:

正在尝试解析此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>

返回的
q
计数始终为0,可能有什么问题

您的
条目
缺少命名空间。我尝试将其替换为
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”)

谢谢您的指点!事实上,我认为名称空间应该总是有前缀,但事实并非如此,这是关于名称空间的,现在它已修复,谢谢@用户1590636,检查我的更新。您不需要总是使用前缀,但在这里您重新定义了默认名称空间,因此您必须使用前缀或使用您重新定义的默认名称空间。
XNamespace def = "http://www.w3.org/2005/Atom";