467,211 Members | 1,210 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,211 developers. It's quick & easy.

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


Jul 31 '06 #1
  • viewed: 10835
Share:
3 Replies


dhurwitz wrote:

<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
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.

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/
Jul 31 '06 #2
Thank you. That is very helpful.

--Dan

"Martin Honnen" wrote:
>

dhurwitz wrote:

<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"

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.

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/
Jul 31 '06 #3

"dhurwitz" <dh******@discussions.microsoft.comwrote in message
news:E8**********************************@microsof t.com...
Also I find it better to use the indent property of the writer, rather than
writing loads of whilespace newlines manually.
Thank you. That is very helpful.

--Dan

"Martin Honnen" wrote:
>>

dhurwitz wrote:

<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"

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.

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/
Aug 3 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by JemPower | last post: by
4 posts views Thread by vikas | last post: by
3 posts views Thread by Abdessamad Belangour | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.