LINQ查询从XML上课内容解析 [英] LINQ query to parse content from XML to class
问题描述
我从我试图通过提取LINQ查询
XML文件的格式一些信息的XML如下所示:
< TopNode>
<样品名称=汤姆ID =0批=1>
<序列>
将; NextItem ID =10> Stand1&放大器; GT;&下; / NextItem>
< PreviousItem ID =9>&台上2安培; GT;< / PreviousItem>
< /序列>
< /采样>
<样品名称=山ID =1批=1>
<序列>
将; NextItem ID =1> Stand1&放大器; GT;&下; / NextItem>
将; NextItem ID =2> Stand3&放大器; GT;&下; / NextItem>
将; PreviousItem ID =3> Stand4&放大器; GT;&下; / PreviousItem>
< /序列>
< /采样>
。
。
。
< / TopNode>
我要得到这两个属性的列表 NextItem
和 PreviousItem
从查询。对于这一点,我定义一个类来得到结果。
公共类提取
{
公共字符串名称
{
获得;
组;
}
公众诠释_id
{
获得;
组;
}
}
LINQ查询(的foreach样本节点,得到PreviousItem和类对象NextItem内容):
VAR EnumerableContent =从XElement.Load项目(file.xml)元素( NextItem)
选择??? ...
问题是我该怎么办该数据在这里传递给类对象为每个样本节点上面的查询。
。
其次,上面只会让我NextItem节点。我怎样写查询为NextItem和PreviousItem?
修改
整体上,我得一个IEnumerable的每个每个序
节点,然后从thie查询返回总体的IEnumerable
EDIT2
分配键
这里给我这个错误。下面的查询
VAR映射= XDocument.Load(file.xml)
.Descendants(序列)
。选择(N => n.Descendants(PreviousItem)
.Union(n.Descendants(NextItem))
。选择(N1 =>新建提取{_id =(INT)n1.Attribute(ID),名称= n1.Value})
).ToDictionary< IEnumberable<菜单> ;, INT>(键=> key._id); //进一步转换这本字典来分类词典(accordign排序,以键)
XDOC VAR = XDocument.Load(....);
VAR的结果= xDoc.Descendants(NextItem)
.Union(xDoc.Descendants(PreviousItem))
。选择(N =>新建{ID = n.Attribute( ID)值,名称= n.Name,值= n.Value})。
- 编辑 -
VAR的结果= XDocument.Load(....)
.Descendants(序列)
。选择(N => N .Descendants(NextItem)
.Union(n.Descendants(PreviousItem))
。选择(N2 =方式>新{ID = n2.Attribute(ID)值,名称= n2.Name,值= n2.Value})
);
I have an xml from which I am trying to extract some information through LINQ query.
The format of the xml file looks like the following:
<TopNode>
<Sample name="Tom" id="0" batch="1">
<Sequences>
<NextItem id="10">Stand1 ></NextItem>
<PreviousItem id="9">Stand2 ></PreviousItem>
</Sequences>
</Sample>
<Sample name="Hill" id="1" batch="1">
<Sequences>
<NextItem id="1">Stand1 ></NextItem>
<NextItem id="2">Stand3 ></NextItem>
<PreviousItem id="3">Stand4 ></PreviousItem>
</Sequences>
</Sample>
.
.
.
</TopNode>
I have to get a list of attributes of both NextItem
and PreviousItem
from the query. For this, I defined a class to get my results.
public class Extract
{
public string name
{
get;
set;
}
public int _id
{
get;
set;
}
}
LINQ Query (foreach Sample node, get the PreviousItem and NextItem content in class object):
var EnumerableContent = from item in XElement.Load("file.xml").Elements("NextItem")
select ???...
The problem is how do I pass the data to the class objects here in the above query for each Sample node.
.
Secondly, the above will only get me NextItem nodes. How do I write the query for both NextItem and PreviousItem?
EDIT
On the whole, I have to get an IEnumerable for each for each of the Sequence
nodes, and then return the overall IEnumerable from thie query.
EDIT2
Assigning key
here is giving me this error. Query below
var mapping = XDocument.Load("file.xml")
.Descendants("Sequences")
.Select(n => n.Descendants("PreviousItem")
.Union(n.Descendants("NextItem"))
.Select(n1 => new Extract { _id = (int)n1.Attribute("id"), Name = n1.Value })
).ToDictionary<IEnumberable<Menu>, int>(key => key._id);//and further converting this dictionary to sorted dictionary (sorted accordign to keys)
var xDoc = XDocument.Load(....);
var result = xDoc.Descendants("NextItem")
.Union(xDoc.Descendants("PreviousItem"))
.Select(n => new {ID = n.Attribute("id").Value, Name =n.Name, Value =n.Value });
--EDIT--
var result = XDocument.Load(....)
.Descendants("Sequences")
.Select(n=> n.Descendants("NextItem")
.Union(n.Descendants("PreviousItem"))
.Select(n2 => new { ID = n2.Attribute("id").Value, Name = n2.Name, Value = n2.Value })
);
这篇关于LINQ查询从XML上课内容解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!