Skip to content

Instantly share code, notes, and snippets.

@dandohotaru
Last active February 10, 2018 19:57
Show Gist options
  • Save dandohotaru/358da2bc01f7d869ef7a1adbe3e7a063 to your computer and use it in GitHub Desktop.
Save dandohotaru/358da2bc01f7d869ef7a1adbe3e7a063 to your computer and use it in GitHub Desktop.
//<?xml version="1.0"?>
//<catalog>
// <book id="bk101">
// <author>Gambardella, Matthew</author>
// <title>XML Developer's Guide</title>
// <genre>Computer</genre>
// <price>44.95</price>
// <publish_date>2000-10-01</publish_date>
// <description>An in-depth look at creating applications
// with XML.</description>
// </book>
// <book id="bk102">
// <author>Ralls, Kim</author>
// <title>Midnight Rain</title>
// <genre>Fantasy</genre>
// <price>5.95</price>
// <publish_date>2000-12-16</publish_date>
// <description>A former architect battles corporate zombies,
// an evil sorceress, and her own childhood to become queen
// of the world.</description>
// </book>
//</catalog>
void Main()
{
var xml = @"<?xml version='1.0'?>
<catalog>
<book id='bk101'>
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id='bk102'>
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>";
var term = "rain";
var document = XDocument.Parse(xml);
document.Dump("XDocument");
var query = from element in document
.Descendants("catalog")
.Elements("book")
let book = new
{
id = element.Attribute("id").Value,
author = element.Element("author").Value,
title = element.Element("title").Value,
genre = element.Element("genre").Value
}
where book.author.ToLower().Contains(term)
|| book.title.ToLower().Contains(term)
|| book.genre.ToLower().Contains(term)
select book;
query.Dump();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment