我在尝试构建XDocument时遇到错误。错误发生在代码
下的System.XML.Linq.Xdocument中 internal override void ValidateString(string s) {
if (!IsWhitespace(s)) throw new ArgumentException(Res.GetString(Res.Argument_AddNonWhitespace));
}
此代码生成Null引用异常。下面是我的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();
修改的 更新的代码
答案 0 :(得分: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
创建时没有错误。