0

I am trying to parse an xml file But facing error as output.dat file could not be found. Firstly I read all xml code in a string and then load it into xmlDocument object but face exception that output.dat not found here is my code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.XPath;
using System.IO;
namespace XML
{
    class Program
{
     static void Main(string[] args)
     {
        try
        {
            StreamReader sr = new StreamReader("output.xml");
            String xml = "";
            String line = sr.ReadLine();
            while (line != null)
            {
                xml += line;
                line = sr.ReadLine();
            }
            sr.Close();
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(xml);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
    }
}

}

Kindly tell me where is error

3
  • When I debug my code xml variable contains all xml from output.xml but got execption at load method Commented Nov 27, 2013 at 16:33
  • Why not just use the Load method to read to file directly? What is the exact exception message? Typically it will indicate what is wrong with the file. Commented Nov 27, 2013 at 16:43
  • LoadXml is for loading from a file. You want what Pooli said below. Commented Nov 27, 2013 at 16:46

1 Answer 1

2

Consider replacing

   XmlDocument xdoc = new XmlDocument();
   xdoc.LoadXml(xml);

With

XDocument xml = XDocument.Parse(xml)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.