473,396 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

ASP.Net [2.0] XMLReader / Stream?

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
6 2385
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
"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
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
"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


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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: xmlguy | last post by:
Cant seem to solve this problem I need to be able to re-use XmlReader and XPathDocument for an XSLT Transform. Basically I have defined following interfaces: Class Render (Common and...
5
by: Dave | last post by:
Hi, I have this code that will write the transformed XML immediately to the browser with the Response object.. XslTransform trans = new XslTransform() trans.Load(MapPath("MyXsl.xsl"))...
0
by: Jen | last post by:
My main question: "How can I get a TextReader or Stream object from an existing XmlReader?". Read on for more: I have an existing XmlReader. Let's call it reader1. I'm creating another reader,...
2
by: BLUE | last post by:
I can set memory stream position to 0 to "reset" it to the initial position, but is there a similar method to reset XmlReader position (else it is at EOF)? I've seen deprecated XmlTextReader had...
7
by: Katit | last post by:
I have string with XML, need to read it using XmlReader. I don't see any way to feed string variable in there. How it's done? Thanks!
5
by: heday60 | last post by:
I've got this application that watches a directory for XML's and then parses them into a DB. It then moves them based on if the XML was succesful or a bad XML... foreach(FileInfo file in...
0
by: gpet44 | last post by:
Hi, I have a problem receiving XML over a NetworkStream in C# .Net 2.0/3.0. I'm creating an XMLReader from the stream. I get the following exception when the XmlReaderreads from the stream as...
1
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
sXML - has XML string //This dies on ReadXML - something about invalid character MemoryStream stream = new MemoryStream(sXML.Length);...
2
by: SammyBar | last post by:
Hi all, I'm trying to convert the xml obtained from a XmlReader object into a UTF-8 array. My general idea is to read the XmlReader and write into a MemoryStream. Then convert the MemoryStream...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.