2016-08-15 110 views
0

我有結構性這樣如何使用LINQ到XML來覆蓋現有的XML文件

<?xml version="1.0" encoding="utf-8" ?> 
<event> 
    <name>Test Event</name> 
    <date>06/09/1990</date> 
    <description>Birthday</description> 
    <blogURL></blogURL> 
</event> 

我想實現是有一個表單接受用戶輸入來創建一個新的XML文檔一個簡單的XML文件具有相同的結構將覆蓋舊的結構。

XML文檔與將接受輸入的ASPX頁面位於同一目錄中。 Press Release

當用戶導航到PressRelease.aspx頁面時,它將加載文檔中的值。

var doc = XDocument.Load(Server.MapPath("~/PressSection.xml")); 
    string currentEventName = (string)doc.Descendants("name").Single(); 
    string currentEventDate = (string)doc.Descendants("date").Single(); 
    string currentEventDescription = (string)doc.Descendants("description").Single(); 
    string currentEventLink = (string)doc.Descendants("blogURL").Single(); 
    if (currentEventLink.ToString() == "") 
    { 
     CurrentEventURL.Text = "This event has no URL"; 
    } 

    CurrentEventName.Text = currentEventName; 
    CurrentEventDescription.Text = currentEventDescription; 
    CurrentEventDate.Text = currentEventDate; 

This works。我抓住了用戶的輸入,並做簡單的驗證這樣

string newEventName = NewEventName.Text; 
    string newEventDescription = NewDescription.Text; 
    string newEventDate = NewDate.SelectedDate.Value.Date.ToString(); 
    string newEventURL = NewURL.Text; 

    if (newEventName == "") 
    { 
     MessageBox.Text = "Please enter a valid event name"; 
     MessageBox.CssClass = "event_error"; 
     return; 
    } 
    if (newEventDescription == "") 
    { 
     MessageBox.Text = "Please enter a valid description"; 
     MessageBox.CssClass = "event_error"; 
     return; 
    } 
    if (newEventDate == null) 
    { 
     MessageBox.Text = "Please select a valid date"; 
     MessageBox.CssClass = "event_error"; 
     return; 
    } 

最後,我建立並保存新的XML文檔類似這樣的

 //Create new document 
     XDocument newEventDocument = new XDocument(new XDeclaration("1.0", "utf-8", null)); 

     //Addd root node 
     XElement RootNode = new XElement("Event"); 

     //add root node to new document 
     newEventDocument.Add(RootNode); 

     //Add event name element then add it to the new document 
     XElement eName; 
     eName = new XElement("name"); 
     eName.Value = newEventName; 
     newEventDocument.Root.Add(eName); 

     //Add event date element then add it to the new document 
     XElement eDate; 
     eDate = new XElement("date"); 
     eDate.Value = newEventDate; 
     newEventDocument.Root.Add(eDate); 

     //Add event description element then add it to the new document 
     XElement eDescription; 
     eDescription = new XElement("description"); 
     eDescription.Value = newEventDescription; 
     newEventDocument.Root.Add(eDescription); 

     //Add event URL element then add it to the new document 
     XElement eURL; 
     eURL = new XElement("blogURL"); 
     eURL.Value = newEventURL; 
     newEventDocument.Root.Add(eURL); 

     //Finally, save the document 
     newEventDocument.Save("PressSection.xml", SaveOptions.None); 

我的程序拋出是無效的權限問題的問題。

Access to the path 'C:\Program Files (x86)\IIS Express\PressSection.xml' is denied. 

我的問題是,我該如何將它保存在我的項目目錄的根目錄中,而不是在我的IIS Express文件夾中?我需要它來覆蓋我的項目中已經存在的現有'PressSection.xml'文件。謝謝!

+0

一個webapp,可以寫入自己的webroot?聽起來......有風險。 – spender

+0

我想過用數據庫做這件事,但這似乎是一個更便宜的選擇。如果你有建議,那麼我對他們開放。我想盡可能地學習。 – onTheInternet

回答

0

這工作。對不起,這樣一個簡單的問題

 string path = Server.MapPath("PressSection.xml"); 
     newEventDocument.Save(Path.Combine(path));