首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >尝试从一个节点创建一个XML文档,并得到“无效的XML文档。文档没有根元素”。

尝试从一个节点创建一个XML文档,并得到“无效的XML文档。文档没有根元素”。
EN

Stack Overflow用户
提问于 2019-01-23 21:44:41
回答 1查看 558关注 0票数 0

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

到目前为止,我的函数中的所有内容都按预期工作,直到我将文件写入磁盘。抛出的异常是:“异常抛出:'System.Xml.XmlException‘in System.Xml.dll”,附加的消息是“无效的XML document。文档没有根元素”。我该如何解决这个问题?

我添加了很多控制台输出,以显示每个阶段都按预期工作。我的函数如下:

代码语言:javascript
复制
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");}

        }

控制台相关信息如下:

代码语言:javascript
复制
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

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

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

EN

回答 1

Stack Overflow用户

发布于 2019-01-24 17:45:35

根据the docsImportNode只复制指定的节点并返回它,实际上并不将其插入到文档中。您可以在给出的示例中看到需要添加的节点。

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

代码语言:javascript
复制
var outputQIFFile = new XmlDocument();

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

outputQIFFile.AppendChild(newQifNode);

outputQIFFile.Save(QIFSaveLocation);

然而,正如评论中所建议的,我强烈建议你看看LINQ to XML (XDocument和朋友)。这是一个更加现代和用户友好的API。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54328707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档