2016-07-29 7 views
0

ist eine Probe meiner XML:Wie alle XML-Elemente in einem XML-Tag wiederherstellen? Diese

 <Library> 
      <Stack> 
       <Book> 
        <Author>....</Author> 
        <Date>....</Date> 
       </Book> 
       <Book> 
        <Author>....</Author> 
        <Date>....</Date> 
       </Book> 
      </Stack> 
      <Stack> 
       <SectionScience> 
        <Book> 
         <Author>....</Author> 
         <Date>....</Date> 
        </Book> 
       </SectionScience> 
       <SectionHorror> 
        <Book> 
         <Author>....</Author> 
         <Date>....</Date> 
        </Book> 
       </SectionHorror> 
       <Book> 
        <Author>....</Author> 
        <Date>....</Date> 
       </Book> 
      </Stack> 
     </Library> 

ich versucht habe, ein Verfahren zu implementieren, die alle diese Informationen wieder erholt, aber es funktioniert nicht: Er erholt sich nur ein Element in einem Stack, und ich möchte um alle Elemente im Stack wiederherzustellen.

Was ich bekommen, ist dies:

Stack 1: erstes Buch;

Stapel 2: erster Abschnitt

Dies ist mein Code:

private void ConstructionInterface() 
{ 
    XElement docX = XElement.Load(Application.StartupPath + @"\Library.xml"); 
    foreach (XElement elemColone in docX.Descendants("Stack")) 
    { 
     if (elemColone.Element("SectionHorror") != null) 
     CreateSectionHorror(elemColone.Element("SectionHorror")); 
     else if (elemColone.Element("SectionScience") != null) 
     CreateSectionScience(elemColone.Element("SectionScience")); 
     else if (elemColone.Elements("Book") != null) 
     CreateBook(elemColone.Element("Book")); 
     } 
    } 
+0

Finden Sie eine gute Anleitung zur Verwendung von Xpath zum Abfragen von XML. – Muckeypuck

+1

Das erste, was ich bemerke ist, dass das ist nicht gültig Xml (Sie können keine Leerzeichen in Tags haben). Die zweite Sache, die ich bemerke, ist, dass Ihr Code wie beschrieben nur eine Aktion pro 'Stack'-Tag ausführen kann. – AakashM

+0

Ja, ich verstehe, aber ich weiß nicht, wie man einen Algorithmus implementiert, die keine Aktion durch Stack-Tag –

Antwort

2

Sie müssen auch durch jeden Stack ‚s Kinder iterieren:

foreach (XElement elemColone in docX.Descendants("Stack")) 
{ 
    foreach (var sectionOrBook in elemColone.Elements()) 
    { 
     if (sectionOrBook.Name == "SectionHorror") 
      CreateSectionHorror(sectionOrBook); 
     else if (sectionOrBook.Name == "SectionScience") 
      CreateSectionScience(sectionOrBook); 
     else if (sectionOrBook.Name == "Book") 
      CreateBook(sectionOrBook); 
    } 
} 
0

Es ist unklar, was "wiederherstellen" bedeutet, aber wenn es bedeutet, eine Kopie des vorhandenen XML zu erstellen, dann wäre es in VB mit XElement

Dim xe As XElement 
    'to load from a file 
    ' xe = XElement.Load("Your Path Here") 

    ' for testing 
    xe = 
     <Library> 
      <Stack> 
       <Book> 
        <Author>....</Author> 
        <Date>....</Date> 
       </Book> 
       <Book> 
        <Author>....</Author> 
        <Date>....</Date> 
       </Book> 
      </Stack> 
      <Stack> 
       <SectionScience> 
        <Book> 
         <Author>....</Author> 
         <Date>....</Date> 
        </Book> 
       </SectionScience> 
       <SectionHorror> 
        <Book> 
         <Author>....</Author> 
         <Date>....</Date> 
        </Book> 
       </SectionHorror> 
       <Book> 
        <Author>....</Author> 
        <Date>....</Date> 
       </Book> 
      </Stack> 
     </Library> 

    Dim recover As XElement = New XElement(xe) ' this line creates a copy 

    ' recover.Save("path here") 
Verwandte Themen