0

This is a sample of my XML:

        <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>

I've attempted to implement a method that recovers all this information, but it doesn't work: It recovers only one item in a Stack, and I want it to recover all the elements in the Stack.

What I get is this:

Stack 1 : first book ;

Stack 2 : first Section

This is my 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"));
        }
    }
dbasnett
  • 11,334
  • 2
  • 25
  • 33
Millet Antoine
  • 405
  • 1
  • 6
  • 24
  • find a good tutorial on using xpath to query xml. – Muckeypuck Jul 29 '16 at 13:05
  • 1
    The first thing I notice is that that isn't valid xml (you can't have spaces in tags). The second thing I notice is that your code as written can only ever perform one action per `Stack` tag. – AakashM Jul 29 '16 at 13:12
  • Yes i understand but i dont know how to implement an algorythm who don't perfom one action by stack tag – Millet Antoine Jul 29 '16 at 13:15
  • yeah `
    ` is invalid XML. you prob want something more like yeah `
    ` is invalid XML.
    – Liam Jul 29 '16 at 13:16

2 Answers2

2

You need to iterate through each Stack's children as well:

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);
    }
}
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
0

It is unclear what 'recover' means but if it means create a copy of the existing XML then in VB using XElement it would be

    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")
dbasnett
  • 11,334
  • 2
  • 25
  • 33