Go Back   Velocity Reviews > Newsgroups > ASP Net
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

ASP Net - read or convert xml file to a string

 
Thread Tools Search this Thread
Old 01-25-2006, 10:47 PM   #1
Default read or convert xml file to a string


how can I read an xml file and convert it to a string

or

convert an XmlTextReader to a string




Jon Paal
  Reply With Quote
Old 01-25-2006, 11:40 PM   #2
KJ
 
Posts: n/a
Default Re: read or convert xml file to a string
Hi Jon,

The C# code below shows you how to do the latter.

It assumes that you have a boolean variable called IsFragment which
indicates the kind of Xml you are dealing with (either a well-formed
document or just a fragment), because these are read out differently.
You can use this example to get the content of your XmlReader into a
string:

XmlReader reader = o.ExecuteXmlReader(); //uses a SqlCommand to get an
XmlReader, for example, from SQL Server. You can also use the
constructor of the XmlTextReader
StringBuilder XmlStringB = new StringBuilder();
if (!IsFragment)
{
reader.MoveToContent();
XmlStringB.Append(reader.ReadOuterXml());
}
else
{
while (reader.Read())
{
XmlStringB.Append(reader.ReadOuterXml());
}
}
return XmlStringB.ToString()

-Hope this helps.



KJ
  Reply With Quote
Old 01-25-2006, 11:42 PM   #3
David Veeneman
 
Posts: n/a
Default Re: read or convert xml file to a string
Since an XML file is just a text file, couldn't you just use a text reader,
instead of an XML reader, to read the file?

--
David Veeneman
Foresight Systems




David Veeneman
  Reply With Quote
Old 01-26-2006, 12:39 AM   #4
Jon Paal
 
Posts: n/a
Default Re: read or convert xml file to a string
need a little more help getting this into vb (IsFragment ?)


Dim XmlStringB As New StringBuilder()

Dim reader as New DataSet()
reader.ReadXml(Server.MapPath("project.xml"))

If Not IsFragment Then
reader.MoveToContent()
XmlStringB.Append(reader.ReadOuterXml())
Else
While reader.Read()
XmlStringB.Append(reader.ReadOuterXml())
End While
End If
Return XmlStringB.ToString()




Jon Paal
  Reply With Quote
Old 01-26-2006, 12:47 AM   #5
KJ
 
Posts: n/a
Default Re: read or convert xml file to a string
Hi Jon,

The IsFragment is just a boolean type variable is declare in my code.
Basically, I use it because of the way the Xml is read in the if/else
that follows. So, if you know your Xml is always well-formed, just do:

Dim IsFragment as Boolean = True

before the if/else, and you are all set.



KJ
  Reply With Quote
Old 01-26-2006, 12:49 AM   #6
KJ
 
Posts: n/a
Default Re: read or convert xml file to a string
Whoops, I meant False (when it's not a fragment)!



KJ
  Reply With Quote
Old 01-26-2006, 01:12 AM   #7
Jon Paal
 
Posts: n/a
Default Re: read or convert xml file to a string
reader.MoveToContent() is throwing an error


Compiler Error Message: BC30456: 'MoveToContent' is not a member of 'System.Data.DataSet'.

Source Error:


Line 93:
Line 94: If Not IsFragment Then
Line 95: reader.MoveToContent()
Line 96: XmlStringB.Append(reader.ReadOuterXml())
Line 97: Else




Jon Paal
  Reply With Quote
Old 01-26-2006, 03:31 AM   #8
KJ
 
Posts: n/a
Default Re: read or convert xml file to a string
OK. Earlier you wrote that you want to read from an XmlTextReader, not
a DataSet. What happened to that idea?



KJ
  Reply With Quote
Old 01-26-2006, 03:45 AM   #9
Jon Paal
 
Posts: n/a
Default Re: read or convert xml file to a string
I have this working:

Dim fp As StreamReader
Dim xmlData As String

fp = File.OpenText(Server.MapPath("project.xml"))
xmlData = fp.ReadToEnd()
fp.Close()




Jon Paal
  Reply With Quote
Old 01-26-2006, 04:06 AM   #10
KJ
 
Posts: n/a
Default Re: read or convert xml file to a string
Simple enough.



KJ
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help .... How to read a log file using c++... EngSara Software 0 05-17-2008 06:10 PM
problems backing up dvds Lawrence Traub DVD Video 11 09-27-2005 07:34 PM
Re: Ripping DVDs. Please answer the attached question. - Question.txt Stan Brown DVD Video 19 02-09-2005 11:19 PM
Burn process failed - help! Log file posted for help troubleshooting Michael Mason DVD Video 1 08-16-2004 09:24 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46