Groups | Blog | Home
all groups > dotnet xml > november 2003 >

dotnet xml : Using XML as a Database


MT
11/21/2003 2:53:51 PM
I have a gigantic XML file which has the following structure:
<root>
<header>
</header>
<entry index="1">asdf</entry>
<entry index="2">fdfsa</entry>
........

</root>

Now, I have a simple XSD defined that automatically generates a Dataset for
me. I use the MyDatabase.ReadXml method to read the xml into the Dataset. It
fills the correct DataTables and DataRows automatically. I think this is an
inefficient way of reading the XML.

Is there a way I can do a query into my file so I can just grab my entry
with index "x" instead of reading the entire document using ReadXML?

Thanks,
MT

Keith Chadwick
11/21/2003 4:42:07 PM
Use an Xpath query on the XmlDataDocument

in xsl you could do

<xsl:tempate select="//entry[@index=$theindex">
do something
</xsl:template>

You could pass theindex up with a param object on your transform.

Or in code assuming the xml is store in a
XmlDataDocument object or other object that supports selectSingleNode

dim entryValue as string
entryValue=myxmldatadocument.selectSingleNode("//entry[@index=" & thenode &
"]").text

the XmlDataDocument is really slow for transforamtions though so you might
want to use a different object.

Cheers
Keith


[quoted text, click to view]

Anders Borum
11/21/2003 11:31:57 PM
Hello!

You could use an forward-only cached Xml reader like the XPathDocument, and
then create an XPathNavigator object using the .CreateNavigator() on the
XPathDocument.

The XPathNavigator allows you to query the Xml using an XPath expression
(and the .Select() method on that class). If your Xml is very heavy, I'd
suggest implementing an XmlTextReader instead (I've processed Xml files over
45MB with no problems at all - it's very snappy).

Is this what you're looking for?

venlig hilsen / with regards
anders borum
--

Oleg Tkachenko
11/23/2003 12:39:09 PM
[quoted text, click to view]

A way inefficent in fact.

[quoted text, click to view]

Just read document using XmlTextReader:

XmlTextReader r = new XmlTextReader("foo.xml");
while (r.Read()) {
if (r.NodeType == XmlNodeType.Element &&
r.Name == "entry" && r.GetAttribute("index")=="2")
Console.WriteLine("Entry value: {0}", r.ReadString());
}

That's the most effective way (and simple too).
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Oleg Tkachenko
11/23/2003 12:48:15 PM
[quoted text, click to view]

Beware that's going to be extremelly slow. XmlDataDocument, big XML and
XPath with those // - the worst combination ever.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Oleg Tkachenko
11/23/2003 1:03:55 PM
[quoted text, click to view]

XPathDocument is not forward-only cached Xml reader! XPathDocument loads
the whole document into memory to be able to navigate it in all
directions. It's more lightweight and faster than XmlDocument, but
anyway. If the querying logic is simple, just like in this case, ordinar
XmlReader is the best solution.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
MT
11/24/2003 11:57:51 AM
What about using the SQLXMLAdapter? Here's some information on it:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sqlxml3/htm
/dotnet_0jck.asp

I will be sequentially reading the data from the XML file and I do need to
read it into the dataset. Wouldn't this be much easier?

MT


[quoted text, click to view]

Anders Borum
11/24/2003 12:52:56 PM
Hi Oleg!

Sorry about the confusion witht the "forward only" statement. What I was
referring to was, that it's optimized for XPath queries (I was in a
different state of mind when I wrote that, I guess ..).

As long as we can agree that the XPathDocument is faster than the
XmlDocument for XPath queries in a read-only mode.

--
venlig hilsen / with regards
anders borum
--

Oleg Tkachenko
11/24/2003 2:24:55 PM
[quoted text, click to view]

Sure! And apart from its speed - it takes much less space in memory due
to much simpler data model.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
AddThis Social Bookmark Button