none
How to read XML file into string?

    Question

  • I have XML files stored on C:\ which im trying to read as string. I used the following code, but it does not read the XML declaration

    XDocument doc = new XDocument("C:\MyXML.xml");
    string xml = doc.ToString();
    I want the fast possible way which can read XML from file with XML declaration and ommiting white spaces if any
    Wednesday, April 13, 2011 3:50 AM

Answers

  • I have XML files stored on C:\ which im trying to read as string.

     

    I want the fast possible way which can read XML from file with XML declaration and ommiting white spaces if any

    The fastest should be using XmlReader and XmlWriter as in the following code sample:

        static void Main(string[] args)
        {
          string xml = GetXml(@"file.xml");
        }
    
        static string GetXml(string url)
        {
          using (XmlReader xr = XmlReader.Create(url, new XmlReaderSettings() { IgnoreWhitespace = true }))
          {
            using (StringWriter sw = new StringWriter())
            {
              using (XmlWriter xw = XmlWriter.Create(sw))
              {
                xw.WriteNode(xr, false);
              }
              return sw.ToString();
            }
          }
        }
    

     

     


    MVP Data Platform Development My blog
    • Marked as answer by lax4u Wednesday, April 13, 2011 8:19 PM
    Wednesday, April 13, 2011 10:17 AM
  • OK but did you try it like this:

    XDocument doc = new XDocument.Load("C:\MyXML.xml");

    string xml = doc.Declaration.ToString() + doc.ToString(SaveOptions.DisableFormatting);

    Fernando


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by lax4u Wednesday, April 13, 2011 8:19 PM
    Wednesday, April 13, 2011 4:33 PM

All replies

  • The statement should be:

    XDocument doc = new XDocument.Load("C:\MyXML.xml");


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, April 13, 2011 4:19 AM
  • Hi lax4u;

    Try it like this.

    XDocument doc = new XDocument.Load("C:\MyXML.xml");
    
    string xml = doc.Declaration.ToString() + doc.ToString(SaveOptions.DisableFormatting);
    
    

    Fernando
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Wednesday, April 13, 2011 4:41 AM
  • I have XML files stored on C:\ which im trying to read as string.

     

    I want the fast possible way which can read XML from file with XML declaration and ommiting white spaces if any

    The fastest should be using XmlReader and XmlWriter as in the following code sample:

        static void Main(string[] args)
        {
          string xml = GetXml(@"file.xml");
        }
    
        static string GetXml(string url)
        {
          using (XmlReader xr = XmlReader.Create(url, new XmlReaderSettings() { IgnoreWhitespace = true }))
          {
            using (StringWriter sw = new StringWriter())
            {
              using (XmlWriter xw = XmlWriter.Create(sw))
              {
                xw.WriteNode(xr, false);
              }
              return sw.ToString();
            }
          }
        }
    

     

     


    MVP Data Platform Development My blog
    • Marked as answer by lax4u Wednesday, April 13, 2011 8:19 PM
    Wednesday, April 13, 2011 10:17 AM
  • I have actually used XDocument doc = new XDocument.Load("C:\MyXML.xml"); it was just typo here.

    Wednesday, April 13, 2011 2:10 PM
  • I have actually used XDocument doc = new XDocument.Load("C:\MyXML.xml"); it was just typo here.


    Load is static method, it should be called as

      XDocument doc = XDocument.Load("file.xml");

    the "new" in "new XDocument.Load("file.xml")" does not make sense.

     


    MVP Data Platform Development My blog
    Wednesday, April 13, 2011 4:29 PM
  • OK but did you try it like this:

    XDocument doc = new XDocument.Load("C:\MyXML.xml");

    string xml = doc.Declaration.ToString() + doc.ToString(SaveOptions.DisableFormatting);

    Fernando


    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    • Marked as answer by lax4u Wednesday, April 13, 2011 8:19 PM
    Wednesday, April 13, 2011 4:33 PM