示例#1
0
        public static bool LoadXmlFile(string xmlFilePath, out XmlFragmentList fragments, out string errorMsg,
                                       bool useRDMStructMode = true)
        {
            var result = LoadXmlFileAsync(xmlFilePath, useRDMStructMode).GetAwaiter().GetResult();

            fragments = result.Item2;
            errorMsg  = result.Item3;
            return(result.Item1);
        }
示例#2
0
        // ProcessXMLDataAsync method, used to parse the the XML data, input is Stream
        // Returns multiple values as the method is async method so it's not allow out or ref syntax
        // Return value as <success(boolean),XMLFragmentList,error message>
        private static async Task <Tuple <bool, XmlFragmentList, string> > ProcessXmlDataAsync(Stream xmlStream,
                                                                                               bool useRDMStrictMode = true)
        {
            var errorMessage = string.Empty;
            var settings     = new XmlReaderSettings {
                ConformanceLevel = ConformanceLevel.Fragment, Async = true
            };
            var fragments        = new XmlFragmentList();
            var xmlFragmentIndex = 0;
            var bSuccess         = true;
            var bValidTag        = false;

            using (var reader = XmlReader.Create(xmlStream, settings))
            {
                try
                {
                    while (await reader.ReadAsync().ConfigureAwait(false))
                    {
                        switch (reader.NodeType)
                        {
                        case XmlNodeType.Comment:
                        {
                            if (useRDMStrictMode)
                            {
                                var commentVal = reader.Value.Trim();
                                if (commentVal.ToLower().StartsWith("incoming") ||
                                    commentVal.ToLower().StartsWith("outgoing"))
                                {
                                    xmlFragmentIndex++;
                                    var fragment = new XMLFragment
                                    {
                                        FragmentNumber = xmlFragmentIndex,
                                        IsIncomingMsg  =
                                            commentVal.ToLower().StartsWith("incoming"),
                                        IsOutGoingMsg =
                                            commentVal.ToLower().StartsWith("outgoing"),
                                        MsgTypeRawXmlData = reader.Value
                                    };
                                    bValidTag = true;
                                    fragments.Add(xmlFragmentIndex, fragment);
                                }
                                else if (bValidTag && commentVal.ToLower().StartsWith("rwfmajorver"))
                                {
                                    fragments.Get(xmlFragmentIndex).RWFMajorMinorVersion           = commentVal;
                                    fragments.Get(xmlFragmentIndex).RWFMajorMinorVersionRawXmlData =
                                        reader.Value;
                                }
                                else if (bValidTag && StartWithDayOfWeek(commentVal.ToLower())
                                         ) // For EMA Java Trace
                                {
                                    fragments.Get(xmlFragmentIndex).TimeStamp      = commentVal;
                                    fragments.Get(xmlFragmentIndex).TimeRawXmlData = reader.Value;
                                }
                                else if (bValidTag && commentVal.ToLower().StartsWith("time:") &&
                                         fragments.Count > 0)
                                {
                                    var strArray = commentVal.Split();
                                    var timeStr  = strArray.Length > 1 ? strArray[1].Trim() : "";

                                    fragments.Get(xmlFragmentIndex).TimeStamp      = timeStr;
                                    fragments.Get(xmlFragmentIndex).TimeRawXmlData = reader.Value;
                                }
                            }

                            await reader.SkipAsync().ConfigureAwait(false);
                        }
                        break;

                        case XmlNodeType.Element:
                        {
                            if (!useRDMStrictMode)
                            {
                                xmlFragmentIndex++;
                                var fragment = new XMLFragment
                                {
                                    FragmentNumber    = xmlFragmentIndex,
                                    IsIncomingMsg     = false,
                                    IsOutGoingMsg     = false,
                                    MsgTypeRawXmlData = string.Empty
                                };

                                fragments.Add(xmlFragmentIndex, fragment);
                            }


                            if (fragments.Get(xmlFragmentIndex) == null)
                            {
                                bSuccess     = false;
                                errorMessage =
                                    $"Cannot find Fragment# {xmlFragmentIndex} in the list\r\nThere could be problem from missing required comments before parsing the XML Element\r\n";

                                continue;
                            }

                            fragments.Get(xmlFragmentIndex).RdmMessageType = reader.Name;
                            var nodeElem    = XNode.ReadFrom(reader);
                            var xmlRawValue = nodeElem;
                            fragments.Get(xmlFragmentIndex).RawXmlData = xmlRawValue.ToString();
                            bValidTag = false;
                        }
                        break;

                        default:
                            await reader.SkipAsync().ConfigureAwait(false);

                            continue;
                        }
                    }
                }
                catch (Exception ex)
                {
                    bSuccess = false;
                    fragments.Remove(xmlFragmentIndex);
                    errorMessage =
                        $"Detect and error when parsing the XML fragment\r\n{ex.Message}\r\nFragment {xmlFragmentIndex} has been removed";
                    await reader.SkipAsync();
                }
            }

            return(new Tuple <bool, XmlFragmentList, string>(bSuccess, fragments, errorMessage));
        }