2011-02-08 16 views
9

Devo creare un documento XML in C#.Come aggiungere uno spazio dei nomi durante la creazione di un file XML?

elemento

La radice deve assomigliare a questo:

<valuation-request 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="valuations.xsd"> 

Sto utilizzando il seguente

XmlElement root = X.CreateElement("valuation-request"); 
root.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); 
root.SetAttribute("xsi:noNamespaceSchemaLocation", "valuations.xsd"); 

Tuttavia questo produce

<valuation-request 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    noNamespaceSchemaLocation="valuations.xsd"> //missing the xsi: 

Che cosa mi manca?

risposta

8

Utilizzare il sovraccarico di SetAttribute, che prende spazio dei nomi così:

root.SetAttribute("noNamespaceSchemaLocation", 
    "http://www.w3.org/2001/XMLSchema-instance", 
    "valuations.xsd" 
); 
+0

sto usando root.SetAttribute ("xsi: noNamespaceSchemaLocation", "valuations.xsd") ;. come suggeriresti che dovrebbe apparire? – Steven

+1

provare questo root.SetAttribute ("noNamespaceSchemaLocation", "valuations.xsd", "http://www.w3.org/2001/XMLSchema-instance"); –

+0

Che restituisce Steven

0

con lo scrittore si aggiunge in questo modo:

var writerSettings = new XmlWriterSettings 
     { 
      Indent = true, 
      IndentChars = " ", 
      NewLineChars = Environment.NewLine, 
      NewLineHandling = NewLineHandling.Replace, 
      Encoding = new UTF8Encoding(false) 
     }; 

XmlWriter writer = XmlWriter.Create("C:\test.xml", writerSettings); 
writer.WriteStartDocument(false); 
writer.WriteStartElement("valuation-request"); 
writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); 
writer.WriteAttributeString("xsi", "noNamespaceSchemaLocation", null, "http://www.gzs.si/e-poslovanje/sheme/eSLOG_1-5_EnostavniRacun.xsd"); 
1

Recentemente, ho incontrato lo stesso problema. Per risolverlo, ho appena aggiungere la riga seguente:

XmlAttribute noNamespaceSchemaLocationAttr = xmlDoc.CreateAttribute("xsi", "noNamespaceSchemaLocation", "http://www.w3.org/2001/XMLSchema-instance"); 
+0

che funziona bene sul mio codice. Per me era importante la parte relativa all'istanza XMLSchema –

Problemi correlati