2012-05-16 91 views
0

我要創建一個XML文件,它包含的元素有類似的屬性:生成XML屬性

<element 
    xsi:schemaLocation="http://test.xsd" 
    xmlns="http://test2" 
    xmlns:xsi=http://test3> 

我想:

XNamespace ns = "xsi";    
var root = new XElement("element", 
         new XAttribute(ns + "schemaLocation", "http://test.xsd"), // (I) 
         new XAttribute(XNamespace.Xmlns, "http://test2"),   // (II) 
         new XAttribute(XNamespace.Xmlns + "xsi", "http://test3"), // (III) 

但這種情況正在發生細微的唯一的事情是(III ):

xmlns:xsi=http://test3 

(Ⅰ)等產生的:

p1:schemaLocation="http://test.xsd" xmlns:p1="xsi" 

和(II)不生成,因爲該行不編譯。

關於如何生成這些屬性的任何想法?

謝謝 大號

編輯 - 也發現在這裏:Creating XML with namespaces and schemas from an XElement

回答

0
const string ns = "http://test2"; 
const string si = "http://test3"; 
const string schema_location = "http://test.xsd"; 

XNamespace xns = ns; 
XNamespace sinsp = si; 

    XElement xe = new XElement(xns + "element", 
      new XAttribute(XNamespace.Xmlns + "xsi", si), 
      new XAttribute(sinsp+ "schemaLocation", schema_location), 
      new XElement(xns + "sometag", "somecontent") 
     ); 

    return xe;