如何获取具有特定主题标签值的注释节点
How to get a note node having particular hashtag value
这是我的 XML 结构:
<Note Id="2" Category="OFFICE" Date="12/6/2014 12:00:00 AM">
<Hashtag>#hashnotes</Hashtag>
<Hashtag>#hashnotes</Hashtag>
<Hashtag>#good</Hashtag>
<Text>this is #hashnotes app #hashnotes are #good</Text>
</Note>
我正在编写的用于在 C# 中使用 LINQ 搜索主题标签值的代码如下:
var user = XmlDoc.Element("HashNotes").Elements("Note")
.Where(e => e.Element("Hashtag").Value == hashtag);
但无法在更深的节点中搜索。
你能告诉我如何提取具有相同名称Hashtag的Elements的值吗?
这应该有效:-
XDocument xdoc = XDocument.Load(@"YourXMLPath.xml");
List<string> result = xdoc.Descendants("Note").Elements("Hashtag")
.Where(x => x.Value == hashtag)
.Select(x => x.Value).ToList();
但是,这显然会给出相同值的列表hashtag
,如果您需要完整的节点,请不要应用Value
属性。
更新:
要检索其他值,您可以这样做:--
var result = xdoc.Descendants("Hashtag")
.Where(x => x.Value == hashtag)
.Select(x => new
{
HashTag = x.Value,
Id = x.Parent.Attribute("Id").Value,
Category = x.Parent.Attribute("Category").Value,
Date = x.Parent.Attribute("Date").Value
});
您当前的代码将 return Note
元素包含 #hashtag
值。
通过添加另一层进一步优化搜索,例如,
var list = doc.Element("HashNotes")
.Elements("Note")
.Elements("Hashtag")
.Where(p=>p.Value == "#hashnotes");
现在 return Hashtag
个元素。
//更新
要提取相关的 Note 元素,您只需为预期的索引调用 .Parent
属性。
int idx_wanted = 0;
return list[idx_wanted].Parent;
这是一个如何操作的例子:
stirng Xml = @"<Note Id='2' Category='OFFICE' Date='12/6/2014 12:00:00 AM'>
<Hashtag>#hashnotes</Hashtag>
<Hashtag>#hashnotes</Hashtag>
<Hashtag>#good</Hashtag>
<Text>this is #hashnotes app #hashnotes are #good</Text>
</Note>";
string SearchParam = "#hashnotes";
XElement element = XElement.Parse(Xml);
var nodes= element.Descendants("Hashtag").Where(e => e.Value == SearchParam);
如果您想从磁盘上的 xml 文件进行操作,则:
XDocument document = XDocument.Load("FileUri");
var nodes = document.Descendants("Hashtag").Where(e => e.Value == SearchParam);
我正在加载 xml 作为字符串,在你的情况下它也可以作为字符串或来自 xml 文件。
这是我的 XML 结构:
<Note Id="2" Category="OFFICE" Date="12/6/2014 12:00:00 AM">
<Hashtag>#hashnotes</Hashtag>
<Hashtag>#hashnotes</Hashtag>
<Hashtag>#good</Hashtag>
<Text>this is #hashnotes app #hashnotes are #good</Text>
</Note>
我正在编写的用于在 C# 中使用 LINQ 搜索主题标签值的代码如下:
var user = XmlDoc.Element("HashNotes").Elements("Note")
.Where(e => e.Element("Hashtag").Value == hashtag);
但无法在更深的节点中搜索。 你能告诉我如何提取具有相同名称Hashtag的Elements的值吗?
这应该有效:-
XDocument xdoc = XDocument.Load(@"YourXMLPath.xml");
List<string> result = xdoc.Descendants("Note").Elements("Hashtag")
.Where(x => x.Value == hashtag)
.Select(x => x.Value).ToList();
但是,这显然会给出相同值的列表hashtag
,如果您需要完整的节点,请不要应用Value
属性。
更新:
要检索其他值,您可以这样做:--
var result = xdoc.Descendants("Hashtag")
.Where(x => x.Value == hashtag)
.Select(x => new
{
HashTag = x.Value,
Id = x.Parent.Attribute("Id").Value,
Category = x.Parent.Attribute("Category").Value,
Date = x.Parent.Attribute("Date").Value
});
您当前的代码将 return Note
元素包含 #hashtag
值。
通过添加另一层进一步优化搜索,例如,
var list = doc.Element("HashNotes")
.Elements("Note")
.Elements("Hashtag")
.Where(p=>p.Value == "#hashnotes");
现在 return Hashtag
个元素。
//更新
要提取相关的 Note 元素,您只需为预期的索引调用 .Parent
属性。
int idx_wanted = 0;
return list[idx_wanted].Parent;
这是一个如何操作的例子:
stirng Xml = @"<Note Id='2' Category='OFFICE' Date='12/6/2014 12:00:00 AM'>
<Hashtag>#hashnotes</Hashtag>
<Hashtag>#hashnotes</Hashtag>
<Hashtag>#good</Hashtag>
<Text>this is #hashnotes app #hashnotes are #good</Text>
</Note>";
string SearchParam = "#hashnotes";
XElement element = XElement.Parse(Xml);
var nodes= element.Descendants("Hashtag").Where(e => e.Value == SearchParam);
如果您想从磁盘上的 xml 文件进行操作,则:
XDocument document = XDocument.Load("FileUri");
var nodes = document.Descendants("Hashtag").Where(e => e.Value == SearchParam);
我正在加载 xml 作为字符串,在你的情况下它也可以作为字符串或来自 xml 文件。