Home > DeveloperSection > Forums > Extract XML Data from String
Ankita Pandey
Ankita Pandey

Total Post:183

Posted on    January-27-2014 2:43 AM

 C# C# 

 1 Reply(s)
 933  View(s)
Rate this:

I am having a input stream which is generated when I upload a file(XML Type). I need the XML data at code behind. I am having the xml data in string by using


 StreamReader stream = new StreamReader(Request.InputStream);

 string x = stream.ReadToEnd();

It also contains the following data at the start of the string


Content-Disposition: form-data; name="MyFile"; filename="Test 123.vfc"

Content-Type: application/octet-stream

at the end of the string


This data is not required for me.

Please help me in getting the right XML String.

Pravesh Singh

Total Post:412

Posted on    January-27-2014 3:11 AM

Hi Ankita,

First you can remove the first three lines and last line from your string.

int n = 3;

string[] lines = str.Split(Environment.NewLine.ToCharArray()).Skip(n).ToArray();

string output = string.Join(Environment.NewLine, lines);

output = output.Remove(str.LastIndexOf(Environment.NewLine));

In your XML string if you don't have a root node then add it like following.

   string xmlTxt  = "<ROOT>" + xmlString + "</ROOT>";

If you have a root node skip above. For a well format XML string you can just use below code

   XmlDocument xmlDocument = new XmlDocument();

   xmlDocument.InnerXml = xmlTxt;

Don't want to miss updates? Please click the below button!

Follow MindStick