2016-07-12 87 views
1

我有一個xml文件。下面是一個簡短的部分:Linq以XML從xml文件中刪除文檔

<?xml version="1.0" encoding="utf-8" standalone="yes"?> 
<documents> 
    <document> 
     <head> 
      <date>01/01/2015</date> 
      <nr>1001</nr> 
     </head> 
     <body> 
      <name>Mark Sweden</name> 
      <cf>AAABBBCCC</cf> 
     </body> 
    </document> 
    <document> 
     <head> 
      <date>01/01/2015</date> 
      <nr>1002</nr> 
     </head> 
     <body> 
      <name>John Car</name> 
      <cf>AAABBBVVV</cf> 
     </body> 
    </document> 
    <document> 
     <head> 
      <date>01/01/2015</date> 
      <nr>1004</nr> 
     </head> 
     <body> 
      <name>Mark Sweden</name> 
      <cf>AAABBBCCC</cf> 
     </body> 
    </document> 
</documents> 

我想刪除,使用LINQ to XML,具有特定的「CF」值的每一個文件。 例如,如果「cf」值是AAABBBCCC,則第一個和第三個文檔將被刪除。

謝謝。

UPDATE

我的問題是得到的文件刪除(LINQ查詢我寫的是錯誤的)。羅伯特麥基指出我正確的方向。 我的解決辦法是:

var Delete = (from x in doc.Descendants(document) 
where x.Descendants("cf").First().Value = "AAABBBCCC" 
select x); 

Delete.Remove(); 
+0

你嘗試什麼樣的代碼和你在哪裏堵塞。請分享您正在嘗試的代碼。 –

+0

這不是一個查詢,這是一個修改,因此,你不能做你所問的,但是,你可能可以使用LINQ返回一個(不同的)結果沒有這些文件,或者你也可以查詢匹配的文件您的標準和循環,並將其從文檔中刪除。 –

回答

0
var [email protected]"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?> 
<documents> 
    <document> 
     <head> 
      <date>01/01/2015</date> 
      <nr>1001</nr> 
     </head> 
     <body> 
      <name>Mark Sweden</name> 
      <cf>AAABBBCCC</cf> 
     </body> 
    </document> 
    <document> 
     <head> 
      <date>01/01/2015</date> 
      <nr>1002</nr> 
     </head> 
     <body> 
      <name>John Car</name> 
      <cf>AAABBBVVV</cf> 
     </body> 
    </document> 
    <document> 
     <head> 
      <date>01/01/2015</date> 
      <nr>1004</nr> 
     </head> 
     <body> 
      <name>Mark Sweden</name> 
      <cf>AAABBBCCC</cf> 
     </body> 
    </document> 
</documents>"; 
var doc = XDocument.Parse(xml); 
foreach(var node in var node in doc.Descendants("document").Where(d=>d.Descendants("cf").First().Value=="AAABBBCCC").ToArray()) 
{ 
    node.Remove(); 
} 
0

使用的XDocument,您可以生成文檔的副本沒有不需要的元素,然後覆蓋原來的文件:

string documentPath; 
string undesiredPattern = "AAABBBCCC"; 
XDocument xdoc = XDocument.Load(documentPath); 
//Or, if your document is already loaded on a string, use XDocument.Parse(string) 
XElement root = xdoc.Root; 

IEnumerable<XElement> desiredElements = 
root. 
Elements(). 
Where(x=>!x.Element("body").Element("cf").Value.Equals(undesiredPattern)); 

XDocument newDoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes")); 
XElement newRoot = new XElement("documents"); 
foreach(XElement xe in desiredElements) 
{ 
    newRoot.Add(xe); 
} 
newDoc.Add(newRoot); 
newDoc.Save(documentPath); 

編輯: 其他方式,更直接(如果您想從文件加載,而不是XDocument.Parse使用XDocument.Load和XDocument.Save事後保存,作爲片上方的):

string document = "your xml"; 
string undesiredPattern = "AAABBBCCC"; 
XDocument xdoc = XDocument.Parse(document); 

xdoc.Root.Elements().Where(x=> 
    x.Element("body"). 
    Element("cf").Value. 
    Equals(undesiredPattern)).Remove(); 
1

試試這個代碼:它應該是幾乎自動explicative,但讓我知道任何疑問

var root = XElement.Load(myPath); 
List<XElement> deleteMe = new List<XElement>(); 
foreach (var item in root.Elements()) 
{ 
    if (item.Element("body").Element("cf").Value.Equals(myCf)) 
    { 
     deleteMe.Add(item); 
    } 
} 
Console.WriteLine("Deleting: " + String.Join(",",deleteMe.Select(x => x.Element("head").Element("nr").Value))); 
Console.ReadKey(); 
foreach (var item in deleteMe) 
{ 
    item.Remove(); 
} 
root.Save(myPath2);