C# 解析XML和提取节点

C# 解析XML和提取节点,c#,xml,C#,Xml,我有以下XML: <bookstore> <book genre='autobiography' publicationdate='1981-03-22' ISBN='1-861003-11-0'> <title>The Autobiography of Benjamin Franklin</title> <author> <first-name>Benjami

我有以下XML:

<bookstore>
    <book genre='autobiography' publicationdate='1981-03-22' ISBN='1-861003-11-0'>
        <title>The Autobiography of Benjamin Franklin</title>
        <author>
            <first-name>Benjamin</first-name>
            <last-name>Franklin</last-name>
        </author>
        <price>8.99</price>
    </book>
</bookstore>

这里有一些使用XElement执行此操作的示例代码:

    var xml = XElement.Load("test.xml");

    foreach (var bookEl in xml.Elements("book"))
    {
        Console.WriteLine("Genre: " + bookEl.Attribute("genre").Value
            + " " + "Publication: " + bookEl.Attribute("publicationdate").Value
            + " " + "ISBN: " + bookEl.Attribute("ISBN").Value);
        Console.WriteLine("Title: " + bookEl.Element("title").Value);
        Console.WriteLine("Author: " + bookEl.Element("author").Element("first-name").Value 
            + " " + bookEl.Element("author").Element("last-name").Value);
        Console.WriteLine("Price: " + bookEl.Element("price").Value);
    }

我建议您仔细阅读XPath和
System.Xml
名称空间。此外,我不确定Unity与此有什么关系。这感觉像是一个“请给我代码”的问题。这并不是Stackoverflow的真正目的。你可能是对的,但如果有人不知道代码并要求它,那没有什么不好的。请不要只是要求我们为你解决问题。向我们展示你是如何试图自己解决问题的,然后向我们展示结果是什么,并告诉我们为什么你觉得它不起作用。请参阅“”,以获得一篇您确实需要阅读的优秀文章。Jawad,如果有人不知道代码,并且不花一点时间来搜索它,那么就有很多错误。谢谢,这就是我一直在寻找的。
    var xml = XElement.Load("test.xml");

    foreach (var bookEl in xml.Elements("book"))
    {
        Console.WriteLine("Genre: " + bookEl.Attribute("genre").Value
            + " " + "Publication: " + bookEl.Attribute("publicationdate").Value
            + " " + "ISBN: " + bookEl.Attribute("ISBN").Value);
        Console.WriteLine("Title: " + bookEl.Element("title").Value);
        Console.WriteLine("Author: " + bookEl.Element("author").Element("first-name").Value 
            + " " + bookEl.Element("author").Element("last-name").Value);
        Console.WriteLine("Price: " + bookEl.Element("price").Value);
    }