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 8570359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T18:32:24+00:00 2026-06-11T18:32:24+00:00

XDocument xDocumentObject = XDocument.Parse(<Test>+ <elementx id=’1′ att=’aaa’ /> + <elementx id=’2′ att=’bbb’ /> +

  • 0
XDocument xDocumentObject = XDocument.Parse("<Test>"+
            "<elementx id='1'  att='aaa' />" +
            "<elementx id='2'  att='bbb' />" +
            "</Test>");

What is the difference between :

1)

byte[] xmlBytes = Encoding.Default.GetBytes(xDocumentObject.ToString());

AND

2)

byte[] xmlBytes;
using (MemoryStream ms = new MemoryStream())
{
   xDocumentObject.Save(ms);
   xmlBytes = ms.ToArray();
}

What is “Default” encoding in (1) and what is the encoding used in (2) and which is the preferred way?

  • 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-06-11T18:32:25+00:00Added an answer on June 11, 2026 at 6:32 pm

    In the first case you are using the encoding for the operating system’s current ANSI code page to convert the string to a byte array. The string represents the XML document generated by the XDocument instance and converted to a byte array. In .NET all strings are Unicode encoded.

    In the second example the encoding of the XDocument is used. So for example if you have the following XML <?xml encoding="utf-8"?> it will use UTF-8. The Declaration property allows you to specify the encoding being used:

    XDocument xDocumentObject = new XDocument(
        new XDeclaration("1.0", "utf-8", "yes"), 
        new XElement(
            "Test",
            new XElement("elementx", new XAttribute("id", "1"), new XAttribute("att", "aaa")), 
            new XElement("elementx", new XAttribute("id", "2"), new XAttribute("att", "bbb"))
        )
    );
    

    or:

    XDocument xDocumentObject = XDocument.Parse(
        @"<?xml version=""1.0"" encoding=""utf-8"" standalone=""yes""?>
        <Test>
            <elementx id=""1"" att=""aaa"" />
            <elementx id=""2"" att=""bbb"" />
        </Test>"
    );
    

    allows you to specify UTF-8 encoding.

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

Sidebar

Related Questions

var doc = XDocument.Parse(inputXML); this bombs out when amount node is not present. How
I have this code: XDocument xdoc = XDocument.Parse(xml); XElement xe = xdoc.Root.Element(program); string[] data
XDocument doc = XDocument.Parse(_data) XElement root = new XElement(student); doc.Element(marks).Add(root); doc.Save(_data) the _data is
I'm using XDocument and LINQ to parse some XML files, and some worked fine,
I have a Xdocument that is populated as follows: XDocument xDoc = XDocument.Parse(new StreamReader(response.GetResponseStream()).ReadToEnd());
I am trying to use XDocument.Parse(string s) to parse some XML that is being
using XDocument with xpath to parse XML which one is better in perfomance ?
I have an XDocument object and the ToString() method returns XML without any indentation.
I would like to query an XDocument object for a given path, (e.g. /path/to/element/I/want)
I'm using XDocument to cache a list of files. <file id=20 size=244318208>a file with

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.