解析XML文件中两条连续的记录

本文关键字:两条 连续 记录 XML 文件 解析 | 更新日期: 2023-09-27 18:08:26

我有一个XML文件:

<root type="service">
  <Msg Date="03/23/2013 12:00:04 AM">Request'HANDSHAKE'</Msg>
  <Msg Date="03/23/2013 12:00:04 AM">Response'RSHANDSHAKE'</Msg>
  <Msg Date="03/23/2013 12:03:04 AM">Request'HANDSHAKE'</Msg>
  <Msg Date="03/23/2013 12:03:04 AM">Response'RSHANDSHAKE'</Msg>
  <Msg Date="03/23/2013 01:34:30 PM">Request 'IQ~bbabb3ff2-...DLE~VNECTRECVBDHANDLE'</Msg>
  <Msg Date="03/23/2013 01:34:30 PM">Response SIQ~7a23da12...RDHANDLE=O000000000014'</Msg>
</root>

我必须解析文件,看看是否每个请求后面都有响应。如果没有响应,则表明有错误。我如何在c#中做到这一点,最好是LINQ?

解析XML文件中两条连续的记录

应该使用像LINQ to XML这样的XML解析器。这样,您的代码就可以像这样:

XDocument doc = XDocument.Load("file.xml");
bool isError = (from e1 in doc.Root.Elements()
                where e1.Value.StartsWith("Request")
                let e2 = e1.ElementsAfterSelf().FirstOrDefault()
                where e2 == null || !e2.Value.StartsWith("Response")
                select e1).Any();

这个文学完成了您所要求的:对于每个Request元素,它检查以下元素是否存在并且是Response元素。

如果您有其他要求,例如Response元素本身也是无效的,您将不得不相应地修改代码

这样的东西会起作用,我刚刚在Visual Studio中测试了它,当然有更优雅的方法来做这使用XPATH过滤器/选择器的一部分…

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
namespace ParseXML
{
    class Program
    {
        static void Main(string[] args)
        {
            String xmlString =
        @"<root type=""service"">
                <Msg Date=""03/23/2013 12:00:04 AM"">Request'HANDSHAKE'</Msg>
                <Msg Date=""03/23/2013 12:00:04 AM"">Response'RSHANDSHAKE'</Msg>
                <Msg Date=""03/23/2013 12:03:04 AM"">Request'HANDSHAKE'</Msg>
                <Msg Date=""03/23/2013 12:03:04 AM"">Response'RSHANDSHAKE'</Msg>
                <Msg Date=""03/23/2013 01:34:30 PM"">Request 'IQ~bbabb3ff2-...DLE~VNECTRECVBDHANDLE'</Msg>
                <Msg Date=""03/23/2013 01:34:30 PM"">Response SIQ~7a23da12...RDHANDLE=O000000000014'</Msg>
            </root>";
            // Create an XmlReader
            using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
            {
                while (reader.ReadToFollowing("Msg"))
                {
                    string request = reader.ReadElementContentAsString();
                    if(reader.ReadToFollowing("Msg"))
                    {
                        string response = reader.ReadElementContentAsString();
                        if (request.Contains("Request") && response.Contains("Response"))
                        {
                            // Request/Response nodes identified...
                        }
                    }
                }
            }
        }
    }
}

这里有一个解决方案,适用于有效情况,也适用于错误,并为您提供确切的金额:

    String xmlString = @"<root type=""service"">
        <Msg Date=""03/23/2013 12:00:04 AM"">Request'HANDSHAKE'</Msg>
        <Msg Date=""03/23/2013 12:00:04 AM"">Response'RSHANDSHAKE'</Msg>
        <Msg Date=""03/23/2013 12:03:04 AM"">Request'HANDSHAKE'</Msg>
        <Msg Date=""03/23/2013 12:03:04 AM"">Response'RSHANDSHAKE'</Msg>
        <Msg Date=""03/23/2013 01:34:30 PM"">Request 'IQ~bbabb3ff2-...DLE~VNECTRECVBDHANDLE'</Msg>
        <Msg Date=""03/23/2013 01:34:30 PM"">Response SIQ~7a23da12...RDHANDLE=O000000000014'</Msg>
    </root>";
    String[] data = xmlString.Split(new string[] { "/Msg"},  StringSplitOptions.None);
    int errors = 0;
    Boolean secondLine = false;
    for (int i = 0; i+1 < data.Length; i ++ )
    {
        if (secondLine)
        {
            if (!data[i].Contains("Response"))
            {
                errors += 1;
            }
            secondLine = false;
        }
        else
        {
            if (!data[i].Contains("Request"))
            {
                errors += 1;
            }
            else
            {
                secondLine = true;
            }
        }
    }
    if (secondLine) { errors += 1; }
    System.Windows.Forms.MessageBox.Show("Errors: " + errors.ToString());