I have an XML file that I want to query with LINQ. I want for every record, create a new row. Here is what I have tried up till yet and have failed.
我有一个XML文件,我想用LINQ查询它。我想为每条记录创建一个新的行。这是我到现在还在尝试的,但失败了。
<?xml version="1.0" encoding="utf-8"?>
<categories xmlns="urn:schemas-pi-meta:categories" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:schemas-pi-meta:categories Xsd/meta.xml.config.xsd">
<category name="history">
<data>
<value name="customer">2</value>
<value name="truck">1</value>
</data>
<category name="record">
<data>
<value name="time">1/3/2013 2:22:41 PM</value>
<value name="quantity">3</value>
<value name="unit">barrels</value>
<value name="cancelled">false</value>
<value name="errored">false</value>
</data>
</category>
</category>
The file is longer so I have cut it down but it repeats itself.
这个文件比较长,所以我把它剪了下来,但是它自己重复。
This is what I have tried to do:
这就是我想做的:
XElement root = XElement.Load("D:\\Linq XM\\history.xml.config");
IEnumerable<XElement> address = from el in root.Elements("categories")
where (string)el.Attribute("category") == "record"
select el;
I have tried to change Elements value thinking that I am perhaps missing something but somehow the query isn't returning my data.
我试图改变元素的值,认为我可能遗漏了一些东西,但不知何故,查询并没有返回我的数据。
1 个解决方案
#1
8
There are four problems as far as I can see.
在我看来,有四个问题。
The first problem is that you're looking for categories
elements under the root element - when the categories
element is the root element. I suspect you really want to be looking for category
elements rather than categories
elements.
第一个问题是在根元素下查找类别元素——当categories元素是根元素时。我怀疑你真的想要寻找类别元素而不是类别元素。
The second problem is that you're trying to find an attribute named category
. It looks to me like you should be checking for an attribute named name
within elements called category
.
第二个问题是您试图找到一个名为category的属性。在我看来,您应该在名为category的元素中检查名为name的属性。
The third problem is that the category with a name
attribute of record
isn't actually a direct child element of categories
at all - it's a descendant, but not a direct child - so you should use Descendants
instead of Elements
.
第三个问题是,带有name属性的category实际上根本不是category的直接子元素——它是子类,而不是直接子类——所以应该使用子类而不是元素。
The fourth problem is that you're not specifying a namespace. This part of the file:
第四个问题是您没有指定名称空间。文件的这一部分:
<categories xmlns="urn:schemas-pi-meta:categories" ...
specifies that the default namespace for this element and descendants is the URI "urn:schemas-pi-meta:categories"
. So you need to specify that when you say what you're looking for.
指定该元素及其后代的默认名称空间是URI“urn:schemas-pi-meta:categories”。所以当你说你要找的东西时,你需要明确这一点。
Putting these all together, you'd get:
把这些放在一起,你会得到:
XNamespace ns = "urn:schemas-pi-meta:categories";
var query = from el in root.Descendants(ns + "category")
where (string) el.Attribute("name") == "record"
select el;
Or without a query expression (as it's more hassle than it's worth here):
或者没有查询表达式(因为它比这里的值更麻烦):
XNamespace ns = "urn:schemas-pi-meta:categories";
var query = root.Descendants(ns + "category")
.Where(el => (string) el.Attribute("name") == "record");
#1
8
There are four problems as far as I can see.
在我看来,有四个问题。
The first problem is that you're looking for categories
elements under the root element - when the categories
element is the root element. I suspect you really want to be looking for category
elements rather than categories
elements.
第一个问题是在根元素下查找类别元素——当categories元素是根元素时。我怀疑你真的想要寻找类别元素而不是类别元素。
The second problem is that you're trying to find an attribute named category
. It looks to me like you should be checking for an attribute named name
within elements called category
.
第二个问题是您试图找到一个名为category的属性。在我看来,您应该在名为category的元素中检查名为name的属性。
The third problem is that the category with a name
attribute of record
isn't actually a direct child element of categories
at all - it's a descendant, but not a direct child - so you should use Descendants
instead of Elements
.
第三个问题是,带有name属性的category实际上根本不是category的直接子元素——它是子类,而不是直接子类——所以应该使用子类而不是元素。
The fourth problem is that you're not specifying a namespace. This part of the file:
第四个问题是您没有指定名称空间。文件的这一部分:
<categories xmlns="urn:schemas-pi-meta:categories" ...
specifies that the default namespace for this element and descendants is the URI "urn:schemas-pi-meta:categories"
. So you need to specify that when you say what you're looking for.
指定该元素及其后代的默认名称空间是URI“urn:schemas-pi-meta:categories”。所以当你说你要找的东西时,你需要明确这一点。
Putting these all together, you'd get:
把这些放在一起,你会得到:
XNamespace ns = "urn:schemas-pi-meta:categories";
var query = from el in root.Descendants(ns + "category")
where (string) el.Attribute("name") == "record"
select el;
Or without a query expression (as it's more hassle than it's worth here):
或者没有查询表达式(因为它比这里的值更麻烦):
XNamespace ns = "urn:schemas-pi-meta:categories";
var query = root.Descendants(ns + "category")
.Where(el => (string) el.Attribute("name") == "record");