Skip to content

Instantly share code, notes, and snippets.

@aoi0308
Last active December 11, 2015 20:09
Show Gist options
  • Save aoi0308/4653311 to your computer and use it in GitHub Desktop.
Save aoi0308/4653311 to your computer and use it in GitHub Desktop.
xml.GetElementById("hoge") 相当のことを LINQ to XML でする。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
const string xmlstr = "<root><p>aaa</p><p id=\"hoge\">bbb</p><p>ccc</p></root>";
//Console.WriteLine(xmlstr);
XDocument xml = XDocument.Parse(xmlstr);
//Console.WriteLine(xml);
var ps = from el in xml.Descendants() where (string)el.Attribute("id") == "hoge" select el;
Console.WriteLine(ps.First());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment