473,320 Members | 2,048 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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
3 11150


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: JemPower | last post by:
Hi, I've created a routine that reads from my sql server and outputs the contents to an XML file. I have two problems. Firstly, if an error occurs (i.e. the user doesn't supply the correct...
4
by: vikas | last post by:
Hi I want to create following xml document using xmlwriter <?xml version="1.0" ?> - <epps> - <remit_trn type="technical"> <remit_date_dtm dtm = "201" >test</remit_date_dtm>...
3
by: Abdessamad Belangour | last post by:
Hi all, I have developped an application that generate XML documents. At most time it works fine, but sometimes (when the size of data to export is big) i got the error message : << The...
2
by: Mori | last post by:
I am using the following code to manufactue xml using xmltextwriter. The xml is okay except for the extra set of start document and the outer <string> element. Where do they come from and how do...
8
by: Marc Gravell | last post by:
I want to write a method that will accept a stream as a parameter, and which will write xml to the stream (based in reality on database results) using the XmlTextWriter class. However, this insists...
4
by: Paul Hadfield | last post by:
Hi, Wonder if anyone can help me on this, In DotNet2.0 I've been quite happily using the WriteAttributeString method of XmlWriter object, but have run into a problem when trying to output a...
1
by: remya1000 | last post by:
i'm using VB.NET and Microsoft Access. and in one of my Access table is called Emp.mdb. and in that Emp.mdb i have lots of tables like MD1, MD2,......MD58 and MB1,MB2,......MB67 and some other...
12
by: G.S. | last post by:
Is it acceptable to use { } blocks to improve readability and maintainability of the code? Example: //add the checkbox column DataGridViewCheckBoxColumn column = new...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.