Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 31st, 2006, 02:15 PM
dhurwitz
Guest
 
Posts: n/a
Default Writing xmlns using WriteAttributeString

I am trying to create an XML spreadsheet for use with Excel. I first saved a
simple spreadsheet as an XML file, and now I am trying to recreate that file
in code, using .NET 2.0. The beginning of the XML file should look like the
following:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">

I can create all of this except for the first xmlns attribute in the
Workbook element. Here is my code:

writer.WriteStartDocument();
writer.WriteWhitespace("\n");
writer.WriteRaw("<?mso-application progid=\"Excel.Sheet\"?>");
writer.WriteWhitespace("\n");
Writer.WriteStartElement("Workbook");
writer.WriteAttributeString("xmlns",
"urn:schemas-microsoft-com:office:spreadsheet");
writer.WriteAttributeString("xmlns", "o", null,
"urn:schemas-microsoft-com:office:office");
writer.WriteAttributeString("xmlns", "x", null,
"urn:schemas-microsoft-com:office:excel");
writer.WriteAttributeString("xmlns", "ss", null,
"urn:schemas-microsoft-com:office:spreadsheet");
writer.WriteAttributeString("xmlns", "html", null,
"http://www.w3.org/TR/REC-html40");
writer.WriteEndElement();

When the code gets to the first WriteAttributeString statement, it gives an
error "The prefix '' cannot be redefined to
'urn:schemas-microsoft-com:office:spreadsheet' within the same start element
tag." If I comment out that line, it works fine, minus the first attribute,
or course. I have tried every permutation I can think of for every oveload to
write an xmlns attribute withoug a local name, to no avail.

Any suggestions?

Thanks.
Dan




  #2  
Old July 31st, 2006, 02:35 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Writing xmlns using WriteAttributeString



dhurwitz wrote:

Quote:
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
Quote:
writer.WriteRaw("<?mso-application progid=\"Excel.Sheet\"?>");
That is a processing instruction so no need to force something with
WriteRaw, doing
writer.WriteProcessingInstruction("mso-application",
"progid=\"Excel.Sheet\"");
should suffice.

Quote:
Writer.WriteStartElement("Workbook");
writer.WriteAttributeString("xmlns",
"urn:schemas-microsoft-com:office:spreadsheet");
Write the element in the proper namespace, there is no need to
explicitly write an xmlns declaration, doing
writer.WriteStartElement("Workbook",
"urn:schemas-microsoft-com:office:spreadsheet");
suffices to have the writer add the xmlns declaration automatically.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #3  
Old July 31st, 2006, 02:55 PM
dhurwitz
Guest
 
Posts: n/a
Default Re: Writing xmlns using WriteAttributeString

Thank you. That is very helpful.

--Dan

"Martin Honnen" wrote:
Quote:
>
>
dhurwitz wrote:
>
>
Quote:
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
>
>
Quote:
writer.WriteRaw("<?mso-application progid=\"Excel.Sheet\"?>");
>
That is a processing instruction so no need to force something with
WriteRaw, doing
writer.WriteProcessingInstruction("mso-application",
"progid=\"Excel.Sheet\"");
should suffice.
>
>
Quote:
Writer.WriteStartElement("Workbook");
writer.WriteAttributeString("xmlns",
"urn:schemas-microsoft-com:office:spreadsheet");
>
Write the element in the proper namespace, there is no need to
explicitly write an xmlns declaration, doing
writer.WriteStartElement("Workbook",
"urn:schemas-microsoft-com:office:spreadsheet");
suffices to have the writer add the xmlns declaration automatically.
>
>
--
>
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
>
  #4  
Old August 3rd, 2006, 11:45 AM
Edward Wohlman
Guest
 
Posts: n/a
Default Re: Writing xmlns using WriteAttributeString


"dhurwitz" <dhurwitz@discussions.microsoft.comwrote in message
news:E8EFE641-4DE7-4F68-B516-03217BA565CB@microsoft.com...
Also I find it better to use the indent property of the writer, rather than
writing loads of whilespace newlines manually.


Quote:
Thank you. That is very helpful.
>
--Dan
>
"Martin Honnen" wrote:
>
Quote:
>>
>>
>dhurwitz wrote:
>>
>>
Quote:
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
>>
>>
Quote:
writer.WriteRaw("<?mso-application progid=\"Excel.Sheet\"?>");
>>
>That is a processing instruction so no need to force something with
>WriteRaw, doing
> writer.WriteProcessingInstruction("mso-application",
>"progid=\"Excel.Sheet\"");
>should suffice.
>>
>>
Quote:
Writer.WriteStartElement("Workbook");
writer.WriteAttributeString("xmlns",
"urn:schemas-microsoft-com:office:spreadsheet");
>>
>Write the element in the proper namespace, there is no need to
>explicitly write an xmlns declaration, doing
> writer.WriteStartElement("Workbook",
>"urn:schemas-microsoft-com:office:spreadsheet");
>suffices to have the writer add the xmlns declaration automatically.
>>
>>
>--
>>
>Martin Honnen --- MVP XML
>http://JavaScript.FAQTs.com/
>>
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles