导致此LINQ到XML参数异常的原因是什么?

时间:2013-05-22 14:54:54

标签: c# xml linq c#-4.0 linq-to-xml

我正在尝试使用最终由数据库提供的数据初始化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.

希望我遗漏了一些显而易见的东西,但我似乎无法发现问题,Google也没有提供有关此错误的任何有用信息。

1 个答案:

答案 0 :(得分:1)

我认为这是因为您获得的代码基本上是尝试将文本直接添加到XML文档中。如果您被允许运行该代码,则最终会得到这个无效的XML:

RatingOverview 
<RatingRequest>
    ...
</RatingRequest>
...

该无效XML片段中的“RatingOverview”文本是异常抱怨的“非空格字符”。

你真正想要的是:

<RatingOverview>
    <RatingRequest>
        ...
    </RatingRequest>
    ...
</RatingOverview>

要实现这一目标,您无需进行太多改变。只需确保您的“RatingOverview”也是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
    )
);