2009-10-29 8 views
5

Mam XDocument i muszę usunąć węzeł i ponownie dodać ten sam węzeł po pewnym manipulowaniu (mój węzeł xelement jest złożony i ma wewnętrzne węzły jako dobrze). Czy ktoś ma dobry sposób, aby to zrobić, ponieważ mój nowy zmanipulowany węzeł jest dodawany na samym końcu xmldocument. Wszelkie urywki kodu byłyby bardzo docenione.Linq do XML - usuń węzeł i dodaj nowy węzeł w tym samym miejscu

Odpowiedz

4

Jeśli dopiero edycji węzeł, a następnie usunąć go, dlaczego w ogóle? Po prostu znajdź w drzewie odnośnik i edytuj go w miejscu.

Jeśli nie jest to opcja dla jakiegoś powodu, to jeden ze sposobów, aby przejść o nim jest taka: kiedy już okazało się, że XElement (lub w ogóle, XNode) trzeba wymienić w drzewie, utworzyć nowy XElement służyć jako zamiennik, a następnie użyć metody XNode.ReplaceWith na starym elemencie, przekazując nową jako argument.

8

Jeśli dobrze cię rozumiem, powinno ci to pomóc.

SolarSystem.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<SolarSystem> 
    <Planets> 
    <Planet Id="1"> 
     <Name>Mercury</Name> 
    </Planet> 
    <Planet Id="2"> 
     <Name>Venus</Name> 
    </Planet> 
    <Planet Id="3"> 
     <Name>Earth</Name> 
    </Planet> 
    </Planets> 
</SolarSystem> 

Kod odnajduje <Planet> Merkury, dodaje dodatkowy element do niego, usuwa go i ponownie umieszcza go na końcu kolekcji <Planets>.

XDocument SolarSystem = XDocument.Load(Server.MapPath("SolarSystem.xml")); 
IEnumerable<XElement> Planets = SolarSystem.Element("SolarSystem").Element("Planets").Elements("Planet"); 

// identify and change Mercury 
XElement Mercury = Planets.Where(p => p.Attribute("Id").Value == "1").FirstOrDefault(); 
Mercury.Add(new XElement("YearLengthInDays", "88")); 

// remove Mercury from current position, and add back in at the end 
Mercury.Remove(); 
Planets.Last().AddAfterSelf(Mercury); 

// save it as new file 
SolarSystem.Save(Server.MapPath("NewSolarSystem.xml")); 

co daje:

<?xml version="1.0" encoding="UTF-8"?> 
    <SolarSystem> 
    <Planets> 
     <Planet Id="2"> 
     <Name>Venus</Name> 
     </Planet> 
     <Planet Id="3"> 
     <Name>Earth</Name> 
     </Planet> 
     <Planet Id="1"> 
     <Name>Mercury</Name> 
     <YearLengthInDays>88</YearLengthInDays> 
     </Planet> 
    </Planets> 
    </SolarSystem> 
+1

+1: Przyjemna próbka 101, aby ludzie zaczęli. –

1

To po prostu opiera się na przykładzie @Ralph Lavelle powyżej. Stworzyłem kompletną aplikację konsolową, dzięki czemu mogłem grać z kodem & lepiej to zrozumieć. Pomyślałem, że to podzielę. Używa tego samego przykładowego XML-a z powyższego, ale musiałem usunąć odwołania do Server.MapPath(), ponieważ nie mogłem wymyślić, jak je uruchomić. Proszę bardzo:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Xml.Linq; 

class LinqDemo 
{ 
static void Main() 
    { 
     XDocument SolarSystem = XDocument.Load("SolarSystem.xml"); 
     IEnumerable<XElement> Planets = SolarSystem.Element("SolarSystem").Element("Planets").Elements("Planet"); 

     // identify and change Mercury 
     XElement Mercury = Planets.Where(p => p.Attribute("Id").Value == "1").FirstOrDefault(); 
     Mercury.Add(new XElement("YearLengthInDays", "88")); 

     // remove Mercury from current position, and add back in at the end 
     Mercury.Remove(); 
     Planets.Last().AddAfterSelf(Mercury); 

     // save it as new file 
     SolarSystem.Save("NewSolarSystem.xml"); 
    } 
} 

Może to pomoże innym noobom LINQ takim jak ja.

+0

Doceń zwięzły przykład pracy. Okazało się, że 'using System;' is *** not *** required. Ale to 'using System.Xml;' *** jest *** wymagane. – DavidRR

+0

Nie ma za co. Nienawidzę znajdowania pytania, które wydaje się mieć odpowiedź, której szukam, i niejasnego kodu o tym, jak właściwie to zrobić. Dobry błąd w korzystaniu z błędu. – delliottg

Powiązane problemy