Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > read or convert xml file to a string

Reply
Thread Tools

read or convert xml file to a string

 
 
Jon Paal
Guest
Posts: n/a
 
      01-25-2006
how can I read an xml file and convert it to a string

or

convert an XmlTextReader to a string


 
Reply With Quote
 
 
 
 
KJ
Guest
Posts: n/a
 
      01-25-2006
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.

 
Reply With Quote
 
David Veeneman
Guest
Posts: n/a
 
      01-25-2006
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


 
Reply With Quote
 
Jon Paal
Guest
Posts: n/a
 
      01-25-2006
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()


 
Reply With Quote
 
KJ
Guest
Posts: n/a
 
      01-25-2006
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.

 
Reply With Quote
 
KJ
Guest
Posts: n/a
 
      01-25-2006
Whoops, I meant False (when it's not a fragment)!

 
Reply With Quote
 
Jon Paal
Guest
Posts: n/a
 
      01-26-2006
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


 
Reply With Quote
 
KJ
Guest
Posts: n/a
 
      01-26-2006
OK. Earlier you wrote that you want to read from an XmlTextReader, not
a DataSet. What happened to that idea?

 
Reply With Quote
 
Jon Paal
Guest
Posts: n/a
 
      01-26-2006
I have this working:

Dim fp As StreamReader
Dim xmlData As String

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


 
Reply With Quote
 
KJ
Guest
Posts: n/a
 
      01-26-2006
Simple enough.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert zip file contents list string to file system feraso Java 0 04-12-2009 06:52 AM
how to read binary file in java and how convert into ASCII padarthi.kavitha@gmail.com Java 1 06-09-2008 09:01 AM
how can i convert a file stream in a string? Manuel C++ 7 09-12-2006 10:25 PM
How do you convert a string obj to a file obj? Matthew Thorley Python 7 05-04-2005 09:04 PM
Q: Newbee. Read and convert CSV file... Martin Hvidberg C Programming 4 02-24-2005 05:51 AM



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 47 48 49 50 51 52 53 54 55 56 57