473,378 Members | 1,417 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,378 software developers and data experts.

How do I use XmlReader on an XmlDocument passed as a parameter

All of the XmlReader examples assume that the XmlDocument is being read from
a file. However, I would like to use it with an XmlDocument passed as a
method parameter, but am not sure how.

Any suggestions?
Jun 19 '06 #1
10 2426


Thirsty Traveler wrote:
All of the XmlReader examples assume that the XmlDocument is being read from
a file. However, I would like to use it with an XmlDocument passed as a
method parameter, but am not sure how.


XmlReader is a fast, forward only non caching way to parse XML. If you
have an XmlDocument instance then XML has already been parsed into a
navigable tree model on which you can do DOM and XPath operations. So I
am not sure why you think you need an XmlReader once you have an
XmlDocument. If you nevertheless want to use the XmlReader API then
there is XmlNodeReader e.g.
XmlReader reader = new XmlNodeReader(xmlDocumentInstanceOrOtherXMLNode);

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 19 '06 #2
What exactly are you trying to do? Read the XmlDocument? If you already have
an XmlDocument, then XPath/XQuery (.SelectNodes(), .SelectSingleNode()) and
the object-model (.GetAttribute() etc) would seem the natural choice - as it
is already too late to beefit from the efficiencies of XmlReader... plus it
is a lot easier to get right! Unless you need to pass a corresponding
xml-reader to another method?

Marc
Jun 19 '06 #3
I have an XML document that is passed from a web service that contains 1 to
many transaction requests to our mainframe. I need to map the values in this
XML document to the mainframe transaction and call it one or more times
(once for each transaction in the XML document). I was thinking XmlReader
would be faster for this purpose.

"Marc Gravell" <ma**********@gmail.com> wrote in message
news:uh**************@TK2MSFTNGP02.phx.gbl...
What exactly are you trying to do? Read the XmlDocument? If you already
have an XmlDocument, then XPath/XQuery (.SelectNodes(),
.SelectSingleNode()) and the object-model (.GetAttribute() etc) would seem
the natural choice - as it is already too late to beefit from the
efficiencies of XmlReader... plus it is a lot easier to get right! Unless
you need to pass a corresponding xml-reader to another method?

Marc

Jun 19 '06 #4
Thirsty Traveler <nf*@nospam.com> wrote:
I have an XML document that is passed from a web service that contains 1 to
many transaction requests to our mainframe. I need to map the values in this
XML document to the mainframe transaction and call it one or more times
(once for each transaction in the XML document). I was thinking XmlReader
would be faster for this purpose.


Faster than what? If you've already got an XmlDocument, all the parsing
has already been done. XML parsing is basically what the reader is for,
so it's redundant at this point.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 20 '06 #5
Hmmm... so in that case, what if I have a document such as this:

<REQUEST_GROUP>
<REQUEST >
<REQUEST_DATA>
<FLOOD_REQUEST _ActionType="Original"
OriginalFloodDeterminationLoanIdentifier="00001238 ">
- <BORROWER _FirstName="Firstname1" _LastName="Lastname1" >
<_RESIDENCE _City="San Rafael" _County=""
_PostalCode="94903" _State="CA" _StreetAddress="1234 Sugarhill Drive" />
</BORROWER>
- <BORROWER _FirstName="Firstname2" _LastName="Lastname2">
<_RESIDENCE _City="Tustin" _County=""
_PostalCode="92780" _State="CA" _StreetAddress="5678 Tustin Ranch Rd."
_StreetAddress2="" />
</BORROWER>
</FLOOD_REQUEST >
<FLOOD_REQUEST _ActionType="Original"
OriginalFloodDeterminationLoanIdentifier="00001238 ">
- <BORROWER _FirstName="Firstname3" _LastName="Lastname3" >
<_RESIDENCE _City="San Rafael" _County=""
_PostalCode="94903" _State="CA" _StreetAddress="9012 Sugarhill Drive" />
</BORROWER>
- <BORROWER _FirstName="Firstname4" _LastName="Lastname4">
<_RESIDENCE _City="Tustin" _County=""
_PostalCode="92780" _State="CA" _StreetAddress="3456 Tustin Ranch Rd."
_StreetAddress2="" />
</BORROWER>
</FLOOD_REQUEST >
</REQUEST_DATA>
<REQUEST >
<REQUEST_GROUP>

The data from this example would need to be parsed into two flood
transactions, each with two co-borrowers and associated residences. However,
doing something like
XmlNodeList nodeList = xml.GetElementsByTagName("BORROWER") returns an array
of four, when I really want two for each request. Other than using the
XmlReader, I am not sure of the best way to do this.
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Thirsty Traveler <nf*@nospam.com> wrote:
I have an XML document that is passed from a web service that contains 1
to
many transaction requests to our mainframe. I need to map the values in
this
XML document to the mainframe transaction and call it one or more times
(once for each transaction in the XML document). I was thinking XmlReader
would be faster for this purpose.


Faster than what? If you've already got an XmlDocument, all the parsing
has already been done. XML parsing is basically what the reader is for,
so it's redundant at this point.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Jun 20 '06 #6
Thirsty Traveler <nf*@nospam.com> wrote:

<snip>
The data from this example would need to be parsed into two flood
transactions, each with two co-borrowers and associated residences. However,
doing something like XmlNodeList nodeList = xml.GetElementsByTagName("BORROWER") returns an array
of four, when I really want two for each request. Other than using the
XmlReader, I am not sure of the best way to do this.


You should get each FLOOD_REQUEST one at a time, and then find the
BORROWER elements underneath that FLOOD_REQUEST.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 21 '06 #7
Like so:

foreach(XmlElement flood in
doc.SelectNodes("/REQUEST_GROUP/REQUEST/REQUEST_DATA/FLOOD_REQUEST")) {
Console.WriteLine("Flood " +
flood.GetAttribute("OriginalFloodDeterminationLoan Identifier"));
foreach (XmlElement borrower in
flood.SelectNodes("BORROWER")) {
Console.WriteLine("Borrower " +
borrower.GetAttribute("_FirstName") + " " +
borrower.GetAttribute("_LastName"));
}
}
Note that this is a simple example of the SelectNodes and
SelectSingleNode methods; they can handle far more expressive queries,
such as "where"-style clauses, parent/child/sibling/ancestor/descendant
queries, etc. Read up on XPath and XQuery for more information.

Marc

Jun 21 '06 #8
Cool, this works great.

I am curious why XmlElements can be extracted from an XmlNodeList? For
example, XmlElement x = flood.SelectSingleNode("BORROWER") generates a
compile error.

"Marc Gravell" <ma**********@gmail.com> wrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Like so:

foreach(XmlElement flood in
doc.SelectNodes("/REQUEST_GROUP/REQUEST/REQUEST_DATA/FLOOD_REQUEST")) {
Console.WriteLine("Flood " +
flood.GetAttribute("OriginalFloodDeterminationLoan Identifier"));
foreach (XmlElement borrower in
flood.SelectNodes("BORROWER")) {
Console.WriteLine("Borrower " +
borrower.GetAttribute("_FirstName") + " " +
borrower.GetAttribute("_LastName"));
}
}
Note that this is a simple example of the SelectNodes and
SelectSingleNode methods; they can handle far more expressive queries,
such as "where"-style clauses, parent/child/sibling/ancestor/descendant
queries, etc. Read up on XPath and XQuery for more information.

Marc

Jun 21 '06 #9
Actially, I got it to work by casting it.

"Thirsty Traveler" <nf*@nospam.com> wrote in message
news:eT**************@TK2MSFTNGP02.phx.gbl...
Cool, this works great.

I am curious why XmlElements can be extracted from an XmlNodeList? For
example, XmlElement x = flood.SelectSingleNode("BORROWER") generates a
compile error.

"Marc Gravell" <ma**********@gmail.com> wrote in message
news:11**********************@m73g2000cwd.googlegr oups.com...
Like so:

foreach(XmlElement flood in
doc.SelectNodes("/REQUEST_GROUP/REQUEST/REQUEST_DATA/FLOOD_REQUEST")) {
Console.WriteLine("Flood " +
flood.GetAttribute("OriginalFloodDeterminationLoan Identifier"));
foreach (XmlElement borrower in
flood.SelectNodes("BORROWER")) {
Console.WriteLine("Borrower " +
borrower.GetAttribute("_FirstName") + " " +
borrower.GetAttribute("_LastName"));
}
}
Note that this is a simple example of the SelectNodes and
SelectSingleNode methods; they can handle far more expressive queries,
such as "where"-style clauses, parent/child/sibling/ancestor/descendant
queries, etc. Read up on XPath and XQuery for more information.

Marc


Jun 21 '06 #10
Thirsty Traveler <nf*@nospam.com> wrote:
Cool, this works great.

I am curious why XmlElements can be extracted from an XmlNodeList? For
example, XmlElement x = flood.SelectSingleNode("BORROWER") generates a
compile error.


That's because SelectSingleNode returns a Node, not an Element - you
might have selected an attribute or a text node, for instance, neither
of which are elements.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 21 '06 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: | last post by:
How do I read the attributes of this XML? I have a page with text boxes that i want to read these values in. notice there are 2 Parameter tags with the same attributes. Code would help ...
1
by: Logician | last post by:
Task: To Read an XML document and handle data contents in .NET for new processing Request: Any details on how to read an XML document processing all nodes and content in .NET Problems:...
8
by: Charles.Deisler | last post by:
Im currently using the following code.. XmlDocument xmlData = new XmlDocument(); XmlTextWriter xmlwriter = new XmlTextWriter(Response.OutputStream,System.Text.Encoding.UTF8); XmlReader...
1
by: | last post by:
How do I read the attributes of this XML? I have a page with text boxes that i want to read these values in. notice there are 2 Parameter tags with the same attributes. Code would help ...
0
by: Jeff | last post by:
Hi, I am trying to use the XML Web Control and the XmlReader but when I try and load the xmlReader into the XmlDocument is gives me this error Error: This document already has a...
1
by: Joe Monnin | last post by:
I have a web service that takes an XmlDocument as a parameter, performs some processing on it, and saves it to a database. The web service signature looks similar to this: public void...
2
by: raj | last post by:
i have this code which is upgraded to work with 2.0 and works great with framework 1.0 and 1.1 without the upgraded code. //2.0 code// public static XmlReader RtfToXml( string Rtf ) { if (...
1
by: engwar | last post by:
The title says it all. How do I use XMLDocument.Validate() against a DTD. In all my Googling about this I only find examples of how to validate an XMLReader or XmlValidatingReader against a DTD....
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.