2014-03-25 91 views
1

嘗試構建XDocument時出現錯誤。該錯誤代碼XDocument錯誤非空白

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

該代碼生成一個空引用異常下內部System.XML.Linq.Xdocument發生。下面是我的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(); 

編輯 更新代碼

回答

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在無任何錯誤。

+0

解決了這個錯誤,但是現在我在使用XMLTextWriter和調用WriteEndDocument時發現了另一個錯誤,該WriteEndDocument表示我沒有根級別元素。 – mlw4428

+0

你可以在問題結尾處發佈那段代碼,我會嘗試重現 –

+0

我已更新原始帖子中的代碼。 – mlw4428