Tag: LINQtoXML
<?xml version="1.0" encoding="utf-8" ?> <PersonList> <Persons id="soccer"> <Person> <Name>中田英寿</Name> <Age>34</Age> </Person> <Person> <Name>中村俊輔</Name> <Age>33</Age> </Person> </Persons> <Persons id="baseball"> <Person> <Name>ダルビッシュ</Name> <Age>25</Age> </Person> <Person> <Name>イチロー</Name> <Age>36</Age> </Person> </Persons> </PersonList>
var xdoc = XDocument.Load("../../persons.xml");
var persons = xdoc.Descendants("Person"); foreach (var person in persons) { Debug.WriteLine("##### start #####"); Debug.WriteLine(person.ToString()); Debug.WriteLine(""); }
##### start ##### <Person> <Name>中田英寿</Name> <Age>34</Age> </Person> ##### start ##### <Person> <Name>中村俊輔</Name> <Age>33</Age> </Person> ##### start ##### <Person> <Name>ダルビッシュ</Name> <Age>25</Age> </Person> ##### start ##### <Person> <Name>イチロー</Name> <Age>36</Age> </Person>
var personsCollection = ( from p in xdoc.Descendants("Persons") where p.Attribute("id").Value == "soccer" select p); foreach (var persons in personsCollection) { Debug.WriteLine(persons.ToString()); }
##### start ##### <Persons id="soccer"> <Person> <Name>中田英寿</Name> <Age>34</Age> </Person> <Person> <Name>中村俊輔</Name> <Age>33</Age> </Person> </Persons>