.net – 如何在XElement上设置命名空间属性
发布时间:2020-12-16 07:42:23 所属栏目:百科 来源:网络整理
导读:我需要将以下属性添加到XElement中: xmlns="http://www.mysite.com/myresource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mysite.com/myresource TheResource.xsd" 将它们添加为XAttribute不起作用,因为“:
我需要将以下属性添加到XElement中:
<xmlns="http://www.mysite.com/myresource" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.mysite.com/myresource TheResource.xsd"> 将它们添加为XAttribute不起作用,因为“:”,我确定不是正确的方式.如何在这里添加这些?
它花了
a lot of blogs,但我终于想出了我认为是这样做的“正确”方式:
XNamespace ns = @"http://www.myapp.com/resource"; XNamespace xsi = @"http://www.w3.org/2001/XMLSchema-instance"; var root = new XElement(ns + "root",new XAttribute(XNamespace.Xmlns+"xsi",xsi.NamespaceName),new XAttribute(xsi + "schemaLocation",@"http://www.myapp/resource TheResource.xsd") ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |