Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
| Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 28th, 2005, 04:15 PM
Authorized User
 
Join Date: Jun 2004
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default Reading from a xml file and writing to another

Hi there,

Using ASP.NET my goal is to modify a xml document using XmlTextReader and XmlTextWriter (performance reasons). While reading a xml document line by line I want to write selective lines to another file.

My problem lies in the fact that XmlTextReader does not allow you to return a single line of xml, only the InnerXml or OuterXml of a node.

For example, consider the following xml
Code:
<animals>
  <fish selected="true">
    <type>Water</type>
  </fish>
  <monkey selected="false">
    <type>Land</type>
  </monkey>
</animals>
Say I want to create a new xml file that contains all of the selected animals (i.e. selected="true") from the above xml. I would write the xml line by line if the elements do not contain attribute selected="false".

Any suggestions on how I can accomplish this?

Thanks!

 
Old April 28th, 2005, 04:29 PM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Lines have no significance in XML. If you know how the XML is formatted and want to exploit this fact, then treat is as text and don't use XML software for the job. If you're parsing it as XML, then the significant units are elements, not lines.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 29th, 2005, 08:57 AM
Authorized User
 
Join Date: Jun 2004
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply. I've come up with another way to accomplish my task and wanted your opinion on which method is less process intensive and more optimal.

The first is your suggestion. I would store the xml as a string and split it on a line break. For each index I would check for "selected='true'" and write as needed.

The second method is to re-create the xml using the XmlTextWriter methods (WriteStartElement, WriteAttributeString, etc). Using XmlTextReader, if I find an element that has attribute selected="true" then create a start element with the same name and create all the same attribute value pairs.

Thanks again :-)

 
Old April 29th, 2005, 09:12 AM
Authorized User
 
Join Date: Jun 2004
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I just realized that your method won't be as simple as it seems. The concept of depth is gone when using an array of strings vs XmlTextReader. That is, if I have nested elements with the same name then I won't know what the corresponding end element is without keeping track (i.e. creating a stack -> more overhead).

 
Old April 29th, 2005, 09:14 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I wouldn't do either of these, I would do it with XSLT. I don't have time for low-level coding...

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 29th, 2005, 01:40 PM
Authorized User
 
Join Date: Jun 2004
Location: , , .
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Using XSLT involves loading the xml document into memory, which I want to avoid since I'm dealing with very large files.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading and writing .PST file shoakat ASP.NET 2.0 Basics 1 January 25th, 2011 08:31 AM
Displaying, reading & writing to xml Meitoosh VB.NET 0 July 24th, 2007 05:21 AM
writing into xml file jerryjohn XML 1 August 18th, 2006 02:40 AM
writing to am xml file MOU Classic ASP XML 0 July 6th, 2005 07:38 AM
Reading from a text file and writing to a Binary 1 scott01 C++ Programming 0 July 28th, 2003 03:59 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.