1

我正在尝试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."

希望我遗漏了一些明显的东西,但我似乎无法发现问题,而且谷歌没有提供任何关于这个错误的有用信息。

4

1 回答 1

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
    )
);
于 2013-05-22T15:02:53.030 回答