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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:02:17+00:00 2026-05-19T09:02:17+00:00

I am trying to write the following into an XML document: <html xmlns:o=urn:schemas-microsoft-com:office:office xmlns:x=urn:schemas-microsoft-com:office:excel

  • 0

I am trying to write the following into an XML document:

<html xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns="http://www.w3.org/TR/REC-html40">

    <head>
     <xml> 
      <x:ExcelWorkbook>
       <x:ExcelWorksheets>
        <x:ExcelWorksheet>
         other code here
        </x:ExcelWorksheet>
       </x:ExcelWorksheets>
      </x:ExcelWorkbook>
     </xml>
    </head>
   </html> 

However, if I use the following code, it strips out the ‘x:’.

System.Xml.XmlDocument document = new System.Xml.XmlDocument();

System.Xml.XmlElement htmlNode = document.CreateElement("html");
htmlNode.SetAttribute("xmlns:o", "urn:schemas-microsoft-com:office:office");
htmlNode.SetAttribute("xmlns:x", "urn:schemas-microsoft-com:office:excel");
htmlNode.SetAttribute("xmlns", "http://www.w3.org/TR/REC-html40");
document.AppendChild(htmlNode);

System.Xml.XmlElement headNode = document.CreateElement("head");
htmlNode.AppendChild(headNode);
headNode.AppendChild(
  document.CreateElement("xml")).AppendChild(
  document.CreateElement("x:ExcelWorkbook"))).AppendChild(
  document.CreateElement("x:ExcelWorksheets")).AppendChild(
  document.CreateElement("x:ExcelWorksheet")).InnerText="other code here";

How can I stop this from happening?

  • 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-19T09:02:18+00:00Added an answer on May 19, 2026 at 9:02 am

    Use the XmlDocument.CreateElement overload which allows you to specify the required prefix & namespace of the element e.g.

    document.CreateElement("x", "ExcelWorkbook", "urn:schemas-microsoft-com:office:excel");
    

    Example

    From your comment I think you have went ahead and create every element in the way I have suggested. What I was referring to was how to get your Excel specific elements to have the correct namespace prefix. See my example below to see how to properly associate each of your elements with the correct namespace prefix:

    // store ns in a local variable as it is going to be re-used
    const string HTML_NS = "http://www.w3.org/TR/REC-html40";
    const string EXCEL_NS = "urn:schemas-microsoft-com:office:excel";
    
    XmlDocument doc = new XmlDocument();
    // create the HTML element and set the HTML namespace as the default
    XmlElement htmlElement = doc.CreateElement("html", HTML_NS);
    // add the office/excel namespaces into the HTML element (so we can reference them later)
    htmlElement.SetAttribute("xmlns:o", "urn:schemas-microsoft-com:office:office");
    htmlElement.SetAttribute("xmlns:x", EXCEL_NS);
    doc.AppendChild(htmlElement);
    // associate the HEAD element with the HTML namespace as this is a HTML element
    XmlElement headElement = doc.CreateElement("head", HTML_NS);    
    htmlElement.AppendChild(headElement);
    // now this is the one I am not too sure about as I don't know which 
    // namespace you would associate the XML tag with. If you run the code 
    // as it is specified here it will write out as <xml xmlns=""> which is
    // correct as we aren't associating it with a namespace. As a workaround 
    // you could associate it with the HTML NS if it is just for writing out 
    XmlElement xmlElement = doc.CreateElement("xml", HTML_NS);   
    headElement.AppendChild(xmlElement);
    // create ExcelWorkbook element and associate with the excel namespace
    // Unlike the HTML/HEAD tags we need to supply the prefix here because the Excel
    // namespace is not the default
    XmlElement xlWorkbookElement = doc.CreateElement("x", "ExcelWorkbook", EXCEL_NS);
    xmlElement.AppendChild(xlWorkbookElement);
    // create ExcelWorksheets element and associate with the excel namespace
    XmlElement xlWorksheetsElement = doc.CreateElement("x", "ExcelWorksheets", EXCEL_NS);
    xlWorkbookElement.AppendChild(xlWorksheetsElement);
    // create the ExcelWorksheet element and associate with the excel namespace
    XmlElement xlWorksheetElement = doc.CreateElement("x", "ExcelWorksheet", EXCEL_NS);
    xlWorksheetsElement.AppendChild(xlWorksheetElement);
    

    Hope that clears things up for you.

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

Sidebar

Related Questions

I am trying to use the following code to write data into an excel
I'm trying to write a Linq2XML query to query the following XML. I need
i have the following xslt sheet: <?xml version=1.0 encoding=UTF-8 ?> <xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform version=1.0> <xsl:variable
I am trying to write some content into an XML file, yet I do
I'm trying to read an xml file into memory, add a node, then write
Newbie to LINQ, and trying to write the following query... select f.Section_ID, f.Page_ID, f.SortOrder,
I'm trying to write a piece of code that will do the following: Take
I am trying to write a C++ program that takes the following inputs from
I have been trying to write a regex that will remove whitespace following a
Currently I'm trying to write my first Python library and I've encountered the following

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.