2012-05-30 78 views
1

的常用方法,我的代碼寫一個XML文件,編寫使用C#中的XML文件到一個特定的XML結構

XmlWriterSettings settings = new XmlWriterSettings(); 
    settings.Indent = true; 
    XmlWriter writer = XmlWriter.Create("Products.xml", settings); 

    writer.WriteStartDocument(); 

    writer.WriteComment("This file is generated by the program."); 

    writer.WriteStartElement("Product"); 
    writer.WriteAttributeString("ID", "001"); 
    writer.WriteAttributeString("Name", "Keyboard"); 
    writer.WriteElementString("Price", "10.00"); 
    writer.WriteStartElement("OtherDetails"); 
    writer.WriteElementString("BrandName", "X Keyboard"); 
    writer.WriteElementString("Manufacturer", "X Company"); 
    writer.WriteEndElement(); 
    writer.WriteEndDocument(); 
    writer.Flush(); 
    writer.Close(); 

但上面的代碼給了我不同的XML結構,如何編寫代碼,如果我需要輸出爲如下結構,

<Books> 
<Book ISBN="0553212419"> 
<title>Sherlock Holmes</title> 
<author>Sir Arthur Conan Doyle</author> 
</Book> 
<Book ISBN="0743273567"> 
<title>The Great Gatsby</title> 
<author>F. Scott Fitzgerald</author> 
</Book> 
<Book ISBN="0684826976"> 
<title>Undaunted Courage</title> 
<author>Stephen E. Ambrose</author> 
</Book> 
<Book ISBN="0743203178"> 
<title>Nothing Like It In the World</title> 
<author>Stephen E. Ambrose</author> 
</Book> 
</Books> 

感謝

+6

只需更改標記名稱,屬性名稱,值等以反映給定的XML? – BoltClock

+0

您是否嘗試更改任何值以查看會發生什麼? – random

回答

9

正如scommented,只需修改你已經寫正確的元素的代碼。

XmlWriter writer = XmlWriter.Create(@"Products.xml", settings); 

writer.WriteStartDocument(); 

writer.WriteComment("This file is generated by the program."); 

writer.WriteStartElement("Books"); 
writer.WriteStartElement("Book"); 
writer.WriteAttributeString("ISBN", "0553212419"); 
writer.WriteElementString("Title", "Sherlock Holmes"); 
writer.WriteElementString("Author", "Sir Arhur Conan Doyle"); 
writer.WriteEndElement(); 
writer.WriteEndElement(); 
writer.WriteEndDocument(); 
writer.Flush(); 
writer.Close(); 

清洗,沖洗,重複。我會建議編寫一個方法來添加每本書。

編輯 - 法寫書

void WriteBookData(XmlWriter writer, string isbn, string title, string author) 
{ 
    writer.WriteStartElement("Book"); 
    writer.WriteAttributeString("ISBN", isbn); 
    writer.WriteElementString("Title", title); 
    writer.WriteElementString("Author", author); 
    writer.WriteEndElement(); 
} 

XmlWriterSettings settings = new XmlWriterSettings(); 
settings.Indent = true; 
using (XmlWriter writer = XmlWriter.Create(@"Products.xml", settings)) 
{ 
    writer.WriteStartDocument(); 

    writer.WriteComment("This file is generated by the program."); 

    writer.WriteStartElement("Books"); 
    WriteBookData(writer, "0553212419", "Sherlock Holmes", "Sir Arhur Conan Doyle"); 
    writer.WriteEndElement(); 
    writer.WriteEndDocument(); 
    writer.Flush(); 
} 
2

你爲什麼不使用LINQ到XML。其非常簡單,非常容易

已瞭解
string filename = string.Empty; 
XElement res= new XElement ("Books",new XElement ("Book",new XAttribute ("ISBN","="+055321212), 
      new XElement ("Title","Sherlock Homes"), 
      new XElement ("author","Sir Arthur Conan") 
      ) 
      ); 


     res.Save(filename); 

可以使用的foreach看看將硬編碼值存儲在上述語句中