Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
| FAQ | Members List | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 6th, 2008, 10:54 AM
Registered User
 
Join Date: Feb 2008
Location: , , Ireland.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default parsing flat xml file using asp.net 2.0

I'm trying to read in a flat xml file into an asp.net application,
i then want to export the contents of the file into a database table in sql server but i'm having problems reading in the xml file.
The xml file has only one level, it looks like the following with data in it:
<?xml version="1.0" encoding="UTF-8" ?>
- <form1>
  <ProjectTitle>mike' s project title</ProjectTitle>
  <ProjectOutline>this is a brief project outline</ProjectOutline>
  <ProjectNo>124321</ProjectNo>
  <ProjectLeader>joe bloggs</ProjectLeader>
  <ProjectDuration>12.00000000</ProjectDuration>
  <EndDate>2008-04-10</EndDate>
  <StartDate>2008-02-04</StartDate>
  <drpOrganisation>Dunmore Ltd</drpOrganisation>
  <drpPartner>Fishermans Co-op</drpPartner>
  <FDDteam>Environment Group 9</FDDteam>
 </form1>

In my .net application I'm using te following code:

Dim objXMLReader As XmlTextReader = New XmlTextReader(sDir & "\" & File.Name) 'open up the xml file for reading


            Do While objXMLReader.Read
                strNode = ""
                For intI = 1 To objXMLReader.Depth
                    strNode &= "-"
                Next
                strNode &= "Name:" & objXMLReader.Name & " "
                strNode &= "Nodetype:" & objXMLReader.NodeType.ToString & " "
                If objXMLReader.HasValue Then
                    strNode &= "Value:" & objXMLReader.Value
                End If
                Response.Write(strNode & "<br/>")
            Loop
            objXMLReader.Close()


The problem is it doesn't traverse properly through the fields.
I think the problem is because my xml file doesn't have 'levels' in it.
thanks

Reply With Quote
  #2 (permalink)  
Old February 6th, 2008, 11:40 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I don't see a problem with the code.

What exactly are you expecting, and what are you seeing?

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #3 (permalink)  
Old February 6th, 2008, 12:09 PM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well Xml(Text)Reader used with only the Read method is kind of a low level API that gives you a lot of different nodes, you will get element nodes, text nodes, and end element nodes the way you have set up your code.
Below I show some VB.NET 2005 sample that reads out the text contents of all elements at Depth 1, that might more likely what you want to achieve:
Code:
        Using reader As XmlReader = XmlReader.Create("..\..\XMLFile1.xml")
            While reader.Read()
                If reader.NodeType = XmlNodeType.Element And reader.Depth = 1 Then
                    Console.WriteLine("Element with name ""{0}"" has contents ""{1}""", reader.Name, reader.ReadString())
                End If
            End While
        End Using
Output with your sample XML is as follows:
Code:
Element with name "ProjectTitle" has contents "mike' s project title"
Element with name "ProjectOutline" has contents "this is a brief project outline"
Element with name "ProjectNo" has contents "124321"
Element with name "ProjectLeader" has contents "joe bloggs"
Element with name "ProjectDuration" has contents "12.00000000"
Element with name "EndDate" has contents "2008-04-10"
Element with name "StartDate" has contents "2008-02-04"
Element with name "drpOrganisation" has contents "Dunmore Ltd"
Element with name "drpPartner" has contents "Fishermans Co-op"
Element with name "FDDteam" has contents "Environment Group 9"
My sample writes to the console, in an ASP.NET application you might want to output the data in a control or send it directly to the Response.


Reply With Quote
  #4 (permalink)  
Old February 11th, 2008, 06:51 AM
Registered User
 
Join Date: Feb 2008
Location: , , Ireland.
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

martin,
that's exactly what I was looking for.
thanks for help.

Reply With Quote
  #5 (permalink)  
Old April 27th, 2008, 10:28 PM
Registered User
 
Join Date: Apr 2008
Location: , , .
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default


Hi, I need to import data from a xml file to sql database using asp.net. Have you figured out a way to do this? Please let me know.

Quote:
quote:Originally posted by mikecarmody
 I'm trying to read in a flat xml file into an asp.net application,
i then want to export the contents of the file into a database table in sql server but i'm having problems reading in the xml file.
The xml file has only one level, it looks like the following with data in it:
<?xml version="1.0" encoding="UTF-8" ?>
- <form1>
  <ProjectTitle>mike' s project title</ProjectTitle>
  <ProjectOutline>this is a brief project outline</ProjectOutline>
  <ProjectNo>124321</ProjectNo>
  <ProjectLeader>joe bloggs</ProjectLeader>
  <ProjectDuration>12.00000000</ProjectDuration>
  <EndDate>2008-04-10</EndDate>
  <StartDate>2008-02-04</StartDate>
  <drpOrganisation>Dunmore Ltd</drpOrganisation>
  <drpPartner>Fishermans Co-op</drpPartner>
  <FDDteam>Environment Group 9</FDDteam>
 </form1>

In my .net application I'm using te following code:

Dim objXMLReader As XmlTextReader = New XmlTextReader(sDir & "\" & File.Name) 'open up the xml file for reading


            Do While objXMLReader.Read
                strNode = ""
                For intI = 1 To objXMLReader.Depth
                    strNode &= "-"
                Next
                strNode &= "Name:" & objXMLReader.Name & " "
                strNode &= "Nodetype:" & objXMLReader.NodeType.ToString & " "
                If objXMLReader.HasValue Then
                    strNode &= "Value:" & objXMLReader.Value
                End If
                Response.Write(strNode & "<br/>")
            Loop
            objXMLReader.Close()


The problem is it doesn't traverse properly through the fields.
I think the problem is because my xml file doesn't have 'levels' in it.
thanks

Reply With Quote
  #6 (permalink)  
Old April 28th, 2008, 02:55 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Which part of this are you struggling with. You have an example above of reading a file, alternatively you could use XmlDocument if the size is not large. You can then use SQL Server's XML features to import or just create standard SQL by parsing the XML. Can you describe the scenario in more detail?

--

Joe (Microsoft MVP - XML)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
parsing a flat file in java Rod Guteriez BOOK: Beginning Java 2 2 May 18th, 2009 09:11 AM
Help Parsing XML File Sojan80 XML 7 January 7th, 2008 06:09 PM
How to Parse - Parsing Flat File Data - Asp darinsee Classic ASP Databases 5 May 24th, 2004 04:11 PM
Parse - Parsing Flat File Data - Asp darinsee Classic ASP Basics 1 May 22nd, 2004 08:39 AM
Parse - Parsing Flat File Data - Asp darinsee Classic ASP Components 1 May 22nd, 2004 08:19 AM



All times are GMT -4. The time now is 02:27 PM.


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