【问题标题】:XDocument Error NonwhitespaceXDocument 错误非空白
【发布时间】:2014-03-25 17:05:29
【问题描述】:

我在尝试构建 XDocument 时遇到错误。错误发生在代码下的 System.XML.Linq.Xdocument 内

 internal override void ValidateString(string s) {
        if (!IsWhitespace(s)) throw new ArgumentException(Res.GetString(Res.Argument_AddNonWhitespace)); 
    } 

此代码正在生成空引用异常。下面是我的 XDocument 代码,我不知道我正在做的事情是在哪里造成的。

            XDocument folderviewContents = new XDocument(
                new XDeclaration("1.0", "utf8", "yes"),
                new XElement("LVNPImport",
                    new XAttribute("xmlns" + "xsd", XNamespace.Get("http://www.w3.org/2001/XMLSchema")),
                    new XAttribute("xmlns" + "xsi", XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance"))),
                new XElement("InterfaceIdentifier", "835"),
                //Start of FolderPaths 
                new XElement("FolderPaths",
                    new XElement("Folder",
                        new XAttribute("fromDate", "TEST"),
                        //attributes for Folder w/ lots of attributes
                        new XAttribute("toDate", "TEST"),
                        new XAttribute("contactName", "APerson"),
                        new XAttribute("email", "AnEmail"),
                        //value for that long Folder w/ lots of attributes
                        "Remittance Advice"),
                    //Facility
                    new XElement("Folder", "TEST"),
                    //PayorID
                    new XElement("Folder", "TEST"),
                    //RemitDate Year
                    new XElement("Folder","TEST"),
                    //RemitDate Month/Year
                    new XElement("Folder","TEST")),
                new XElement("DocumentType", "RA"),
                new XElement("DocumentDescription","TEST"),
                new XElement("TotalFiles", "1"));

             //Create a writer to write XML to the console.
        XmlTextWriter writer = null;
        writer = new XmlTextWriter(Console.Out);
        //Use indentation for readability.
        writer.Formatting = Formatting.Indented;
        writer.Indentation = 4;

        folderviewContents.WriteTo(writer);
        writer.WriteEndDocument();
        writer.Close();
        Console.ReadLine();

编辑 更新代码

【问题讨论】:

    标签: c# .net xml linq linq-to-xml


    【解决方案1】:

    您在根级别创建了多个元素。假设 LVNPImport 是您的根节点,只需移动一个右括号即可解决此问题:

            XDocument folderviewContents = new XDocument(
                new XDeclaration("1.0", "utf8", "yes"),
                new XElement("LVNPImport",
                    new XAttribute("xmlns" + "xsd", XNamespace.Get("http://www.w3.org/2001/XMLSchema")),
                    new XAttribute("xmlns" + "xsi", XNamespace.Get("http://www.w3.org/2001/XMLSchema-instance")),
                new XElement("InterfaceIdentifier", "835"),
                //Start of FolderPaths 
                new XElement("FolderPaths",
                    new XElement("Folder",
                        new XAttribute("fromDate", "TEST"),
                //attributes for Folder w/ lots of attributes
                        new XAttribute("toDate", "TEST"),
                        new XAttribute("contactName", "APerson"),
                        new XAttribute("email", "AnEmail"),
                //value for that long Folder w/ lots of attributes
                        "Remittance Advice"),
                //Facility
                    new XElement("Folder", "TEST"),
                //PayorID
                    new XElement("Folder", "TEST"),
                //RemitDate Year
                    new XElement("Folder", "TEST"),
                //RemitDate Month/Year
                    new XElement("Folder", "TEST")),
                new XElement("DocumentType", "RA"),
                new XElement("DocumentDescription", "TEST"),
                new XElement("TotalFiles", "1")));
    

    我在本地对此进行了测试,XDocument 的创建没有错误。

    【讨论】:

    • 解决了这个错误,但现在我在使用 XMLTextWriter 并调用 WriteEndDocument 时遇到另一个错误,它说我没有根级元素。
    • 你能否在问题的最后贴出那段代码,我会尝试重现
    • 删除 writer.WriteEndDocument() 行 - 不需要,因为这全部在您写入流中的 XDocument 中。
    猜你喜欢
    • 1970-01-01
    • 2021-12-07
    • 1970-01-01
    • 2016-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多