2013-08-07 67 views
1

好吧,我有一種方法應該讀取一個XML文檔,然後將它們全部包裝在一個更大的XML文檔中。XmlWriter.WriteStartDocument()不向文件輸出聲明

<Elements> 
    <Element> 
    <Element> 
</Elements> 

的問題是,使用該代碼的輸出文件時,下方缺少聲明

<?xml > 

任何理由這是爲什麼?我希望這是我失蹤的愚蠢。

XmlWriter xmlWriter = XmlWriter.Create(outputFileName, xwsSettings); 

xmlWriter.WriteStartDocument(); 

xmlWriter.WriteStartElement("Elements"); 

foreach (var item in items) 
{ 
    xmlReader = XmlReader.Create(item.FullFilename); 

    while (xmlReader.Read()) 
    { 
     if(xmlReader.NodeType == XmlNodeType.Element) 
     { 
      xmlWriter.WriteRaw(xmlReader.ReadOuterXml()); 
      break; 
    } 
    } 
} 

xmlWriter.WriteEndElement(); 
xmlWriter.WriteEndDocument(); 

xmlWriter.Flush(); 
xmlWriter.Close(); 
xmlWriter = null; 
+1

什麼是'xwsSettings'的設置? – Tim

+0

是的,謝謝@Tim,我是一個完整的白癡...... xwsSettings.OmitXmlDeclaration = true; 我以前用ConformanceLevel.Fragment這就是爲什麼我想我已經省略了聲明處理... 什麼白癡 –

+0

嘗試'XmlWriter.Create(SW,新XmlWriterSettings {OmitXmlDeclaration =真});' – Nilesh

回答

3

由於@Tim曾在突出顯示要檢查我有我的XmlWriterSettings正確設置

XmlWriterSettings xwsSettings = new XmlWriterSettings(); 
xwsSettings.CheckCharacters = false; 
xwsSettings.CloseOutput = true; 
xwsSettings.ConformanceLevel = ConformanceLevel.Document; 
xwsSettings.Encoding = Encoding.UTF8; 
xwsSettings.Indent = false; 
xwsSettings.NewLineHandling = NewLineHandling.None; 

xwsSettings.OmitXmlDeclaration = true; <---- should have been false;