我正在尝试使用C#创建一个xml文件,我不断收到此错误:
{"此操作会创建错误的结构化文档。"}
我正在按照我在网上找到的指南,但我不确定我做错了什么。
我关注的指南位于:http://luka.manojlovic.net/wp-content/uploads/2012/04/05-Code.png
这是我的代码:
var xml = new XDocument(new XElement("Data"),
new XElement("Parameter",
new XElement("ID", parameters[0].Id.ToString("B")), new XElement("Value", runbookId)), //RunbookID
new XElement("Parameter",
new XElement("ID", parameters[1].Id.ToString("B")), new XElement("Value", employee.JobTitle)), //Title
new XElement("Parameter",
new XElement("ID", parameters[2].Id.ToString("B")), new XElement("Value", employee.UserName)),//Username
new XElement("Parameter",
new XElement("ID", parameters[3].Id.ToString("B")), new XElement("Value", employee.LastName)), //Lastname
new XElement("Parameter",
new XElement("ID", parameters[4].Id.ToString("B")), new XElement("Value", GetManager(employee))), //Manager
new XElement("Parameter",
new XElement("ID", parameters[5].Id.ToString("B")), new XElement("Value", "")), //Phone
new XElement("Parameter",
new XElement("ID", parameters[6].Id.ToString("B")), new XElement("Value", employee.Initials)),//GUI Initials
new XElement("Parameter",
new XElement("ID", parameters[7].Id.ToString("B")), new XElement("Value", employee.EmployeeNumber)), //Employee #
new XElement("Parameter",
new XElement("ID", parameters[8].Id.ToString("B")), new XElement("Value", GetCompany(employee))), //Company
new XElement("Parameter",
new XElement("ID", parameters[9].Id.ToString("B")), new XElement("Value", "")), //Employee Security
new XElement("Parameter",
new XElement("ID", parameters[10].Id.ToString("B")), new XElement("Value", GetDepartment(employee))), //Department
new XElement("Parameter",
new XElement("ID", parameters[11].Id.ToString("B")), new XElement("Value", employee.Location)), //Office
new XElement("Parameter",
new XElement("ID", parameters[12].Id.ToString("B")), new XElement("Value", employee.FirstName)) //First Name
);
答案 0 :(得分:2)
您正在尝试使用多个root elements创建XDocument
。你不能这样做,因为XML标准不允许这样做。您可能打算将"Parameter"
元素设为Data
的孩子:
var xml = new XDocument(new XElement("Data",
new XElement("Parameter",
new XElement("ID", parameters[0].Id.ToString("B")), new XElement("Value", runbookId)), //RunbookID
new XElement("Parameter",
new XElement("ID", parameters[1].Id.ToString("B")), new XElement("Value", employee.JobTitle)), //Title
new XElement("Parameter",
new XElement("ID", parameters[2].Id.ToString("B")), new XElement("Value", employee.UserName)),//Username
new XElement("Parameter",
new XElement("ID", parameters[3].Id.ToString("B")), new XElement("Value", employee.LastName)), //Lastname
new XElement("Parameter",
new XElement("ID", parameters[4].Id.ToString("B")), new XElement("Value", GetManager(employee))), //Manager
new XElement("Parameter",
new XElement("ID", parameters[5].Id.ToString("B")), new XElement("Value", "")), //Phone
new XElement("Parameter",
new XElement("ID", parameters[6].Id.ToString("B")), new XElement("Value", employee.Initials)),//GUI Initials
new XElement("Parameter",
new XElement("ID", parameters[7].Id.ToString("B")), new XElement("Value", employee.EmployeeNumber)), //Employee #
new XElement("Parameter",
new XElement("ID", parameters[8].Id.ToString("B")), new XElement("Value", GetCompany(employee))), //Company
new XElement("Parameter",
new XElement("ID", parameters[9].Id.ToString("B")), new XElement("Value", "")), //Employee Security
new XElement("Parameter",
new XElement("ID", parameters[10].Id.ToString("B")), new XElement("Value", GetDepartment(employee))), //Department
new XElement("Parameter",
new XElement("ID", parameters[11].Id.ToString("B")), new XElement("Value", employee.Location)), //Office
new XElement("Parameter",
new XElement("ID", parameters[12].Id.ToString("B")), new XElement("Value", employee.FirstName)) //First Name
)
);