Dot-Net
如何在 XElement 上設置命名空間屬性
我需要將以下屬性添加到 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 不起作用,而且我確信這不是正確的方法。我如何在那裡添加這些?
它花了很多部落格,但我終於想出了我認為是“正確”的方法來做到這一點:
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") );