如何恢复 XML 标签中的所有 XML 元素?

How to recover all the XML elements in an XML tag?

这是我的样本 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>

我试图实现一种恢复所有这些信息的方法,但它不起作用:它只恢复 Stack 中的一个项目,我希望它恢复 Stack 中的所有元素堆叠。

我得到的是这样的:

Stack 1 : first book ;

Stack 2 : first Section

这是我的代码:

 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"));
        }
    }

您还需要遍历每个 Stack 的 children:

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);
    }
}

不清楚 'recover' 是什么意思,但如果它意味着创建现有 XML 的副本,那么在 VB 中使用 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")