EXTRACT XML DATA FROM STRING

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
C# 
C#
 1214  View(s)
Ratings:
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

------WebKitFormBoundary8na5dBbHc4ydfxVU

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

Content-Type: application/octet-stream

at the end of the string

------WebKitFormBoundary8na5dBbHc4ydfxVU--

This data is not required for me.

Please help me in getting the right XML String.

  1. Pravesh Singh

    Post:412

    Points:2888
    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;

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!