Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6952389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:20:35+00:00 2026-05-27T14:20:35+00:00

I want to read all XML contents from a file. The code below only

  • 0

I want to read all XML contents from a file. The code below only works when the XML declaration (<?xml version="1.0" encoding="UTF-8"?>) is removed. What is the best way to read the file without removing the XML declaration?

XmlTextReader reader = new XmlTextReader(@"c:\my path\a.xml");
            reader.Read();
            string rs = reader.ReadOuterXml();

Without removing the XML declaration, reader.ReadOuterXml() returns an empty string.

<?xml version="1.0" encoding="UTF-8"?>  
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
  <s:Header>
    <a:Action s:mustUnderstand="1">http://www.as.com/ver/ver.IClaimver/Car</a:Action>
    <a:MessageID>urn:uuid:b22149b6-2e70-46aa-8b01-c2841c70c1c7</a:MessageID>
    <ActivityId CorrelationId="16b385f3-34bd-45ff-ad13-8652baeaeb8a" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">04eb5b59-cd42-47c6-a946-d840a6cde42b</ActivityId>
    <a:ReplyTo>
      <a:Address>http://www.w3.org/2005/08/addressing/anonymous</a:Address>
    </a:ReplyTo>
    <a:To s:mustUnderstand="1">http://localhost/ver.Web/ver2011.svc</a:To>
  </s:Header>
  <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Car xmlns="http://www.as.com/ver">
      <carApplication>
        <HB_Base xsi:type="HB" xmlns="urn:core">
          <Header>
            <Advisor>
              <AdvisorLocalAuthorityCode>11</AdvisorLocalAuthorityCode>
              <AdvisorType>1</AdvisorType>
            </Advisor>
          </Header>
          <General>
            <ApplyForHB>yes</ApplyForHB>
            <ApplyForCTB>yes</ApplyForCTB>
            <ApplyForFSL>yes</ApplyForFSL>
            <ConsentSupplied>no</ConsentSupplied>
            <SupportingDocumentsSupplied>no</SupportingDocumentsSupplied>
          </General>
        </HB_Base>
      </carApplication>
    </Car>
  </s:Body>
</s:Envelope>

Update

I know other methods that use NON-xml reader (e.g. by using File.ReadAllText()). But I need to know a way that uses an xml method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:20:36+00:00Added an answer on May 27, 2026 at 2:20 pm

    There can be no text or whitespace before the <?xml ?> encoding declaration other than a BOM, and no text between the declaration and the root element other than line break.

    Anything else is an invalid document.

    UPDATE:

    I think your expectation of XmlTextReader.read() is incorrect.

    Each call to XmlTextReader.Read() steps through the next “token” in the XML document, one token at a time. “Token” means XML elements, whitespace, text, and XML encoding declaration.

    Your call to reader.ReadOuterXML() is returning an empty string because the first token in your XML file is an XML declaration, and an XML declaration does not have an OuterXML.

    Consider this code:

        XmlTextReader reader = new XmlTextReader("test.xml");
        reader.Read();
        Console.WriteLine(reader.NodeType);  // XMLDeclaration
        reader.Read();
        Console.WriteLine(reader.NodeType);  // Whitespace
        reader.Read();
        Console.WriteLine(reader.NodeType);  // Element
        string rs = reader.ReadOuterXml();
    

    The code above produces this output:

    XmlDeclaration
    Whitespace
    Element
    

    The first “token” is the XML declaration.

    The second “token” encountered is the line break after the XML declaration.

    The third “token” encountered is the <s:Envelope> element. From here a call to reader.ReadOuterXML() will return what I think you’re expecting to see – the text of <s:Envelope> element, which is the entire soap packet.

    If what you really want is to load the XML file into memory as objects, just call
    var doc = XDocument.Load("test.xml")
    and be done with the parsing in one fell swoop.

    Unless you’re working with an XML doc that is so monstrously huge that it won’t fit in system memory, there’s really not a lot of reason to go poking through the XML document one token at a time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read line n1->n2 from file foo.c into the current buffer. I
I want to read an xml file, apply a transform, then write to another
I want to read UTF-8 input in Perl, no matter if it comes from
I want to read each line from a text file and store them in
I have the following xml structure : <?xml version=1.0 encoding=utf-8 ?> <nws> <url>http://cpa.hypotheses.org/feed</url> <url>http://news.ycombinator.com/rss</url>
I want to read a fairly large xml file. Its small enough to fit
I want to read all items of a feed in C#. The solutions I've
I have a text file. I want read that file. But In that if
I want to read the contents of a URL but don't want to hang
I want to read and write from serial using events/interrupts. Currently, I have it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.