2017-07-15 46 views
0

我想刪除XML元素<Contact>其中ID與lstBox選定索引相匹配。代碼運行,但是,它實際上刪除了我的XML文件中的所有內容,所以我留下了一個空的txt文件。我有這樣的代碼:XML在C#中被刪除

private async void btnDeleteContact_Click(object sender, RoutedEventArgs e) 
    { 
     StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Contacts.xml"); 
     XDocument xdoc = XDocument.Load(file.Path); 
     if (lstBox.SelectedIndex != -1) 
     { 
      xdoc.Element("Contacts") 
       .Elements("Contact") 
       .Where(x => (string)x.Attribute("ID") == lstBox.SelectedItem.ToString()).Remove(); 
      lstBox.SelectedIndex = -1; 
      updateXMLFile(xdoc); 
     } 
    } 

這是我的XML文件

<?xml version="1.0" encoding="UTF-8" ?> 
<Contacts> 
    <Contact> 
     <ID>salpea</ID> 
     <FirstName>Sally</FirstName> 
     <LastName>Pearson</LastName> 
     <Mobile>0431529562</Mobile> 
     <Email>[email protected]</Email> 
    </Contact> 
    <Contact> 
     <ID>gresul</ID> 
     <FirstName>Greg</FirstName> 
    <LastName>Sullivan</LastName> 
     <Mobile>0432928381</Mobile> 
     <Email>[email protected]</Email> 
    </Contact> 
    <Contact> 
     <ID>chrmac</ID> 
     <FirstName>Christie</FirstName> 
    <LastName>Mack</LastName> 
     <Mobile>0421231231</Mobile> 
     <Email>[email protected]</Email> 
    </Contact> 
</Contacts> 

列表框選擇是藍色的。

enter image description here

不知道這是否是相關的,但是這是我的整個文件here

感謝有關此事的任何幫助引擎收錄。

+0

你使用的是C#嗎? –

+1

在您的XML文件中,' ...'是* XML元素*而不是* XML屬性*所以'(string)x.Attribute(「ID」)== lstBox.SelectedItem.ToString()'將永遠是假的。事實上,在測試中似乎沒有任何東西被移除。所以我懷疑問題實際上是在'updateXMLFile(xdoc);'中。你可以分享該代碼嗎? – dbc

+1

嘗試刪除ID爲「chrmac」的元素時,演示提琴沒有顯示任何內容:https://dotnetfiddle.net/T6ffsH – dbc

回答

1
private async void btnDeleteContact_Click(object sender, RoutedEventArgs e) 
    { 
     StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Contacts.xml"); 
     XDocument xdoc = XDocument.Load(file.Path); 
     if (lstBox.SelectedIndex != -1) 
     { 
      var id = lstBox.SelectedItem.ToString(); 
      XmlNode node = xdoc.SelectSingleNode(string.Format("/Contacts/Contact[@ID='{0}']", id)); 

      if (node != null) 
      { 
       XmlNode parent = node.ParentNode; 
       parent.RemoveChild(node); 
       updateXMLFile(xdoc); 
      } 
     } 
    } 
+0

嗨,我只是試過這個,但它一直說XDocument不包含值SelectSingleNode。我試圖添加一個引用,但也沒有工作。 – Rick1990

+0

是否https://stackoverflow.com/questions/35089399/selectsinglenode-is-giving-compilation-error-in-dnx-core-5-0有幫助? – mjwills

1

但是,您可以使用xmlNode。我有過類似的需求消失,固定使用xmlNode這樣

XmlTextReader reader = new XmlTextReader(path); 

    XmlDocument doc = new XmlDocument(); 
    XmlNode node = doc.ReadNode(reader); 

    foreach (XmlNode chldNode in node.ChildNodes) 
    { 
     string employeeName = chldNode.Attributes["Name"].Value; 
     if (employeeName == Employee) 
     {      
      //******your code here 
     } 
    } 

虛擬XML

<Root> 
    <Employee Name ="TestName"> 
    <Childs/> 
</Root> 

我從here取出的參考。在你的上下文中,如果匹配,你可以刪除子節點。

希望它可以幫助你。

1

我從你們所有人那裏收到了答案,而這最終導致了工作。

private async void updateXMLFile(XDocument xdoc) 
     { 
      try 
      { 
       //StorageFile file = await installedLocation.CreateFileAsync("Contacts.xml", CreationCollisionOption.ReplaceExisting); 
       StorageFile file = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync("Contacts.xml"); //This line was the replacement for the one above. 
       await FileIO.WriteTextAsync(file, xdoc.ToString()); 
      } 
      catch (Exception ex) 
      { 
       String s = ex.ToString(); 
      } 
     } 

x.Attribute to x.Element

感謝所有那些誰幫助也發生了變化。