2013-03-07 45 views
8

Tôi có một đối tượng xmlwriter được sử dụng trong một phương thức. Tôi muốn kết xuất tập tin này để đọc nó. Có cách nào đơn giản để làm việc này không?Viết xmlwriter vào tập tin

Cảm ơn

+0

nếu bạn đã có một thể hiện của 'XmlWriter', không phải là nó đã có một' Stream' ('MemoryStream',' Fi leStream', v.v.) để ghi vào? – publicgk

Trả lời

10

Sử dụng mã này

 // Create the XmlDocument. 
     XmlDocument doc = new XmlDocument(); 
     doc.LoadXml("<item><name>wrench</name></item>"); 

     // Add a price element. 
     XmlElement newElem = doc.CreateElement("price"); 
     newElem.InnerText = "10.95"; 
     doc.DocumentElement.AppendChild(newElem); 

     // Save the document to a file and auto-indent the output. 
     XmlTextWriter writer = new XmlTextWriter(@"C:\data.xml", null); 
     writer.Formatting = Formatting.Indented; 
     doc.Save(writer); 

Như tìm thấy trên MSDN: http://msdn.microsoft.com/en-us/library/z2w98a50.aspx

+0

+1 Không biết về 'Định dạng. Hiện tại' – Brad

3

Một khả năng là để thiết lập các XmlWriter để xuất ra một tập tin văn bản:

using (var writer = XmlWriter.Create("dump.xml")) 
{ 
    ... 
} 
Các vấn đề liên quan