导致LINQ到XML参数异常的原因
本文关键字:异常 参数 LINQ XML 导致 | 更新日期: 2023-09-27 18:14:45
我试图初始化一个XDocument
的数据,最终将由数据库提供。出于测试目的,我对数据进行了硬编码。
WFEvent[] wfs = new WFEvent[] {
new WFEvent {
ID = new XElement("WorkflowEventID", 0),
ParentID = new XElement("ParentID", 0),
Active = new XElement("Active", false),
Status = new XElement("Status", "Complete")
},
new WFEvent {
ID = new XElement("WorkflowEventID", 1),
ParentID = new XElement("ParentID", 0),
Active = new XElement("Active", true),
Status = new XElement("Status", "In Progress")
},
new WFEvent {
ID = new XElement("WorkflowEventID", 2),
ParentID = new XElement("ParentID", 1),
Active = new XElement("Active", false),
Status = new XElement("Status", "Pending")
},
new WFEvent {
ID = new XElement("WorkflowEventID", 3),
ParentID = new XElement("ParentID", 2),
Active = new XElement("Active", false),
Status = new XElement("Status", "Pending")
},
new WFEvent {
ID = new XElement("WorkflowEventID", 4),
ParentID = new XElement("ParentID", 3),
Active = new XElement("Active", false),
Status = new XElement("Status", "Pending")
},
new WFEvent {
ID = new XElement("WorkflowEventID", 5),
ParentID = new XElement("ParentID", 4),
Active = new XElement("Active", false),
Status = new XElement("Status", "Pending")
},
new WFEvent {
ID = new XElement("WorkflowEventID", 6),
ParentID = new XElement("ParentID", 5),
Active = new XElement("Active", false),
Status = new XElement("Status", "Pending")
},
new WFEvent {
ID = new XElement("WorkflowEventID", 7),
ParentID = new XElement("ParentID", 6),
Active = new XElement("Active", false),
Status = new XElement("Status", "Pending")
},
new WFEvent {
ID = new XElement("WorkflowEventID", 8),
ParentID = new XElement("ParentID", 7),
Active = new XElement("Active", false),
Status = new XElement("Status", "Pending")
},
new WFEvent {
ID = new XElement("WorkflowEventID", 9),
ParentID = new XElement("ParentID", 8),
Active = new XElement("Active", false),
Status = new XElement("Status", "Pending")
},
};
XDocument xml =
new XDocument("RatingOverview",
new XElement("RatingRequest",
new XElement("CreateNewRatingRequest", wfs[0].ID, wfs[0].ParentID, wfs[0].Active, wfs[0].Status)
),
new XElement("Assessment",
new XElement("NeedsAssessment", wfs[1].ID, wfs[1].ParentID, wfs[1].Active, wfs[1].Status),
new XElement("GroupConcerns", wfs[2].ID, wfs[2].ParentID, wfs[2].Active, wfs[2].Status),
new XElement("Recomendation", wfs[3].ID, wfs[3].ParentID, wfs[3].Active, wfs[3].Status)
),
new XElement("TechnicalAssistancePlan",
new XElement("VerifyCurrentRating", wfs[4].ID, wfs[4].ParentID, wfs[4].Active, wfs[4].Status),
new XElement("PlanDeveloped", wfs[5].ID, wfs[5].ParentID, wfs[5].Active, wfs[5].Status),
new XElement("ContactLog", wfs[6].ID, wfs[6].ParentID, wfs[6].Active, wfs[6].Status)
),
new XElement("EnvironmentalRatingReport",
new XElement("ERSScores", wfs[7].ID, wfs[7].ParentID, wfs[7].Active, wfs[7].Status),
new XElement("ERSPlanDeveloped", wfs[8].ID, wfs[8].ParentID, wfs[8].Active, wfs[8].Status)
),
new XElement("SubmitRequest",
new XElement("SendToDCC", wfs[9].ID, wfs[9].ParentID, wfs[9].Active, wfs[9].Status)
)
);
当XDocument xml
初始化时,我得到以下异常:
" System.ArgumentException: Non white space characters cannot be added to content.
"
希望我错过了一些明显的东西,但我似乎不能发现问题,谷歌没有产生任何有用的信息关于这个错误。
我认为这是因为您得到的代码基本上是试图直接向XML文档添加文本。如果允许您运行该代码,您将得到以下无效的XML:
RatingOverview
<RatingRequest>
...
</RatingRequest>
...
无效XML片段中的"RatingOverview"文本是异常所抱怨的"非空白字符"。
你真正想要的是:
<RatingOverview>
<RatingRequest>
...
</RatingRequest>
...
</RatingOverview>
要到达那里,你不需要改变太多。只要确保你的"ratinoverview"也是XElement
,而不仅仅是string
。试试这个:
XDocument xml =
new XDocument(
new XElement("RatingOverview", // <== fix here
new XElement("RatingRequest",
new XElement("CreateNewRatingRequest", wfs[0].ID, wfs[0].ParentID, wfs[0].Active, wfs[0].Status)
),
...
) // <== don't forget to add a parenthesis here
)
);