【问题标题】:How to find consecutive nodes with consecutive attribute values using XDocument or XElement?如何使用 XDocument 或 XElement 查找具有连续属性值的连续节点?
【发布时间】:2018-02-26 02:15:30
【问题描述】:

如何检查 xml 文件中包含属性的连续节点(用逗号和空格分隔),该属性的值的整数部分增加 +1,并将匹配的(如果有)字符串写入文件使用XDocumentXElement 方法?这是一个示例 xml 文件

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth <chk att="sdf1">A-list</chk>, <chk att="sdf2">B-list</chk>, <chk att="sdf3">X-list</chk> look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former <chk att="sdf14">lA-list</chk>, <chk att="sdf16">pB-list</chk>, <chk att="sdf3">sX-list</chk> architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
   <book id="bk103">
      <author>Corets, Eva</author>
      <title>Maeve Ascendant</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-11-17</publish_date>
      <description>After the collapse <chk att="sdf11">A-list</chk>, <chk att="sdf20">B-list</chk>, <chk att="sdf21">X-list</chk>, <chk att="sdf22">A-list</chk>, <chk att="sdf23">B-list</chk>, <chk att="sdf25">X-list</chk> of a nanotechnology 
      society in England, the young survivors lay the 
      foundation for a new society.</description>
   </book>
</catalog>

我想提取字符串

<chk att="sdf1">A-list</chk>, <chk att="sdf2">B-list</chk>, <chk att="sdf3">X-list</chk>

<chk att="sdf20">B-list</chk>, <chk att="sdf21">X-list</chk>, <chk att="sdf22">A-list</chk>, <chk att="sdf23">B-list</chk>

来自上面的 xml 文件? 我考虑过为此使用正则表达式 find 但我无法找到添加递增属性值的方法...

我该怎么做?

【问题讨论】:

    标签: c# linq-to-xml


    【解决方案1】:

    这是一种方法,

    请注意,没有错误检查,而且非常简洁,您需要在稳健性和清晰度这两个方面进行改进。

    using System;
    using System.Linq;
    using System.Text.RegularExpressions;
    using System.Xml.Linq;
    
    namespace ConsoleApp1
    {
        internal static class Program
        {
            private const string Text = @"<?xml version=""1.0""?>
    <catalog>
       <book id=""bk101"">
          <author>Gambardella, Matthew</author>
          <title>XML Developer's Guide</title>
          <genre>Computer</genre>
          <price>44.95</price>
          <publish_date>2000-10-01</publish_date>
          <description>An in-depth <chk att=""sdf1"">A-list</chk>, <chk att=""sdf2"">B-list</chk>, <chk att=""sdf3"">X-list</chk> look at creating applications 
          with XML.</description>
       </book>
       <book id=""bk102"">
          <author>Ralls, Kim</author>
          <title>Midnight Rain</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-12-16</publish_date>
          <description>A former <chk att=""sdf14"">lA-list</chk>, <chk att=""sdf16"">pB-list</chk>, <chk att=""sdf3"">sX-list</chk> architect battles corporate zombies, 
          an evil sorceress, and her own childhood to become queen 
          of the world.</description>
       </book>
       <book id=""bk103"">
          <author>Corets, Eva</author>
          <title>Maeve Ascendant</title>
          <genre>Fantasy</genre>
          <price>5.95</price>
          <publish_date>2000-11-17</publish_date>
          <description>After the collapse <chk att=""sdf11"">A-list</chk>, <chk att=""sdf20"">B-list</chk>, <chk att=""sdf21"">X-list</chk>, <chk att=""sdf22"">A-list</chk>, <chk att=""sdf23"">B-list</chk>, <chk att=""sdf25"">X-list</chk> of a nanotechnology 
          society in England, the young survivors lay the 
          foundation for a new society.</description>
       </book>
    </catalog>
    ";
    
            private static void Main(string[] args)
            {
                var document = XDocument.Parse(Text);
    
                var books = document.Element("catalog").Elements("book");
    
                foreach (var book in books)
                {
                    var attributes = book.Element("description").Elements("chk").Attributes("att");
    
                    var values = attributes.Select(s => int.Parse(Regex.Match(s.Value, @"\d+").Value)).ToArray();
    
                    var first = values.First();
                    var last = values.Last();
                    var sum = Enumerable.Range(first, Math.Max(0, last - first + 1)).ToArray().Sum();
    
                    var consecutive = values.Sum() == sum;
    
                    Console.WriteLine(consecutive);
                }
            }
        }
    }
    

    结果:

    True
    False
    False
    

    【讨论】:

    • 为什么最后一个结果是false,我还想提取整个匹配字符串,以防3个或更多连续节点具有连续值..检查问题,结果应该找到两个值
    • 你问一个范围何时是连续的,这就是它的作用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多