![]() |
|
|
|||||||
![]() |
ASP Net - read or convert xml file to a string |
|
|
Thread Tools | Search this Thread |
|
|
#1 |
|
how can I read an xml file and convert it to a string
or convert an XmlTextReader to a string Jon Paal |
|
|
|
|
#2 |
|
Posts: n/a
|
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 |
|
|
|
#3 |
|
Posts: n/a
|
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 |
|
|
|
#4 |
|
Posts: n/a
|
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 |
|
|
|
#5 |
|
Posts: n/a
|
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 |
|
|
|
#6 |
|
Posts: n/a
|
Whoops, I meant False (when it's not a fragment)!
KJ |
|
|
|
#7 |
|
Posts: n/a
|
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 |
|
|
|
#8 |
|
Posts: n/a
|
OK. Earlier you wrote that you want to read from an XmlTextReader, not
a DataSet. What happened to that idea? KJ |
|
|
|
#9 |
|
Posts: n/a
|
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 |
|
|
|
#10 |
|
Posts: n/a
|
Simple enough.
KJ |
|
![]() |
| Thread Tools | Search this Thread |
|
|
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 |