---
title: "Extract XML Data from String"  
description: "Extract XML Data from String"  
author: "Anonymous User"  
published: 2014-01-27  
updated: 2014-01-27  
canonical: https://www.mindstick.com/forum/1878/extract-xml-data-from-string  
category: "c#"  
tags: ["c#"]  
reading_time: 1 minute  

---

# Extract XML Data from String

I am having a input [stream](https://yourviews.mindstick.com/view/88509/us-intercepts-iranian-tanker-m-t-stream-amid-hormuz-tensions) which is generated when I [upload](https://www.mindstick.com/forum/12860/how-to-upload-file-asynchronously-using-generic-handler) a file(XML Type). I need the [XML data](https://www.mindstick.com/forum/149/problem-in-showing-the-xml-data-in-table-form-on-browser) at [code behind](https://www.mindstick.com/forum/2199/call-javascript-s-function-from-code-behind). I am having the xml data in [string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) by using

[StreamReader](https://www.mindstick.com/forum/161576/explain-the-difference-between-streamreader-and-file-readalllines) stream = new StreamReader(Request.InputStream);

string x = stream.ReadToEnd();

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

------WebKitFormBoundary8na5dBbHc4ydfxVU

[Content](https://www.mindstick.com/articles/13019/get-the-best-content-marketing-pricing-packages-for-your-brand)-Disposition: form-data; name="MyFile"; [filename](https://www.mindstick.com/interview/23463/difference-between-include-filename-and-include-filename)="Test 123.vfc"

Content-Type: [application](https://www.mindstick.com/articles/12824/calculator-application-in-android)/octet-stream

at the end of the string

------WebKitFormBoundary8na5dBbHc4ydfxVU--

This data is not [required](https://www.mindstick.com/forum/160269/what-is-required-data-annotation-how-does-it-affect-model-validation) for me.

Please help me in getting the right XML String.

## Replies

### Reply by Pravesh Singh

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;


---

Original Source: https://www.mindstick.com/forum/1878/extract-xml-data-from-string

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
