[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Is there a way to load xml file contents into a string variable without copy/paste by user in C#??

Posted on 2007-07-23
6
Medium Priority
?
270 Views
Last Modified: 2013-12-17
This question pertains to C#
Hi,
Is there a function in C# to acess an XML file and store its contents (less the first line) to a string variable. I have achieved this in a windows application by copy/paste xml elements into a rich text box control. Then setting the text proprty of rtb control to a string variable:
//variable to hold the xml txt which is pasted into rich text box
string RtRcp= rtbProcessRtRcp.Text;

Is there a way for this to happen automatically without having to actually copy and paste into a rich text box? Is there a function that will access the file and copy its contents (elements only) into a string variable??

Thanks
0
Comment
Question by:Saxitalis
  • 3
  • 3
6 Comments
 
LVL 7

Expert Comment

by:davidwainwright
ID: 19546168
I use:

                string inputXml;
                using (StreamReader inputQueryReader = new StreamReader(inputXmlQueryFilePath))
                {
                    inputXml = inputQueryReader.ReadToEnd();
                }

where I've entered inputXmlQueryFilePath as an argumnet which is a path to a designated XML file, though obviously this is down to preference.

Hope this helps,
Dave
0
 

Author Comment

by:Saxitalis
ID: 19546398
Thanks for the reply Dave...

I'm not sure how to add the reference to the StreamReader class in my project. Would this be a COM??
0
 
LVL 7

Expert Comment

by:davidwainwright
ID: 19546421
Hi Sax

StreamReader is a class in System.IO - you just need to add the namespace of the right library - by default, you should have at the top of your code 'using System;' - add a line below this as 'using System.IO;' (no apostrophies!!); now in your main routine, you should be able to type 'Stre' and the intellisense will automatically sense this new class being added...

Thanks,

Dave
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Saxitalis
ID: 19546796
Thanks Dave! This works great...

Do you know how I can get rid of the first two lines of my Xml file? In other words, have the first two lines parsed out of the file when saved to a string variable.

Ex. I don't want these lines in from the Xml file in the variable:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
- <!-- Original source: C:\EHarvest\ObserverExportFiles\Text Files\SPCOMP.txt
  -->
Thanks,
Sax
 
0
 
LVL 7

Accepted Solution

by:
davidwainwright earned 1500 total points
ID: 19547116
Hi Sax

You cant ever actually delete the contents of the file in-situ; for example, when you edit a file using notepad, it parses the existing file into memory, does the operation required then overwrites the original file.

I'm guessing the best way forward might be for you to look at the ReadLine class as an alternative to the StreamReader, then attach a counter to loop through the lines and then pull the first two??

Dave
0
 

Author Comment

by:Saxitalis
ID: 19547437
Ok Thanks Dave
  I'll play with the Readline class and see if I can get something to work.

Sax
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month18 days, 8 hours left to enroll

826 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question