469,356 Members | 1,994 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

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 20171
"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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Timo Nentwig | last post: by
4 posts views Thread by Gordon Dickens | last post: by
4 posts views Thread by Hollywood | last post: by
7 posts views Thread by pintihar | last post: by
4 posts views Thread by BizTalk Benjamin | last post: by
16 posts views Thread by TT (Tom Tempelaere) | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.