|
別解。XPathでやってみた。
using System;
using System.Xml.XPath;
class Program
{
static void Enumerate(string xml, string path)
{
System.IO.StringReader reader = new System.IO.StringReader(xml);
XPathDocument doc = new XPathDocument(reader);
XPathNavigator nav = doc.CreateNavigator();
foreach (var item in nav.Select(path))
{
Console.WriteLine(item);
}
}
static void Main(string[] args)
{
Enumerate(
"<?xml version='1.0' ?>" +
"<roop>" +
" <structure>" +
" <item>TEST1</item>" +
" <item>TEST2</item>" +
" <item>TEST3</item>" +
" </structure>" +
"</roop>",
"/roop/structure/item");
Console.WriteLine();
Enumerate(
"<?xml version='1.0' ?>" +
"<roop>" +
" <structure>" +
" <item value='TEST1' />" +
" <item value='TEST2' />" +
" <item value='TEST3' />" +
" </structure>" +
"</roop>",
"/roop/structure/item/@value");
Console.WriteLine();
}
}
|