【问题标题】:How to get inner xml from string containing XMl如何从包含 XMl 的字符串中获取内部 xml
【发布时间】:2014-12-08 01:08:01
【问题描述】:

所以如果我有这个带有 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>"

我想从中制作两个不同的字符串,如下所示:

"<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>"

因为我想把它们放在列表中。我尝试使用拆分但没有结果

【问题讨论】:

  • 用 XmlDocument 打开它并循环浏览内容。
  • 但这会得到值。我需要用它以字符串形式制作小的 xmls

标签: c# xml string parsing split


【解决方案1】:

你可以用 LINQ 在一行中做到这一点:

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

【讨论】:

    猜你喜欢
    • 2015-10-23
    • 2013-11-05
    • 2011-06-15
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多