首页 > 解决方案 > 捕获无效的 XML

问题描述

我有以下代码用于解析包含对象集合的 XML 字符串。如果无法解析,我想将任何给定对象的 XML 捕获为字符串。如果解析不正确,我想存储它并分析它。我不希望做出根本性的改变,但我不知道如何抓取无效的 XML 部分并捕获它。xmlReader.ReadOuterXml() 无法解析时抛出异常。提前致谢。

// we want to read each canonical item in the report from oracle separately
            while (xmlReader.ReadToFollowing(ROOT_ELEMENT))
            {
                string itemXml = String.Empty;

                try
                {
                    // this gives us the whole segment of xml including the root element tag
                    itemXml = xmlReader.ReadOuterXml();
                    xmlReader.
                    T processedItem = default;

                    processedItem = reportMapper.Mapper(itemXml);
                    successfulItems.Add(new ProcessingResult<T>()
                    {
                        ProcessedItem = processedItem
                    });
                }

标签: c#xmlxmlreader

解决方案


推荐阅读