static void Main(string[] args) { XElement purchaseOrder = XElement.Load("Contacts.xml"); string partNos = (string)(from item in purchaseOrder.Descendants("City") select item).First(); Console.WriteLine(partNos); }
输出结果:Mercer Island
其中Contacts.xml文件如下:
<?xml version="1.0"?> <Contacts> <Contact> <Name>Patrick Hines</Name> <Phone Type="Home">206-555-0144</Phone> <phone Type="Work">425-555-0145</phone> <Address> <Street1>123 Main St</Street1> <City>Mercer Island</City> <State>WA</State> <Postal>68042</Postal> </Address> </Contact> </Contacts>
标签:XML,xml,string,Contacts,C#,LINQ,555,Main From: https://www.cnblogs.com/echo-efun/p/18418897