473,757 Members | 9,145 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Creat e(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 2405
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.Creat e(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********@joer gjooss.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.Loa d(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(st ring fileName) {
// 1. Create an XmlReaderSettin gs instance and set its properties
// according to your parsing and validation requirements
XmlReaderSettin gs settings = new XmlReaderSettin gs();
settings.Valida tionFlags = XmlSchemaValida tionFlags.None;

// 2. Obtain a stream to read the XML from. XmlReader.Creat e() 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(FileStrea m stream =
new FileStream(file Name, FileMode.Open, FileAccess.Read ))
// 3. Create an XmlReader using your stream and your settings
using(XmlReader reader = XmlReader.Creat e(stream, settings)) {
// Simply dump all nodes with their name and type
while(reader.Re ad()) {
string nodeDescription = String.Format(
"{0} [{1}]",
reader.Name,
reader.NodeType );
Console.WriteLi ne(nodeDescript ion);
}
}
}

Cheers,
--
Joerg Jooss
ne********@joer gjooss.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
3717
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 public inside the class)
5
4754
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")) trans.Transform(oXmlDataDocument, null, Response.OutputStream, null); //Writes immediately to browser her But if I wanted to return the transformation as a variable and not immediately to the browser, the only way I found to do this was using the...
0
2638
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, reader2, after creating new XmlReaderSettings and a new XmlParserContext. // customContext and customSettings may or may not make use reader1's properties XmlParserContext customContext = new XmlParserContext(...);
2
11537
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 ResetState method... why XmlReader has not? Thanks, Luigi.
7
44539
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
3189
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 dir.GetFiles("*.xml")) { try { ParseXML(file.FullName); }
0
2451
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 follows while(reader.Read()) { ... } System.Xml.XmlException: Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it. Line 2, position 3. Unfortunately I...
1
1364
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); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); binaryFormatter.Serialize(stream, sXML); stream.Position = 0;
2
5590
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 bytes into utf-8. MemoryStream ms = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(ms, new UTF8Encoding(false)); writer.Formatting = Formatting.Indented;
0
9489
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7286
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.