【问题标题】:Find XML Values based on regex基于正则表达式查找 XML 值
【发布时间】:2017-10-25 16:22:32
【问题描述】:

我有一个带有通配符值的配方 XML 文件(基于 * 符号)。我有代码应该确定匹配的值,但它没有找到任何结果。

我一直在阅读有关 LINQ 和正则表达式的所有内容,并关注 this

我做错了什么?

C# 代码

 Regex regEx = new Regex("450455-501", RegexOptions.IgnoreCase);

            XDocument recipefile = XDocument.Load(@"C:\testData\Recipes\Recipes.xml");

            var recipes = (from recipe in recipefile.Descendants("recipe").
               Where(r => regEx.IsMatch(r.Element("partnumber").Value))
                           select new
                           {
                               partnumber = recipe.Element("partnumber").Value,
                               recipename = recipe.Element("recipename ").Value,
                           });

            //print product attributes to the console
            foreach (var recipe in recipes)
            {
                MessageBox.Show("Product Name:    " + recipe.partnumber);
                MessageBox.Show("List Price:      " + recipe.recipename);
            }

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<body>
    <recipes>
    <recipe>
        <partnumber value="450455-50*" />
        <recipename value="porkchop" />
    </recipe>
    <recipe>
        <partnumber value="420100-1**" />
        <recipename value="porkchop" />
    </recipe>
    </recipes>
</body>

【问题讨论】:

  • 给定 xml 的预期结果是什么?
  • 您做错了什么是期望正则表达式 /450455-501/420100-1** 匹配,这没有任何意义,也没有迹象表明您试图找出正则表达式是什么。
  • 实际上,您的错误在于相信this pseudo-article 的作者比您更了解该主题。不幸的是,那篇文章毫无价值。关键是正则表达式本身的内容,文章中从未提及。
  • @EdPlunkett 好的 - 这篇文章太糟糕了 - 我仍然不知道该怎么做 - 我查了正则表达式,但不是很清楚 - 如果它只是正则表达式是错误的 - 但我担心的是我的代码在多个地方坏了 - 如果不是这样,那么好的
  • @user1438082 如果您在代码中发现了一个错误(这里是一个:该正则表达式字符串错误),请修复它。不用担心其他错误。修复你拥有的那个。使用调试器确保那一件事正常工作。如果代码然后在其他地方中断,那么担心那个错误。

标签: c# regex linq linq-to-xml


【解决方案1】:

这行得通

// Load the recipe
            XDocument doc = XDocument.Load(@"C:\test\Recipes\Recipes.xml");
            string response = "420100-199";

            //Note If you know the result should contain 0 or 1 elements, you can use FirstOrDefault() instead of ToList();
            var results = (from c in doc.Descendants("recipe")
                           from f in c.Descendants("partnumber")
                           where Regex.IsMatch(response ,WildcardToRegex((string)f.Attribute("value")))
                           select c).ToList();


            if (results.Count == 0)
            {
                MessageBox.Show("No Recipe Found  ");
                return;
            }

            else if(results.Count > 1)
            {
                MessageBox.Show("Error: More than 1 recipe matches the part number");

            }

            else
            {
                textBox_recipe.Text = string.Format("{0}", results[0].Element("recipename").Attribute("value").Value);
            }

// 使用*符号将通配符部件号转换为兼容正则表达式的方法

public static string WildcardToRegex(string pattern)
{
    return "^" + Regex.Escape(pattern)
                      .Replace(@"\*", ".*")
                      .Replace(@"\?", ".")
               + "$";
}

【讨论】:

  • 什么是WildCardToRegex?如果您不包含该函数的代码,则这不是问题的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-14
  • 1970-01-01
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多