473,791 Members | 3,105 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

xmlns="" in created elements

In the XML document I'm trying to create I do the following

elem = _doc.CreateElem ent("Author");
elem.InnerText = "something" ;
parentElem.Appe ndChild(elem);

Thiw works properly however the resulting XML file has the following

<Author xmlns="">someth ing</Author>

what I'm trying to understand is why the xmlns="" is added...what I'm trying
to get is just
<Author>somethi ng</Author>

I'm sure I've just done something silly or not set some attribute that I
need to use here.
Thanks
--
Neil
Nov 12 '05 #1
5 12443


NeilL wrote:
In the XML document I'm trying to create I do the following

elem = _doc.CreateElem ent("Author");
elem.InnerText = "something" ;
parentElem.Appe ndChild(elem);

Thiw works properly however the resulting XML file has the following

<Author xmlns="">someth ing</Author>


If you use CreateElement(" Author") then you are creating an element in
no namespace. If you insert the element as a child of some element that
is in a namespace then the serializer has to add the xmlns="" to make
sure the proper markup is created.

If you want to create an element in some namespace then you need to
provide the namespace URI to the CreateElement method e.g.
doc.CreateEleme nt("Author", "http://example.com/2005/whatever")
or perhaps you want
doc.CreateEleme nt("Author", parentElem.Name spaceURI);

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
What I want it to use is the default for the document. So how would that be
specified... by useing the DocumentElement .NamespaceURI ?
--
Neil
"Martin Honnen" wrote:


NeilL wrote:
In the XML document I'm trying to create I do the following

elem = _doc.CreateElem ent("Author");
elem.InnerText = "something" ;
parentElem.Appe ndChild(elem);

Thiw works properly however the resulting XML file has the following

<Author xmlns="">someth ing</Author>


If you use CreateElement(" Author") then you are creating an element in
no namespace. If you insert the element as a child of some element that
is in a namespace then the serializer has to add the xmlns="" to make
sure the proper markup is created.

If you want to create an element in some namespace then you need to
provide the namespace URI to the CreateElement method e.g.
doc.CreateEleme nt("Author", "http://example.com/2005/whatever")
or perhaps you want
doc.CreateEleme nt("Author", parentElem.Name spaceURI);

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Nov 12 '05 #3


NeilL wrote:
What I want it to use is the default for the document. So how would that be
specified... by useing the DocumentElement .NamespaceURI ?


If you regard doc.DocumentEle ment.NamespaceU RI as the "default for the
document" then pass that to CreateElement.
In my understanding there is no such thing as the default for the
document as you can have structures like
<element xmlns="http://example.com/ns1">
<element xmlns="http://example.com/ns2">
<element xmlns="http://example.com/n3" />
</element>
</element>
where the default namespace is declared and redeclared at different levels.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #4
NeilL wrote:
What I want it to use is the default for the document. So how would that be
specified... by useing the DocumentElement .NamespaceURI ?


Does your Document use no Namespaces? Like:

<root>
<elem>
<Author>Joe</Author>
</elem>
</root>

If you just never use the "Namespaced "-Overloads of CreateElement, you
will get this Document.

If you don't specify explicit prefixes (using CreateElement-Overloads)
your document may look like this:

<root> <!-- empty xmlns -->
<elem xmlns="foo">
<bar /> <!-- foo is in xmlns "foo" -->
<Author xmlns="">Joe</Author>
</elem>
</root>

But if you specify prefixes it looks like this:

<root> <!-- empty xmlns -->
<a:elem xmlns:a="foo">
<bar /> <!-- empty xmlns -->
<Author>Joe</Author>
</a:elem>
</root>

Xml Namespaces are "inherited" through the structure, see
<http://www.w3.org/TR/REC-xml-names/#scoping-defaulting>
--
Pascal Schmitt
Nov 12 '05 #5
Ok, Thanks, I thnk I get it (close to getting it in any case).

Using
doc.CreateEleme nt("Author", parentElem.Name spaceURI);
fixed the problem that I was having.

Thanks

--
Neil
"Pascal Schmitt" wrote:
NeilL wrote:
What I want it to use is the default for the document. So how would that be
specified... by useing the DocumentElement .NamespaceURI ?


Does your Document use no Namespaces? Like:

<root>
<elem>
<Author>Joe</Author>
</elem>
</root>

If you just never use the "Namespaced "-Overloads of CreateElement, you
will get this Document.

If you don't specify explicit prefixes (using CreateElement-Overloads)
your document may look like this:

<root> <!-- empty xmlns -->
<elem xmlns="foo">
<bar /> <!-- foo is in xmlns "foo" -->
<Author xmlns="">Joe</Author>
</elem>
</root>

But if you specify prefixes it looks like this:

<root> <!-- empty xmlns -->
<a:elem xmlns:a="foo">
<bar /> <!-- empty xmlns -->
<Author>Joe</Author>
</a:elem>
</root>

Xml Namespaces are "inherited" through the structure, see
<http://www.w3.org/TR/REC-xml-names/#scoping-defaulting>
--
Pascal Schmitt

Nov 12 '05 #6

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

Similar topics

3
10043
by: Mike Dickens | last post by:
hi, i'm sure this has come up before but havn't managed to find an answer. if i have the following xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet method="xml" version="1.0" xmlns:ns1="abc" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output indent="yes" media-type="text/xml" standalone="yes" version="1.0"/> <xsl:template match="/">
0
1310
by: johnsocs | last post by:
All I'm trying to write an xml schema for the following xml from the google web service api. In the schema I'm not sure how to describe the soapenv:encodingStyle attribute. Thanks. <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
1
2091
by: Zhenya Sigal via .NET 247 | last post by:
I have the following code: XmlElement parent= m_xmlDoc.CreateElement("parent", "http://tempuri.org/myns"); parent.InnerXml = "<child1>text</child1><child2>text</child2>"; "http://tempuri.org/myns" is declared as the default namespace for this xml file. When I set InnerXml above, each <child> node gets the following extra attribute: xmlns="". I'd like child nodes to inherit the default namespace instead of the empty namespace. How can I...
3
7144
by: Keith Hill | last post by:
I am creating an XmlDocument in code and then using XmlTextWriter via doc.WriteTo(xwriter) to output the result to a text box. I have a root element that defines a default namespace. However, the sub-elements are created without any namespaces like so: XmlElement elem = doc.CreateElement("Foo"); root.AppendChild(elem); but in the output I get this:
6
13598
by: TS | last post by:
Hi, i have a problem validating xml against schema. I used http://apps.gotdotnet.com/xmltools/xsdvalidator/Default.aspx validator and it says it is fine. Can you tell me why this doesn't work? Thanks! Schema: <?xml version="1.0"?> <xs:schema id="ReportInfo" targetNamespace="http://tempuri.org/Reports.xsd"
2
3527
by: Andy Fish | last post by:
Hi, I have discovered that if I use this syntax: public string bar() { return "hello, world"; } then it is not possible to call the web service using the MS soap toolkit.
0
1598
by: R. Ian Lee | last post by:
I've built an XSLT file that transforms data to SpreadsheetML format. The XSLT uses a <xsl:call-template/to build each worksheet. For some reason, when I transform the file, it is inserting xmlns="" attributes into my <Worksheetelements which causes the worksheet not to work with Excel. I'm using the System.Xml.Xsl.XslTransform class to perform the transform. Here's a snippet of the XSLT template that is called: <xsl:template...
4
4821
by: BorisBoshond | last post by:
Hi all, Hope someone is able and willing to help me with following problem. I received a xsd file from another company, our company i supposed to return xml based on that xsd. Problem is that I don't really understand how these namespace work in xml. (I am however aware of what problems namespaces solve) I'm not even sure if the provided xsd is 'common' practice, although it validates correctly. So I'll describe exactly what I've...
4
17895
markmcgookin
by: markmcgookin | last post by:
Hi, I am creating an MXL doc using XSLT but for some reason it is churning out elements like this <DateTimeLastSaved xmlns="" /> <UserName xmlns="" /> when I delete xmlns="" it works fine, but if I leave it in there are issues reading/saving the file. Is there any way to remove this xmlns="" in the xslt? Cheers,
0
9515
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,...
1
10155
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,...
0
9029
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...
1
7537
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
6776
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
5431
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
5559
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4110
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2916
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.