Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XDocument错误非空白_C#_.net_Xml_Linq_Linq To Xml - Fatal编程技术网

C# XDocument错误非空白

C# XDocument错误非空白,c#,.net,xml,linq,linq-to-xml,C#,.net,Xml,Linq,Linq To Xml,我在尝试构建XDocument时出错。错误发生在代码下的System.XML.Linq.Xdocument内部 internal override void ValidateString(string s) { if (!IsWhitespace(s)) throw new ArgumentException(Res.GetString(Res.Argument_AddNonWhitespace)); } 此代码正在生成空引用异常。下面是我的XDocument代码

我在尝试构建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();
编辑
更新代码

您在根级别创建了多个元素。假设
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
中。