Extract XML Data from String

Total Post:183


 1714  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.

  1. Re: Extract XML Data from String

    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;


Please check, If you want to make this post sponsored

You are not a Sponsored Member. Click Here to Subscribe the Membership.