如何从包含XMl的字符串中获取内部xml

v.g*_*.g. 2 c# xml string parsing split

所以如果我有这个带有xml的示例字符串.

"<bookstore>
   <book genre='autobiography' publicationdate='1981-03-22' ISBN='1-861003-11-0'>
       <title>The Autobiography of Benjamin Franklin</title>
       <author age="40">
           <first-name>Benjamin</first-name>
           <last-name>Franklin</last-name>
       </author>
       <author age="56">
           <first-name>Barack</first-name>
           <last-name>Obama</last-name>
       </author>
       <price>8.99</price>
   </book>
 </bookstore>"
Run Code Online (Sandbox Code Playgroud)

我想从它做两个不同的字符串如下:

"<author age="40">
           <first-name>Benjamin</first-name>
           <last-name>Franklin</last-name>
       </author>"
Run Code Online (Sandbox Code Playgroud)

"<author age="56">
           <first-name>Barack</first-name>
           <last-name>Obama</last-name>
       </author>"
Run Code Online (Sandbox Code Playgroud)

因为我想把它们放在List中.我尝试使用Split但没有结果

Jon*_*lis 6

您可以使用LINQ在一行中执行此操作:

List<string> list = XDocument.Parse(xmlString)
                     .Descendants("author")
                     .Select(x => x.ToString())
                     .ToList();
Run Code Online (Sandbox Code Playgroud)