ASP.Net [2.0] XMLReader / Stream?

R

Rob Meade

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
 
J

Joerg Jooss

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,
 
R

Rob Meade

...
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
 
J

Joerg Jooss

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,
 
R

Rob Meade

...

[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
 
M

Martin Honnen

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.
 
R

Rob Meade

...
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top