By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,213 Members | 1,966 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,213 IT Pros & Developers. It's quick & easy.

ASP.Net [2.0] XMLReader / Stream?

P: n/a
Hi all,

I'm having a few difficulties with the above, ie I cant find any good
examples anywhere of how to do what I want to do!

I have an xml file on my desktop which I want to read in.

Having checked the info for XML file reading it suggests that with .net 2.0
you should use the XMLReader class...

Ok - so down that road I went..it seems that I need to give this a Stream -
do you think I can work this out - nope...

So far I have this:

' delcare variables
Dim Stream As Stream
Dim XmlReader As XmlReader

' create a new instance of our object
XmlReader = XmlReader.Create(Stream)

I appreciate that some where between creating the new instance of the
XMLReader and declaring it I need to create a new steam and read in the XML
file - this is where I'm lost..

I keep going around in circles and keep stumbling across the StreamReader
class - but I dont see how this is any good to me as I cant pass the
StreamReader into the XmlReader object...

Any help with this would be appreciated..I had an example of how to do this
before with .net 1.0 - but it wasn't using the new XMLReader class (think it
might have been XMLTextReader or something instead)..

Regards

Rob
May 27 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Thus wrote Rob,
Hi all,

I'm having a few difficulties with the above, ie I cant find any good
examples anywhere of how to do what I want to do!

I have an xml file on my desktop which I want to read in.

Having checked the info for XML file reading it suggests that with
.net 2.0 you should use the XMLReader class...

Ok - so down that road I went..it seems that I need to give this a
Stream - do you think I can work this out - nope...

So far I have this:

' delcare variables
Dim Stream As Stream
Dim XmlReader As XmlReader
' create a new instance of our object
XmlReader = XmlReader.Create(Stream)
I appreciate that some where between creating the new instance of the
XMLReader and declaring it I need to create a new steam and read in
the XML file - this is where I'm lost..

I keep going around in circles and keep stumbling across the
StreamReader class - but I dont see how this is any good to me as I
cant pass the StreamReader into the XmlReader object...

Any help with this would be appreciated..I had an example of how to do
this before with .net 1.0 - but it wasn't using the new XMLReader
class (think it might have been XMLTextReader or something instead)..


The relevance to ASP.NET escapes me here, but a FileStream should do the
trick ;-)

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
May 27 '06 #2

P: n/a
"Joerg Jooss" wrote ...
The relevance to ASP.NET escapes me here, but a FileStream should do the
trick ;-)


Hi Joerg,

The relevance, which I should have explained better in my first post - I'm
writing a set of classes that will be used in a) a windows app that will
read the xml file from my desktop (whilst testing) - b) my web application
for reading xml files on the server.

The code is going to be the same in the classes, albeit the location of the
file might differ...

It's the iteration part I'm having problems with now...

In .net 1.0 - I used to "load" an xml file into an XML document - from there
I could iterate through each XML Element thus finding the correct ones.

I dont "get" the XMLReader stuff - I dont seem to be able to grab the
elements at all?

A small example would be handy!

Regards

Rob
May 27 '06 #3

P: n/a
Thus wrote Rob,
Hi Joerg,
[...]
The code is going to be the same in the classes, albeit the location
of the file might differ...

It's the iteration part I'm having problems with now...

In .net 1.0 - I used to "load" an xml file into an XML document - from
there I could iterate through each XML Element thus finding the
correct ones.
You can still use the XmlDocument.Load(string uri) overload. If it does what
you want by all means use it.
I dont "get" the XMLReader stuff - I dont seem to be able to grab the
elements at all?


XmlReader will fetch one XML node (*not* element) at a time. You will need
to use an XmlReader if you need a more fine grained control over the XML
parsing (and validation) process. Here's a sample that illustrates the process.

public void ParseXmlFile(string fileName) {
// 1. Create an XmlReaderSettings instance and set its properties
// according to your parsing and validation requirements
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationFlags = XmlSchemaValidationFlags.None;

// 2. Obtain a stream to read the XML from. XmlReader.Create() allows you
// to specify a URI to read from , so doing this isn't really required,
but
// it gives you more fine grained control over how the file is being
accessed.
using(FileStream stream =
new FileStream(fileName, FileMode.Open, FileAccess.Read))
// 3. Create an XmlReader using your stream and your settings
using(XmlReader reader = XmlReader.Create(stream, settings)) {
// Simply dump all nodes with their name and type
while(reader.Read()) {
string nodeDescription = String.Format(
"{0} [{1}]",
reader.Name,
reader.NodeType);
Console.WriteLine(nodeDescription);
}
}
}

Cheers,
--
Joerg Jooss
ne********@joergjooss.de
May 27 '06 #4

P: n/a
"Joerg Jooss" wrote ...

[snip]

Hi Joerg,

Thanks for the reply - I'd resorted back to my 1.1 attempts prior to your
reply and seem to have it doing what I wanted it to do - thank you for the
example, I will most likely give it a try later.

Could you explain/clarify for me - the difference between a "node" and an
"element" - you mentioned that it would only look at a single node in your
reply...

So, in this mini example..

<newsgroupUsers>
<user>
<name>Rob</name>
</user>
<user>
<name>Joerg</name>
</user>
</newsgroupUsers>

Are you saying that the XMLReader will be used to pick up one of the above
"name" nodes...as opposed to a "user" element?

Thanks again for the example and any further help.

Regards

Rob
May 27 '06 #5

P: n/a


Rob Meade wrote:

Could you explain/clarify for me - the difference between a "node" and an
"element" - you mentioned that it would only look at a single node in your
reply...

So, in this mini example..

<newsgroupUsers>
<user>
<name>Rob</name>
</user>
<user>
<name>Joerg</name>
</user>
</newsgroupUsers>


Joerg posted some sample code that you could have used to run it against
your XML sample to see what kind of nodes the reader processes with each
Read call.
Then you would get the output

newsgroupUsers [Element]
[Whitespace]
user [Element]
[Whitespace]
name [Element]
[Text]
name [EndElement]
[Whitespace]
user [EndElement]
[Whitespace]
user [Element]
[Whitespace]
name [Element]
[Text]
name [EndElement]
[Whitespace]
user [EndElement]
[Whitespace]
newsgroupUsers [EndElement]

and you can see that the reader pulls in Element nodes, Text nodes,
Whitespace nodes.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 27 '06 #6

P: n/a
"Martin Honnen" wrote ...
and you can see that the reader pulls in Element nodes, Text nodes,
Whitespace nodes.


Hi Martin,

Thanks muchly for the reply and explanation.

Cheers

Rob
May 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.