c# - 使用C#从XML检索文本

标签 c# .net regex xml

我有一个包含以下内容的文本文件:

<Person>
    <Prenom>Jack</Prenom>
    <Nom>Jhon</Nom>
    <Adresse>4 rue de la Mélandine</Adresse>
    <Tél></Tél>
    <Email>email@gmail.com</Email>
    <PhotoPath>c:\Program Files\Zonedetec\Gestionnaire de tâche v2\Img\5295f1ea-372a-4f2f-8f32-c52e8a48cc0839105.png</PhotoPath>
    <Age>19</Age>
    <Id>4640434</Id>
</Person>
<Person>
    <Prenom>Jean</Prenom>
    <Nom>Delamar</Nom>
    <Adresse>13 rue de la Mélandine</Adresse>
    <Tél></Tél>
    <Email>email@gmail.com</Email>
    <PhotoPath>c:\Program Files\Zonedetec\Gestionnaire de tâche v2\Img\5295f1ea-372a-4f2f-8f32-c52e8a48cc0839105.png</PhotoPath>
    <Age>19</Age>
    <Id>4640434</Id>
</Person>

我想检索标签之间的所有值
例如,在一个列表中,我想检索和之间的值(此处为2)

我该怎么办?

我尝试了这个:
internal static void LoadPerson()
    {
        string data = File.ReadAllText(Main.PersonnePath);

        Regex regex = new Regex("<Person>(.*)</Person>");
        var v = regex.Match(data);
        string s = v.Groups[1].ToString();

        MessageBox.Show(s);
    }

除了s根本不包含任何东西

你能帮助我吗?
谢谢。

最佳答案

如果仅需要此值作为纯文本。您可以使用正则表达式或XMLSerializer或(Linq to XML)。

选择一种或另一种方法之前,需要分析的是:

1)我该怎么办?

1.a)如果您只需要每个标签内的纯文本。并且您将不执行任何验证/计算/重新解析器。您可以轻松地使用这两种方法。

1.a.1)使用正则表达式:

    public List<string> GetValueByRegex(string input)
    {
        string pattern = @"<Person>([\s\S]*?)</Person>";

        var matches = Regex.Matches(input, pattern);

        if (matches.All(m => !m.Success))
            return null;

        var result = new List<string>();
        foreach (Match match in matches)
        {
            result.Add(match.Groups[1].Value);
        }
        return result;
    }

1.a.2)使用XDocument解析Xml字符串

Important: XDocument requires that your XML have one root Tag to work. As Your XML has two root Tags. I forced it with string interpolation $"<root>{input}</root>"


    public List<string> GetValueByXmlParse(string input)
    {
        var result = new List<string>();
        var ensureThereAreOnlyOneRootTag = $"<root>{input}</root>";

        XDocument xmlDocument = XDocument.Parse(ensureThereAreOnlyOneRootTag);
        foreach(var personXml in xmlDocument.Root.Elements("Person"))
        {
            result.Add(String.Concat(personXml.Nodes()));
        }
        return result;
    }

1.b)如果您要对从XML提取的数据进行任何处理,最好将其解析为一个对象。

You can make Visual Studio generate one by copy the XML value and click in Edit > Paste Special > Paste XML As Classes.



@PavelAnikhouski已经分享了一个很好的例子。

2)我真的需要一个好的表现吗?

为了回答这个问题,我使用Benchmark nuget包来比较所有选项。结果如下:
|                Method |    Gen 0 | Allocated |
|---------------------- |---------:|----------:|
|       GetValueByRegex |   1.2207 |    2688 B |
|    GetValueByXmlParse | 115.6006 |  243536 B |

Gen 0 : GC Generation 0 collects per 1000 operations

Allocated : Allocated memory per single operation (managed only, inclusive, 1KB = 1024B)



因此,答案是:取决于您需要如何处理结果。希望我能帮助您做出决定。

最好的祝福

关于c# - 使用C#从XML检索文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59335342/

相关文章:

c# - casting三元表达式如何解决溢出编译错误

c# - 异步 : What practices in large classes?

C# 更改 DataGridView 中的列标题

c# - 如何检查一个接口(interface)是否在 C# 中扩展了另一个接口(interface)?

.net - .net 中的方法调用有多昂贵

visual-studio - 是否有正则表达式可以在一个句子中找到两个不同的词?

c# - 如何使用抽象类创建通用列表?

c# - 使表格在函数C#中可用

javascript - 如何使用 Regex 检查 URL 数组以查看是否返回原始文件夹

javascript - 替换字符串中的单词