2014-12-23 25 views
-2

我有下面的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>Benjamin</first-name> 
      <last-name>Franklin</last-name> 
     </author> 
     <price>8.99</price> 
    </book> 
</bookstore> 

我想读它,并显示结果如下:

Genre: autobiography 
Publication: 1981-03-22 
ISBN: 1-861003-11-0 
Title: The Autobiography of Benjamin Franklin 
Author: Benjamin Franklin 
Price: 8.99 
+0

我建议你阅读XPath和'System.Xml'命名空间。另外,我不确定Unity与此有什么关系。 – Phylogenesis

+0

这感觉就像一个'请给我代码'的问题。这不是Stackoverflow的真正意义。 – Steven

+1

你可能是对的,但如果有人知道代码并要求它,那没有什么不好的。 –

回答

1

这里有一些示例代码来做到这一点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); 
    } 
+0

谢谢,这就是我一直在寻找的。 –