Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 4th, 2008, 06:22 AM
Authorized User
 
Join Date: Nov 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default XmlWriter and default namespace

Hi to all,

I want to create a document with a namespace like this:

<AuditFile xmlns="urn:OECD:StandardAuditFile-Tax:PT_1.00_01">

with this code:

writer.WriteStartElement("AuditFile");
writer.WriteAttributeString("xmlns", null, null, "urn:OECD:StandardAuditFile-Tax:PT_1.00_01");


But the environment tells me that I cannot redefine the prefix '' from '' to 'urn:OECD:StandardAuditFile-Tax:PT_1.00_01' inside the same tag of the initial element.

I found that

writer.WriteStartElement("AuditFile");
writer.WriteAttributeString("xmlns", "x", null, "urn:OECD:StandardAuditFile-Tax:PT_1.00_01");


gives no problem, but the output is like this:

<AuditFile xmlns:x="urn:OECD:StandardAuditFile-Tax:PT_1.00_01">

I found this information in "o'Reilly XML XSLT XPath Pocket Reference 2nd edition":

In Example 1.1, we scoped each tag with the OReilly name-space. Namespaces are declared using the
xmlns:something attribute, where something defines the prefix of the name-space. The attribute
value is a unique identifier that differentiates this namespace from all other namespaces; the use of a
URI is recommended. In this case, we use the O'Reilly URI http://www.oreilly.com as the default
namespace, which should guarantee uniqueness. A namespace declaration can appear as an attribute
of any element, in which case the namespace remains inside that element's opening and closing tags.
Here are some examples:
<OReilly:Books xmlns:OReilly='http://www.oreilly.com'>
...
</OReilly:Books>
<xsl:stylesheet xmlns:xsl='http://www.w3.org'>
...
</xsl:stylesheet>


 
Old December 4th, 2008, 07:02 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Try the following:

Code:
writer.WriteAttributeString("xmlns", "", "http://www.w3.org/2000/xmlns/", "urn:OECD:StandardAuditFile-Tax:PT_1.00_01");

/- Sam Judson : Wrox Technical Editor -/
 
Old December 4th, 2008, 07:04 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

No actually, that doesn't match up with the error you are getting.

What you should really be doing is this:

Code:
writer.WriteStartElement("AuditFile", "urn:OECD:StandardAuditFile-Tax:PT_1.00_01");
I suspect your error is because this is not the initial element in your XML stream.

/- Sam Judson : Wrox Technical Editor -/
 
Old December 4th, 2008, 07:16 AM
Authorized User
 
Join Date: Nov 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

The first system gives the error again, and the second system creates the following structure:

<AuditFile>
  <xmlns xmlns="urn:OECD:StandardAuditFile-Tax:PT_1.00_01">

What I really want to write is:

<AuditFile xmlns="urn:OECD:StandardAuditFile-Tax:PT_1.00_01">






 
Old December 4th, 2008, 07:25 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Quote:
quote:Originally posted by miguel.ossa
 Hello,

The first system gives the error again, and the second system creates the following structure:

<AuditFile>
<xmlns xmlns="urn:OECD:StandardAuditFile-Tax:PT_1.00_01">


[/blue][/size=1]
I find that very hard to believe. Can you the complete code for that part of the document? It can't create an xmlkns element that you haven't asked it to write.



--

Joe (Microsoft MVP - XML)
 
Old December 4th, 2008, 07:25 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

I don't see the error you are getting with the second code segment. My complete code is as follows, and produces exactly the output you ask for:
Code:
            writer.WriteStartDocument();
            writer.WriteStartElement("AuditFile", "urn:OECD:StandardAuditFile-Tax:PT_1.00_01");
            writer.WriteEndElement();
            writer.WriteEndDocument();
If you are still not getting this then it must be something about the way you are setting up your XmlTextWriter, or some of the other code you are writing.

/- Sam Judson : Wrox Technical Editor -/
 
Old December 4th, 2008, 07:46 AM
Authorized User
 
Join Date: Nov 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

In fact, my code is not exactly in the form I posted, because it's a generic XML generator.

The real instructions are these (I copied and pasted the relevant code):


XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = false;
XmlWriter writer = XmlWriter.Create(@sNombreFicheroXML, settings);

writer.WriteStartDocument();
writer.WriteStartElement(arTags[0]);
writer.WriteStartElement(arTags[1], sDatos);
...


where arTags[0] has the value "AuditFile", arTags[1] is "xmlns", and sDatos is "urn:...".

The result (I tested again):

<?xml version="1.0" encoding="utf-8"?>
<AuditFile>
  <xmlns xmlns="urn:OECD:StandardAuditFile-Tax:PT_1.00_01">




 
Old December 4th, 2008, 07:49 AM
Authorized User
 
Join Date: Nov 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

Sorry, after posting the message I saw the bug. It must be:

XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = false;
XmlWriter writer = XmlWriter.Create(@sNombreFicheroXML, settings);

writer.WriteStartElement(arTags[0], sDatos);
...


Thank you!

 
Old December 4th, 2008, 07:52 AM
Authorized User
 
Join Date: Nov 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

And to do something like this?

<AuditFile xmlns="urn:OECD:StandardAuditFile-Tax:PT_1.00_01" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ReadsoftInvoice.xsd ">

That's the line I wanted to write from the beginning... I exclude the other namespaces to simplify.


 
Old December 4th, 2008, 07:57 AM
Authorized User
 
Join Date: Nov 2003
Posts: 78
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I found it!


XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.NewLineOnAttributes = false;
XmlWriter writer = XmlWriter.Create(@sNombreFicheroXML, settings);

writer.WriteStartElement("AuditFile", "urn:OECD:StandardAuditFile-Tax:PT_1.00_01");
writer.WriteAttributeString("xmlns", null, null, "urn:OECD:StandardAuditFile-Tax:PT_1.00_01");
writer.WriteAttributeString("xmlns", "xsi", null, "http://www.w3.org/2001/XMLSchema-instance");








Similar Threads
Thread Thread Starter Forum Replies Last Post
default-xpath-namespace stolte XSLT 1 March 19th, 2008 12:49 PM
Default Namespace - XSLT 1.0 Geierwally XSLT 2 July 9th, 2007 11:08 AM
Dynamic Default Namespace EcceBozo XSLT 2 June 6th, 2007 04:05 PM
How can I change the default namespace allanhu BOOK: ASP.NET Website Programming Problem-Design-Solution 1 November 1st, 2004 01:15 PM
Default Namespace (where is it?) bekim BOOK: ASP.NET Website Programming Problem-Design-Solution 4 June 12th, 2004 03:50 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.