3

我正在尝试向 ClaimTypesOffered 元素添加更多声明,如下所示:

<fed:ClaimTypesOffered>
  <auth:ClaimType Uri="http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name" Optional="true" xmlns:auth="http://docs.oasis-open.org/wsfed/authorization/200706">
    <auth:DisplayName>Name</auth:DisplayName>
    <auth:Description>The name of the subject.</auth:Description>
  </auth:ClaimType>
</fed:ClaimTypesOffered>

那里有很多命名空间的魔法,我正在努力解决它。仅仅获得正确的元素名称是很困难的。我已经尝试了以下所有方法:

new XElement(XNamespace.Get("auth") + "ClaimType", "somedata");

<ClaimType xmlns="auth">somedata</ClaimType>

new XElement(XName.Get("{http://docs.oasis-open.org/wsfed/authorization/200706}auth"), "somedata");

<auth xmlns="http://docs.oasis-open.org/wsfed/authorization/200706">somedata</auth>

new XElement("auth:ClaimType", "somedata");

System.Xml.XmlException : The ':' character, hexadecimal value 0x3A, cannot be included in a name.

我正在寻求帮助以进一步解决这个问题,生成包含属性和内部元素的声明的完整示例将非常棒,即使朝着正确的方向进行小幅推动也将不胜感激。

4

1 回答 1

0

关键是在父元素上定义命名空间,然后子元素可以使用该命名空间。如果没有新的 XAttribute(XNamespace.Xmlns + "auth", auth.NamespaceName),则命名空间以原始问题中所见的其他方式定义。

        XNamespace fed = "http://docs.oasis-open.org/wsfed/federation/200706";
        XNamespace auth = "http://docs.oasis-open.org/wsfed/authorization/200706";

        XElement root = new XElement(auth + "ClaimType",
            new XAttribute(XNamespace.Xmlns + "auth", auth.NamespaceName),
            new XAttribute("Uri", "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name"),
            new XAttribute("Optional", "true"),
            new XElement(auth+"DisplayName", "EmployeeID"),
            new XElement(auth+"Description", "The employee's designated ID number.")
        );

        Console.WriteLine(root);
于 2012-09-03T20:06:11.647 回答