473,589 Members | 2,649 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Create XML element with a namespace prefix

Hi,

I am having trouble with creation of XML programmaticall y using .NET. Specifically Im trying to create an element which looks like below and insert into an existing xml doc:
<Worksheet ss:Name="TKCShe et1">
</Worksheet>

The existing xml doc is:

<?xml version="1.0"?>
<?mso-application progid="Excel.S heet"?>
<Workbook xmlns="urn:sche mas-microsoft-com:office:spre adsheet"
xmlns:o="urn:sc hemas-microsoft-com:office:offi ce"
xmlns:x="urn:sc hemas-microsoft-com:office:exce l"
xmlns:ss="urn:s chemas-microsoft-com:office:spre adsheet"
xmlns:html="htt p://www.w3.org/TR/REC-html40">

</Workbook>

I had no problem using LoadXml() to read this existing xml into m_XmlDoc.

But the code I am using for creating that Worksheet node (as explained above):

// add new worksheet xml node at the end of the document
XmlElement wsNode = m_XmlDoc.Create Element("Worksh eet");

XmlAttribute wsName = m_XmlDoc.Create Attribute("ss:N ame");
//XmlAttribute wsName = m_XmlDoc.Create Attribute("ss", "Name", XmlNsMgr.Lookup Namespace("ss") ); //- this did not work either :(

wsName.Value = ?TKCSheet1?;
wsNode.Attribut es.Append(wsNam e);
m_XmlDoc.Docume ntElement.Appen dChild(wsNode);
But this code gives me:
<Worksheet Name="TKCSheet1 " xmlns="">
</Worksheet>

I want ss:Name NOT just Name, in my xml and also I do NOT want xmlns="" in that element!

Can somebody please help me out here?

Thanks,
Krishna
PS: I know probably using Excel 2003 .NET libraries might be cleaner, but I have limited to get this thing running.
--------------------------------
From: Krishna Tulasi

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>tq30o75pHke +OTwMr4tQXQ==</Id>
Nov 12 '05 #1
4 20481
"Krishna Tulasi via .NET 247" <an*******@dotn et247.com> wrote in message news:OA******** ******@TK2MSFTN GP12.phx.gbl...
// add new worksheet xml node at the end of the document
XmlElement wsNode = m_XmlDoc.Create Element("Worksh eet");
XmlAttribute wsName = m_XmlDoc.Create Attribute("ss:N ame"); : : But this code gives me:
<Worksheet Name="TKCSheet1 " xmlns="">
</Worksheet>

I want ss:Name NOT just Name, in my xml and also I do NOT
want xmlns="" in that element!


XmlElement wsNode = m_XmlDoc.Create Element(
null, // Prefix
"Worksheet" , // Tag name
"urn:schema s-microsoft-com:office:spre adsheet" // Default xmlns URI.
);

When tag prefix is null, you're saying that the QName for this XML
element should assume the default namespace URI. The default
namespace URI for the document is "urn:schema s-microsoft-com:
office:spreadsh eet," fine, but you must also tell CreateElement( )
the namespace URI explicitly. When the namespace URI argument
matches the current default namespace URI, .NET knows it doesn't
have to emit an xml namespace declaration. If you don't specify a
namespace URI, .NET follows your directions: you've told .NET
this element MUST have no tag prefix, the only way CreateElement( )
can reconcile this is by putting a default xml namespace declaration
on the element.
XmlAttribute wsName = m_XmlDoc.Create Attribute(
"ss", // Prefix
"Name", // Local Name
"urn:schema s-microsoft-com:office:spre adsheet" // xmlns URI for ss prefix.
);

Observe here that I tell CreateAttribute ( ) the prefix I want. It's going
to find, that it already knows about this prefix, as long as the namespace
URI you give it matches the namespace URI it knows about for this
prefix.
Derek Harmon
Nov 12 '05 #2
"Krishna Tulasi via .NET 247" <an*******@dotn et247.com> wrote in message news:OA******** ******@TK2MSFTN GP12.phx.gbl...
// add new worksheet xml node at the end of the document
XmlElement wsNode = m_XmlDoc.Create Element("Worksh eet");
XmlAttribute wsName = m_XmlDoc.Create Attribute("ss:N ame"); : : But this code gives me:
<Worksheet Name="TKCSheet1 " xmlns="">
</Worksheet>

I want ss:Name NOT just Name, in my xml and also I do NOT
want xmlns="" in that element!


XmlElement wsNode = m_XmlDoc.Create Element(
null, // Prefix
"Worksheet" , // Tag name
"urn:schema s-microsoft-com:office:spre adsheet" // Default xmlns URI.
);

When tag prefix is null, you're saying that the QName for this XML
element should assume the default namespace URI. The default
namespace URI for the document is "urn:schema s-microsoft-com:
office:spreadsh eet," fine, but you must also tell CreateElement( )
the namespace URI explicitly. When the namespace URI argument
matches the current default namespace URI, .NET knows it doesn't
have to emit an xml namespace declaration. If you don't specify a
namespace URI, .NET follows your directions: you've told .NET
this element MUST have no tag prefix, the only way CreateElement( )
can reconcile this is by putting a default xml namespace declaration
on the element.
XmlAttribute wsName = m_XmlDoc.Create Attribute(
"ss", // Prefix
"Name", // Local Name
"urn:schema s-microsoft-com:office:spre adsheet" // xmlns URI for ss prefix.
);

Observe here that I tell CreateAttribute ( ) the prefix I want. It's going
to find, that it already knows about this prefix, as long as the namespace
URI you give it matches the namespace URI it knows about for this
prefix.
Derek Harmon
Nov 12 '05 #3
the CreateAttribute (string,string) method takes
a qualified name and a namespace URI.

http://msdn.microsoft.com/library/en...buteTopic2.asp

NOT a prefix.
in other words, THIS

XmlAttribute wsName =
m_XmlDoc.Create Attribute("Name ",
"urn:schema s-microsoft-com:office:spre adsheet");

NOT THIS:

m_XmlDoc.Create Attribute("Name ", "ss");
-Dino
"Krishna Tulasi via .NET 247" <an*******@dotn et247.com> wrote in message
news:OA******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

I am having trouble with creation of XML programmaticall y using .NET. Specifically Im trying to create an element which looks like below and
insert into an existing xml doc: <Worksheet ss:Name="TKCShe et1">
</Worksheet>

The existing xml doc is:

<?xml version="1.0"?>
<?mso-application progid="Excel.S heet"?>
<Workbook xmlns="urn:sche mas-microsoft-com:office:spre adsheet"
xmlns:o="urn:sc hemas-microsoft-com:office:offi ce"
xmlns:x="urn:sc hemas-microsoft-com:office:exce l"
xmlns:ss="urn:s chemas-microsoft-com:office:spre adsheet"
xmlns:html="htt p://www.w3.org/TR/REC-html40">

</Workbook>

I had no problem using LoadXml() to read this existing xml into m_XmlDoc.

But the code I am using for creating that Worksheet node (as explained above):
// add new worksheet xml node at the end of the document
XmlElement wsNode = m_XmlDoc.Create Element("Worksh eet");

XmlAttribute wsName = m_XmlDoc.Create Attribute("ss:N ame");
//XmlAttribute wsName = m_XmlDoc.Create Attribute("ss", "Name", XmlNsMgr.Lookup Namespace("ss") ); //- this did not work either :(
wsName.Value = ?TKCSheet1?;
wsNode.Attribut es.Append(wsNam e);
m_XmlDoc.Docume ntElement.Appen dChild(wsNode);
But this code gives me:
<Worksheet Name="TKCSheet1 " xmlns="">
</Worksheet>

I want ss:Name NOT just Name, in my xml and also I do NOT want xmlns="" in that element!
Can somebody please help me out here?

Thanks,
Krishna
PS: I know probably using Excel 2003 .NET libraries might be cleaner, but I have limited to get this thing running.

--------------------------------
From: Krishna Tulasi

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>tq30o75pHke +OTwMr4tQXQ==</Id>

Nov 12 '05 #4
the CreateAttribute (string,string) method takes
a qualified name and a namespace URI.

http://msdn.microsoft.com/library/en...buteTopic2.asp

NOT a prefix.
in other words, THIS

XmlAttribute wsName =
m_XmlDoc.Create Attribute("Name ",
"urn:schema s-microsoft-com:office:spre adsheet");

NOT THIS:

m_XmlDoc.Create Attribute("Name ", "ss");
-Dino
"Krishna Tulasi via .NET 247" <an*******@dotn et247.com> wrote in message
news:OA******** ******@TK2MSFTN GP12.phx.gbl...
Hi,

I am having trouble with creation of XML programmaticall y using .NET. Specifically Im trying to create an element which looks like below and
insert into an existing xml doc: <Worksheet ss:Name="TKCShe et1">
</Worksheet>

The existing xml doc is:

<?xml version="1.0"?>
<?mso-application progid="Excel.S heet"?>
<Workbook xmlns="urn:sche mas-microsoft-com:office:spre adsheet"
xmlns:o="urn:sc hemas-microsoft-com:office:offi ce"
xmlns:x="urn:sc hemas-microsoft-com:office:exce l"
xmlns:ss="urn:s chemas-microsoft-com:office:spre adsheet"
xmlns:html="htt p://www.w3.org/TR/REC-html40">

</Workbook>

I had no problem using LoadXml() to read this existing xml into m_XmlDoc.

But the code I am using for creating that Worksheet node (as explained above):
// add new worksheet xml node at the end of the document
XmlElement wsNode = m_XmlDoc.Create Element("Worksh eet");

XmlAttribute wsName = m_XmlDoc.Create Attribute("ss:N ame");
//XmlAttribute wsName = m_XmlDoc.Create Attribute("ss", "Name", XmlNsMgr.Lookup Namespace("ss") ); //- this did not work either :(
wsName.Value = ?TKCSheet1?;
wsNode.Attribut es.Append(wsNam e);
m_XmlDoc.Docume ntElement.Appen dChild(wsNode);
But this code gives me:
<Worksheet Name="TKCSheet1 " xmlns="">
</Worksheet>

I want ss:Name NOT just Name, in my xml and also I do NOT want xmlns="" in that element!
Can somebody please help me out here?

Thanks,
Krishna
PS: I know probably using Excel 2003 .NET libraries might be cleaner, but I have limited to get this thing running.

--------------------------------
From: Krishna Tulasi

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>tq30o75pHke +OTwMr4tQXQ==</Id>

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3286
by: Timo Nentwig | last post by:
Hi! I'm new to XML schemas; can somebody explain why bar is undefined? <xs:element name="bar"> <xs:complexType> <xs:simpleContent> <xs:extension base="xs:string"> <xs:attribute name="id" type="xs:string" use="required" /> </xs:extension>
4
2387
by: Gordon Dickens | last post by:
I have target xml to generate from schema. All of the XML instances have the same global element i.e. <base>. I would like to combine all of the schemas into a single schema where I could generate any of the specific instances. sample schema one: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"> <xs:element name="base">
4
2615
by: Hollywood | last post by:
I'm using XML serialization to produce the following XML document: <TestDoc xmlns:srd="some-url"> <Additional> <Security> <srd:Login>login_id</srd:Login> <srd:Password>password</srd:Password> </Security> </Additional> </TestDoc>
7
5199
by: pintihar | last post by:
Hi, As a follow on from an earlier post I have another question about xslt. Is it possible to create the stylsheet programatically? Is this sensible? In the first phase I needed to map element name from inbound xml to my internal elements to standardize disparate input. Now I could just create an xslt stylesheet for each possible inbound format and be done, but I think it would be powerful to be able store this mapping in a database and...
6
2575
by: Martin | last post by:
Hi, I have a xml file like the one below <?xml version="1.0" encoding="utf-8"?><e1 xmlns:e1="http://tempuri.org/Source1.xsd" e1:att1="1" e1:att2="2" e1:rest="345"/> If I try to create a schema for it with Visual Studio, I get the error "Failed to create a schema for this data file because:
4
7286
by: BizTalk Benjamin | last post by:
Hi, I have an XmlDocument loaded from a memory stream. I set the document element prefix in this way XmlElement e = xDoc.DocumentElement; e.Prefix = "abc" When i simply write the document element to the command line it shows the prefix. However, it does not persist in the XmlDoc so when i save the XmlDoc into a file, the prefix is no longer there.
16
3511
by: TT (Tom Tempelaere) | last post by:
Hi all, I created an XSD to define the structure of an XML file for my project. I made an XML file linked to the XSD using XmlSpy. The problem is that if I read the file using .NET XmlDocument and then query for the root element, the result is always null (1). However if I strip the root element of all attributes generated by XmlSpy, then there is no problem to find the root element with .NET XML classes (2). (1) The XML for which...
9
6466
by: Mark Olbert | last post by:
I'm trying to serialize (using XmlSerializer.Serialize) a class that I generated from an XSD schema using XSD.EXE /c. The problem I'm running into is that the root element needs to be unqualified, and the default namespace needs to be included on it as an attribute. The schema I'm using is this: <xs:schema xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:html="http://www.w3.org/TR/REC-html40"...
13
8147
by: Bill Nguyen | last post by:
Is it possible to create your won XSD to use with .NET based on an XML content? For example the one below: <?xml version="1.0"?> <pcats:FuelsDoc xmlns="http://www.naxml.org/Retail-EDI/Vocabulary/2003-10-16" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="NAXML-FuelPrice15.xsd"> <pcats:TransmissionHeader>
0
7865
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8233
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8360
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7990
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
5731
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5399
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3849
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3889
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1461
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.