LINQ到XML查询不拾取任何值 [英] Linq to XML query not picking up any values

查看:138
本文介绍了LINQ到XML查询不拾取任何值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要通过搜索XML文档。主要结构如下:

I have an XML document that I'm trying to search through. The Main structure is as follows:

<?xml version="1.0"?>
<!DOCTYPE ONIXMessage SYSTEM "http://www.editeur.org/onix/2.1/reference/onix-international.dtd">
<ONIXMessage xmlns="http://www.editeur.org/onix/2.1/reference" release="2.1">
  <Header>
    <FromCompany>MyCo</FromCompany>
    <FromPerson>Joe Bloggs</FromPerson>
    <FromEmail>joe@bloggs.com</FromEmail>
    <SentDate>20120522</SentDate>
  </Header>
  <Product>
    <ProductForm>DG</ProductForm>
    <Title>
      <TitleType>01</TitleType>
      <TitleText>Blogg</TitleText>
    </Title>
    <WorkIdentifier>
      <WorkIDType>15</WorkIDType>
      <IDValue>PI3564231</IDValue>
    </WorkIdentifier>
    <Language>
      <LanguageRole>01</LanguageRole>
      <LanguageCode>eng</LanguageCode>
    </Language>
  </Product>
</ONIXMessage>

这显然缩短停机简单,但产品标签中,有很多的更多信息。 A文件也可以有任意数量的产品的标签。

It's obviously shortened down for simplicity, but within the 'Product' tag, there is a lot more information. A File can also have any number of 'Product' tags.

我使用LINQ to XML来搜索这个文件,并采取内容到我的数据库,但我的问题是,我的查询是不是在文件中拿起任何产品的标签。这里就是产品应拿起code的片段:

I'm using linq to xml to search this document and take contents into my database, however my issue is that my query is not picking up any 'Product' tags in the file. Here is the snippet of code where the product should be picked up:

    XElement onix = XElement.Load(fs);
    // Get all the product information.
    //
    var products = from prod in onix.Elements("Product") select prod;

    foreach (var product in products)
    {
            //Process product
    }

已经通过code调试,我可以看到变量ONIX正在填充。我可以看到在该变量中的文件的全部内容。 '产品'但不被填充。

Having debugged through the code, I can see that the variable 'onix' is being populated. I can see the entire contents of the file in that variable. 'products' however is not being populated.

任何人都可以看看是否有什么我做错了吗?

Can anyone see if there's something I'm doing wrong?

推荐答案

没有命名空间:

var products = onix.Descendants()
             .Where(m => m.Name.LocalName == "Product")
             .ToList();

这篇关于LINQ到XML查询不拾取任何值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆