2017-08-08 45 views
1

我想將以下標籤寫入XML文件。C#XmlWriter WriteStartAttribute

<StructureMetaData xsi:schemaLocation="MSD" xmlns="MSD" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <!-- here a following more tags --> 
</StructureMetaData> 

這些我下面

XmlWriterSettings settings = new XmlWriterSettings 
{ 
    Indent = true, 
    IndentChars = "\t" 
    CheckCharacters = false, 
}; 

XmlWriter xmlWriter = XmlWriter.Create (filename, settings); 

xmlWriter.WriteStartDocument(); 
xmlWriter.WriteStartAttribute ("xsi", "schemaLocation", "MSD"); 
xmlWriter.WriteStartAttribute ("xmlns", "MSD"); 
xmlWriter.WriteStartAttribute ("xmlns", "xsi", "http://www.w3.org/2001/XMLSchema-instance"); 

但WriteStartAttribute的第一個電話後,我得到了以下異常:

「令牌StartAttribute狀態文件將導致無效的XML文檔「。

如何將這些屬性寫入xml標籤?

回答

0

你將不得不編寫一個啓動元素上的屬性寫入:

xmlWriter.WritestartElement("StructureMetaData"); 
xmlWriter.WriteStartAttribute(...); 
0

它更容易使用XML LINQ:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Xml; 
using System.Xml.Linq; 

namespace ConsoleApplication72 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string root = "<StructureMetaData xsi:schemaLocation=\"MSD\" xmlns=\"MSD\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"></StructureMetaData>"; 

      XDocument doc = XDocument.Parse(root); 
      XElement metaData = doc.Root; 
      XNamespace ns = metaData.GetDefaultNamespace(); 

      metaData.Add(new XElement(ns + "ABC", new object[] { 
       new XElement(ns + "DEF", new object[] { 
        new XAttribute("rst","abc"), 
        123}), 
       new XElement(ns + "GHI", new object[] { 
        new XAttribute("uvw","def"),      
        456}), 
       new XElement(ns + "JKL", new object[] { 
        new XAttribute("xyz","ghi"), 
        789}) 
      })); 

      doc.Save("filename"); 
     } 
    } 
} 
+0

謝謝,它的工作原理。但與子項我有一個XML問題。我得到以下內容: ABC 123 456 789 我將讀取如何產生與LINQ的XML文件。每個XElement都有一個xmlns屬性。 – Holgis

+0

我更新了代碼。 – jdweng

0

你有幾個問題:

  1. 您需要編寫root element ta g <StructureMetaData第一個,然後開始編寫它的任何屬性。有一個默認名稱空間xmlns="MSD",因此您需要將其寫入該名稱空間。

  2. 您正在調用XmlWriter.WriteStartAttribute(),但此方法只寫入屬性名稱。您仍然必須使用例如XmlWriter.WriteString()在名稱後寫。或者,您可以使用XmlWriter.WriteAttributeString()的一個電話號碼來寫入名稱和值。

  3. 您應該通過using聲明關閉並處理您的xmlWriter

下面是使用WriteStartAttribute()你的代碼的工作版本:

using (var xmlWriter = XmlWriter.Create(filename, settings)) 
{ 
    xmlWriter.WriteStartDocument(); 

    var rootNamespace = "MSD"; 

    // Write the root element in the MSD namespace 
    xmlWriter.WriteStartElement("StructureMetaData", rootNamespace); 

    // Write the root element's attribute names and values 
    xmlWriter.WriteStartAttribute("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance"); 
    xmlWriter.WriteString("MSD"); 
    xmlWriter.WriteStartAttribute("xmlns"); 
    xmlWriter.WriteString(rootNamespace); 
    xmlWriter.WriteStartAttribute("xmlns", "xsi", null); 
    xmlWriter.WriteString("http://www.w3.org/2001/XMLSchema-instance"); 

    // Write the root element contents 

    // Close the root element 
    xmlWriter.WriteEndElement(); 

    // Close the document 
    xmlWriter.WriteEndDocument(); 
} 

而且使用WriteAttributeString()一個簡單的版本:

using (var xmlWriter = XmlWriter.Create(filename, settings)) 
{ 
    xmlWriter.WriteStartDocument(); 

    var rootNamespace = "MSD"; 

    // Write the root element in the MSD namespace 
    xmlWriter.WriteStartElement("StructureMetaData", rootNamespace); 

    // Write the root element's attribute names and values 
    xmlWriter.WriteAttributeString("xsi", "schemaLocation", "http://www.w3.org/2001/XMLSchema-instance", "MSD"); 
    xmlWriter.WriteAttributeString("xmlns", rootNamespace); 
    xmlWriter.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance"); 

    // Write the root element contents 

    // Close the root element 
    xmlWriter.WriteEndElement(); 

    // Close the document 
    xmlWriter.WriteEndDocument(); 
} 

樣品.Net fiddle顯示這兩個選項。

+0

謝謝,現在它工作。我通過WriteAttributeString – Holgis

+0

@Holgis發現了兩個以上參數的問題 - 歡迎您。如果問題得到解答,請做[標記爲](https://meta.stackexchange.com/q/147531)。 – dbc