2012-09-03 173 views
3

我想更多的要求添加到ClaimTypesOffered元素,如下图所示:生成XML命名空间

<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. 

我寻求帮助,相处这进一步,生成包括属性和内在要素的要求的完整的例子将是真棒,即使是很小的推在正确的方向将不胜感激。

+0

尝试在[所以]为['[XML名称空间] [LINQ到XML]'](http://stackoverflow.com/questions/tagged/xml-namespaces+linq-to搜索-xml) –

+0

http://stackoverflow.com/questions/6747304/in-c-is-there-a-way-to-generate-an-xdocument-using-the-short-prefix-instead-of –

+0

@JohnSaunders发布这意味着有类似于我的问题的现有答案。我搜索了现有的答案,发现与名称空间生成XML很少有关系。对我来说,这个回应基本上等于“google it”。 – jjr2527

回答

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);