473,396 Members | 2,020 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,396 software developers and data experts.

Create XML element with a namespace prefix

Hi,

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

The existing xml doc is:

<?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">

</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.CreateElement("Worksheet");

XmlAttribute wsName = m_XmlDoc.CreateAttribute("ss:Name");
//XmlAttribute wsName = m_XmlDoc.CreateAttribute("ss", "Name", XmlNsMgr.LookupNamespace("ss")); //- this did not work either :(

wsName.Value = ?TKCSheet1?;
wsNode.Attributes.Append(wsName);
m_XmlDoc.DocumentElement.AppendChild(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 20458
"Krishna Tulasi via .NET 247" <an*******@dotnet247.com> wrote in message news:OA**************@TK2MSFTNGP12.phx.gbl...
// add new worksheet xml node at the end of the document
XmlElement wsNode = m_XmlDoc.CreateElement("Worksheet");
XmlAttribute wsName = m_XmlDoc.CreateAttribute("ss:Name"); : : 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.CreateElement(
null, // Prefix
"Worksheet", // Tag name
"urn:schemas-microsoft-com:office:spreadsheet" // 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:schemas-microsoft-com:
office:spreadsheet," 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.CreateAttribute(
"ss", // Prefix
"Name", // Local Name
"urn:schemas-microsoft-com:office:spreadsheet" // 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*******@dotnet247.com> wrote in message news:OA**************@TK2MSFTNGP12.phx.gbl...
// add new worksheet xml node at the end of the document
XmlElement wsNode = m_XmlDoc.CreateElement("Worksheet");
XmlAttribute wsName = m_XmlDoc.CreateAttribute("ss:Name"); : : 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.CreateElement(
null, // Prefix
"Worksheet", // Tag name
"urn:schemas-microsoft-com:office:spreadsheet" // 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:schemas-microsoft-com:
office:spreadsheet," 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.CreateAttribute(
"ss", // Prefix
"Name", // Local Name
"urn:schemas-microsoft-com:office:spreadsheet" // 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.CreateAttribute("Name",
"urn:schemas-microsoft-com:office:spreadsheet");

NOT THIS:

m_XmlDoc.CreateAttribute("Name", "ss");
-Dino
"Krishna Tulasi via .NET 247" <an*******@dotnet247.com> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl...
Hi,

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

The existing xml doc is:

<?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">

</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.CreateElement("Worksheet");

XmlAttribute wsName = m_XmlDoc.CreateAttribute("ss:Name");
//XmlAttribute wsName = m_XmlDoc.CreateAttribute("ss", "Name", XmlNsMgr.LookupNamespace("ss")); //- this did not work either :(
wsName.Value = ?TKCSheet1?;
wsNode.Attributes.Append(wsName);
m_XmlDoc.DocumentElement.AppendChild(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.CreateAttribute("Name",
"urn:schemas-microsoft-com:office:spreadsheet");

NOT THIS:

m_XmlDoc.CreateAttribute("Name", "ss");
-Dino
"Krishna Tulasi via .NET 247" <an*******@dotnet247.com> wrote in message
news:OA**************@TK2MSFTNGP12.phx.gbl...
Hi,

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

The existing xml doc is:

<?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">

</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.CreateElement("Worksheet");

XmlAttribute wsName = m_XmlDoc.CreateAttribute("ss:Name");
//XmlAttribute wsName = m_XmlDoc.CreateAttribute("ss", "Name", XmlNsMgr.LookupNamespace("ss")); //- this did not work either :(
wsName.Value = ?TKCSheet1?;
wsNode.Attributes.Append(wsName);
m_XmlDoc.DocumentElement.AppendChild(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
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"...
4
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...
4
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>...
7
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...
6
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...
4
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...
16
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...
9
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,...
13
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.