473,772 Members | 3,672 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 20503
"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
3298
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
2395
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
2630
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
5208
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
2590
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
7294
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
3529
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
6480
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
8165
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
9620
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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
9912
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8934
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6715
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
5354
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
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.