• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 278
  • Last Modified:

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

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
Saxitalis
Asked:
Saxitalis
  • 3
  • 3
1 Solution
 
davidwainwrightCommented:
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
 
SaxitalisAuthor Commented:
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
 
davidwainwrightCommented:
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
SaxitalisAuthor Commented:
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
 
davidwainwrightCommented:
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
 
SaxitalisAuthor Commented:
Ok Thanks Dave
  I'll play with the Readline class and see if I can get something to work.

Sax
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now