【问题标题】:What is causing this LINQ to XML Argument Exception?是什么导致此 LINQ to XML 参数异常?
【发布时间】:2013-05-22 14:54:54
【问题描述】:

我正在尝试使用最终将由数据库提供的数据来初始化 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 没有提供任何有关此错误的有用信息。

【问题讨论】:

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


    【解决方案1】:

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

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

    无效 XML sn-p 中的“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
        )
    );
    

    【讨论】:

    • 谢谢你,成功了!我认为这是我忽略的一些简单的事情。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-06-02
    相关资源
    最近更新 更多