C# 检索Xml文件中包含文本“Fixed”的所有消息的GUID,并使用c存储它们

C# 检索Xml文件中包含文本“Fixed”的所有消息的GUID,并使用c存储它们,c#,xml,C#,Xml,这是我的Xml文件:- <commit><subject><![CDATA[Fixed:Bu| Integer Fields Not Saving with >10 digits [ 3f19d010-c472-4ae6-9d29-b727707fe867 ]]]></subject></commit> <commit><subject><![CDATA[Merge branch 'iq17' of

这是我的Xml文件:-

<commit><subject><![CDATA[Fixed:Bu| Integer Fields Not Saving with >10 digits [ 3f19d010-c472-4ae6-9d29-b727707fe867 ]]]></subject></commit>
<commit><subject><![CDATA[Merge branch 'iq17' of github.iqsmartapp.com:DEV/EnterpriseDesktop into iq17]]></subject></commit><commit><subject><![CDATA[Fixed:Bug-5 | Regression - [Desktop - Action Menu] - Item Print option do not download the PDF file instead displaying the blank screen in the next window. [ def7b8cb-6819-4380-9a57-5af635f9b70b ]]]></subject></commit>

试试这样的

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Text.RegularExpressions;

namespace ConsoleApplication34
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<commit><subject><![CDATA[Fixed:Bu| Integer Fields Not Saving with >10 digits [ 3f19d010-c472-4ae6-9d29-b727707fe867 ]]]></subject></commit>" +
                "<commit><subject><![CDATA[Merge branch 'iq17' of github.iqsmartapp.com:DEV/EnterpriseDesktop into iq17]]></subject></commit><commit><subject><![CDATA[Fixed:Bug-5 | Regression - [Desktop - Action Menu] - Item Print option do not download the PDF file instead displaying the blank screen in the next window. [ def7b8cb-6819-4380-9a57-5af635f9b70b ]]]></subject></commit>";

            xml = "<Root>" + xml + "</Root>";

            StringReader sReader = new StringReader(xml);
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.CheckCharacters = false;
            XmlReader reader = XmlReader.Create(sReader);

            XDocument  elements = XDocument.Load(reader); 
            List<string> subjects = elements.Descendants("subject").Select(x => (string)x).ToList();


            List<string> guids = subjects.Select(x => Regex.Match(x, @"(?'guid'([a-z0-9]+-){4}[a-z0-9]+)").Groups["guid"].Value.Trim()).Where(x => x.Length > 0).ToList(); 

         } 


    }


}
如果您在每个CDATA中有多个GUID,那么请使用

List<string> guids = subjects.Select(x => Regex.Matches(x, @"(?'guid'([a-z0-9]+-){4}[a-z0-9]+)").Cast<Match>()
                .Select(y => y.Groups["guid"].Value.Trim()).Where(y => y.Length > 0)).SelectMany(x => x).ToList(); 
检查是否有固定的

List<string> guids = subjects.Where(x => x.Contains("Fixed:Bu")).Select(x => Regex.Matches(x, @"(?'guid'([a-z0-9]+-){4}[a-z0-9]+)").Cast<Match>()
                .Select(y => y.Groups["guid"].Value.Trim()).Where(y => y.Length > 0)).SelectMany(x => x).ToList(); 

我们真的很想帮助你,但你的问题的设计方式并没有明确说明你的问题和/或问题是什么。这甚至不是一个问题,只是对代码的要求。如果有人知道答案,请帮助这个人。如果有人知道问题,请帮助我们。你想检索什么值?你是loo吗king for xpath?澄清其他内容。抱歉。我想从subject标记获取GUID。因为消息包含固定的文本:谢谢jdweng。我尝试了你的解决方案,它奏效了。现在,假设我有一个文件,其中有大约20行类似于此。现在,对于每个cdata,我有一条消息,但对于某些消息,它包含Fi对于这些固定的,我希望相应的正则表达式存储在GUID中。如果一个标记中有多个GUID,则只需更改Match to Match集合。我更新了代码。