473,385 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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
Jan 25 '06 #1
9 1350
KJ
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.

Jan 25 '06 #2
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
Jan 25 '06 #3
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()
Jan 26 '06 #4
KJ
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.

Jan 26 '06 #5
KJ
Whoops, I meant False (when it's not a fragment)!

Jan 26 '06 #6
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
Jan 26 '06 #7
KJ
OK. Earlier you wrote that you want to read from an XmlTextReader, not
a DataSet. What happened to that idea?

Jan 26 '06 #8
I have this working:

Dim fp As StreamReader
Dim xmlData As String

fp = File.OpenText(Server.MapPath("project.xml"))
xmlData = fp.ReadToEnd()
fp.Close()
Jan 26 '06 #9
KJ
Simple enough.

Jan 26 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a...
1
by: vkrasner | last post by:
It works with VS2003 and does not in VS2005: in VS2003 : string sMyvalue = ConfigurationSettings.AppSettings; in VS2005 (does not work!!) string sMyvalue = ConfigurationManager.AppSettings; ...
0
by: Peter | last post by:
I am having a problem reading an Excel file that is XML based. The directory I am reading contains Excel files that can be of two types. Either generic Microsoft based or XML based. I am reading...
10
by: Tibby | last post by:
I need to read/write not only text files, but binary as well. It seems like on binary files, it doesn't right the last 10% of the file. -- Thanks --- Outgoing mail is certified Virus...
3
by: JenHu | last post by:
Hi, I want read line by line and characters. The characters are fix length text file, no specific delimited method between each fields. The first line is header line, the last line is footer. ...
5
by: JenHu | last post by:
Hi experts, I wrote a function which retrieves a file in the folder, the file path is : Dim sr As New StreamReader(strFilepath & ReturnFileName) What if I have more than 1 file_name in...
2
by: Eric | last post by:
Hi, I need to send data from a file into a structure. In Vb6 this was done like so. public Type Struct1 v1 as string v2 as string end type
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
6
by: arnuld | last post by:
This works fine, I welcome any views/advices/coding-practices :) /* C++ Primer - 4/e * * Exercise 8.9 * STATEMENT: * write a program to store each line from a file into a *...
2
by: Plumebee | last post by:
Hi, I am very new to programming and have just started to use Visual Basic 2005 Express Edition. I am trying to read from a text file to draw a rectangles and lines. However to begin I'm trying to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.