Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert XmlDocument to a string 1

Status
Not open for further replies.

dashen

Programmer
Jul 14, 2005
233
US
Code:
Private myQuestions As New XmlDocument
myQuestions.Load(filename)
myQuestions.Value.ToString

I have an xml file that I would like to load into a SQL database as a text file. I cannot load the xml file directly because I am using a vendor software that requires the Text field. I am not sure how to load the XmlDocument as a String, but the above code was my attempt at that. Any help would be appreciated. Thanks.
 
Code:
Dim oXmlDoc as XmlDocument

oXmlDoc.Load(filename)

sUsername = oXmlDoc.GetElementsByTagName("username").Item(0).InnerText
        sPassword = oXmlDoc.GetElementsByTagName("password").Item(0).InnerText
        sDatabase = oXmlDoc.GetElementsByTagName("database").Item(0).InnerText
        sInclude = 
oXmlDoc.GetElementsByTagName("include").Item(0).InnerText
        sExclude = oXmlDoc.GetElementsByTagName("exclude").Item(0).InnerText

Here's how I get values from my xmlDoc. If you extract from each tag to a single string, you could then send the string into the text file as you please.

Failing that if you want to keep the full xml formatting etc (rather than just the values) you could parse the file as though it was a Text document using a StreamReader, then output that into the text field.

Cheers,
Dave

"Yes, I'll stop finding bugs in the software - as soon as you stop writing bugs into the software." <-- Me

For all your testing needs: Forum1393
 
Yeah, I know how to get individual elements, but I want to get the whole text value, so that I can store it in a text field in a database.

That is very different. Do I need to use a StreamReader?
 
If all you want to do is read an entire text file then maybe one of the following will help.

[tt]
Dim filecontents As String = My.Computer.FileSystem.ReadAllText("Filename")
[/tt]

or

[tt]
Dim sr As New System.IO.StreamReader("Filename")
Dim filecontents As String = sr.ReadToEnd()
[/tt]


Hope this helps.

[vampire][bat]
 
My first star for a solution to an XML question!!!

I normally ignore questions that have XML, HTML or anything like that in them, so thanks, and glad I could help - (although the answer had nothing to do with XML, but we'll keep that bit quiet [wink]).

[vampire][bat]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top