尝试从节点创建XML文档并获取“无效的XML文档。该文档没有根元素。”

时间:2019-01-23 13:44:41

标签: c# xml qif

我正在尝试从某些XML中提取单个节点,并将该节点及其所有子内容写入新的XML文档,然后将其保存到磁盘。 在某些情况下(例如我的测试用例),存在多个符合条件的节点,并且必须将所有节点都写入单独的文件中。

到目前为止,我的函数中的所有内容均按预期工作,直到我将文件写入磁盘为止。 引发的异常是:“引发的异常:System.Xml.dll中的'System.Xml.XmlException'” 并且附带的消息是“无效的XML文档。该文档没有根元素。”我该如何解决?

我添加了很多控制台输出,以表明每个阶段都按预期工作。 我的功能如下:

private void ExtractQIF()
        {
            var httpClient = new HttpClient();
            int QIFFound = 0;
            try
            {
                string assetsTarget = httpAuthority + "/asset";                
                var XMLRaw = httpClient.GetStringAsync(assetsTarget);
                XmlDocument XML = new XmlDocument();
                XML.LoadXml(XMLRaw.Result);                                             
                XmlNodeList QIFDocuments = XML.GetElementsByTagName("QIFDocument"); 
                Console.WriteLine("QIF Documents found: " + QIFDocuments.Count);        // WE ARE SUCCESSFULLY FINDING QIFDOCS IN THE ASSETS REQ
                foreach (XmlNode QIFDoc in QIFDocuments)
                {
                    QIFFound++;
                    try
                    {
                        Console.WriteLine(QIFDoc.InnerXml); // Debug shizzle
                        Console.WriteLine($"Document #{QIFFound} is being saved");
                        XmlDocument OutputQIFFile = new XmlDocument();
                        OutputQIFFile.ImportNode(QIFDoc, true);
                        Console.WriteLine("Node Imported to Document"); // Debug Shizzle
                        OutputQIFFile.Save(QIFSaveLocation); // This is the bastard line that isn't working
                        Console.WriteLine($"Document {QIFFound} Saved to Disk"); // Debug Shizzle
                        //Finally we should change the file extension to .qif
                    }
                    catch (Exception balls)
                    {
                        Console.WriteLine("COULD NOT EXTRACT QIF: Failed at document " + QIFFound);
                        Console.WriteLine(balls.Message);
                    }

                }
                Console.WriteLine("All QIF written to disk");                
            }
            catch
            { MessageBox.Show("COULD NOT EXTRACT ALL QIF");}

        }

相关的控制台信息如下:

QIF Documents found: 2
<QPId xmlns="http://qifstandards.org/xsd/qif3">ec871a54-5bb5-470b-9ff7-de757d7726a1</QPId><Version xmlns="http://qifstandards.org/xsd/qif3"><TimeCreated>2019-01-22T15:51:20.874+00:00</TimeCreated></Version><Header xmlns="http://qifstandards.org/xsd/qif3">.... Blah Blah Blah, a shit tonne of XML
Document #1 is being saved
Node Imported to Document
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
COULD NOT EXTRACT QIF: Failed at document 1
Invalid XML document. The document does not have a root element.
<QPId xmlns="http://qifstandards.org/xsd/qif3">27400291-ea3d-41de-afee-69d186c4288f</QPId><Version xmlns="http://qifstandards.org/xsd/qif3"><TimeCreated>2019-01-22T15:50:16.827+00:00</TimeCreated></Version><Header xmlns="http://qifstandards.org/xsd/qif3"><Application> .... Blah Blah Bla, a shit tonne of XML
Document #2 is being saved
Node Imported to Document
Exception thrown: 'System.Xml.XmlException' in System.Xml.dll
COULD NOT EXTRACT QIF: Failed at document 2
Invalid XML document. The document does not have a root element.
All QIF written to disk

我也尝试过将节点附加到文档本身,但是围绕它们的错误和建议似乎使我回到了上面的实现方式。

在我看来,该节点已正确填充。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

对于每个the docsImportNode仅复制并返回指定的节点,实际上并没有将其插入文档中。在给定的示例中,您可以看到需要添加该节点。

因此,要进行这项工作,您需要进行更改以执行以下操作:

var outputQIFFile = new XmlDocument();

var newQifNode = outputQIFFile.ImportNode(QIFDoc, true);

outputQIFFile.AppendChild(newQifNode);

outputQIFFile.Save(QIFSaveLocation);

但是,如注释中所建议,我强烈建议您查看LINQ to XML(XDocument和朋友)。这是一个更加现代且用户友好的API。