I’m planning to use XML for database purpose. Only thing I was able to do is read whole XML file. I want to be able to read only some data and I don’t know how to do that.
Here is a simple XML
<Books>
<Book>
<Title>Animals</Title>
<Author>J. Anderson</Author>
</Book>
<Book>
<Title>Car</Title>
<Author>L. Sawer</Author>
</Book>
</Books>
I’m interested in app where output is gonna be
Books:
Animals
Cars
Authors:
J. Anderson
L. Sawer
I’m just want to learn how read specific data from XML not whole file.
[SOLVED]
I have used Linq to XML
I don’t think you can “legally” load only part of an XML file, since then it would be malformed (there would be a missing closing element somewhere).
Using LINQ-to-XML, you can do
var doc = XDocument.Load("yourfilepath"). From there its just a matter of querying the data you want, say like this:HTH.
EDIT:
Okay, just to make this a better sample, try this (with @JWL_’s suggested improvement):
You will need to adjust the path in
XDocument.Load()to point to your XML file, but the rest should work. Ask questions about which parts you don’t understand.