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

More than one namespace declaration in an element

I'm using XmlTextWriter to generate an XML document. I can't figure
out how to get more than one namespace declaration in an element, when
there is no WriteNamespaceDeclaration() method.

Thanks,

Gustaf

Nov 12 '05 #1
8 3438
"Gustaf Liljegren" <gu**************@bredband.net> wrote:
I'm using XmlTextWriter to generate an XML document. I can't figure
out how to get more than one namespace declaration in an element, when there is no WriteNamespaceDeclaration() method.


I found a solution to this, by just adding an attribute:

w.WriteAttributeString("xmlns:int", NS_INT);

But there's still a problem. The elements below this element doesn't
recognize the attributes, so the namespace declarations are repated
for each element. Is there a solution for this too?

Gustaf

Nov 12 '05 #2
Gustaf Liljegren wrote:
I'm using XmlTextWriter to generate an XML document. I can't figure
out how to get more than one namespace declaration in an element, when
there is no WriteNamespaceDeclaration() method.


What for? Namespace declarations are generated automatically on demand
(if you write element or attribute, which is in non-declared yet namespace).
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #3
Gustaf Liljegren wrote:
But there's still a problem. The elements below this element doesn't
recognize the attributes, so the namespace declarations are repated
for each element. Is there a solution for this too?


Most likely you are doing something wrong. You are not supposed to
create namespace declarations by hands, that's XmlWriter's responsibility.
--
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
Nov 12 '05 #4
To write namespace declarations in an element you must use one of the WriteAttribute* methods. Take into account that a namespace declaration is sintactically a form of an attribute. For example, to write out the namespace declarations of the following XML element

<someElement xmlns="http://an-url" xmlns:a="http://another-url"><!--Element content--></someElement

You can use

writer.WriteAttributeString("xmlns", null, "http://an-url"
writer.WriteAttributeString("xmlns", "a", null, "http://another-url"
Nov 12 '05 #5
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote:
But there's still a problem. The elements below this element doesn't recognize the attributes, so the namespace declarations are repated for each element. Is there a solution for this too?


Most likely you are doing something wrong. You are not supposed to
create namespace declarations by hands, that's XmlWriter's

responsibility.

Thank you both. What complicates my problem is that I need to import a
chunk of XML from an existing document, into a document that I
generate dynamically. Here's the code for extracting the chunk:

XmlDocument d = new XmlDocument();
d.Load(file_document);
XmlNodeList nl =
d.DocumentElement.GetElementsByTagName("GeneralDoc umentInformation",
NS_XBRL);

And here is the document that is being generated:

// Create XmlTextWriter
XmlTextWriter w = new XmlTextWriter("output.xml",
System.Text.Encoding.UTF8);
w.Formatting = Formatting.Indented;
w.Indentation = 2;
w.WriteStartDocument();
w.WriteStartElement("xbrl", NS_XBRL);
w.WriteAttributeString("xmlns:int", NS_INT);
w.WriteAttributeString("xmlns:bas", NS_BAS);
w.WriteRaw(nl.Item(0).OuterXml);

The chunk that is imported looks like this:

<GeneralDocumentInformation>
<int:DocumentLastEditDate>1995-09-25</int:DocumentLastEditDate>
<int:EntityCurrentLegalNameName />
<int:EntityIdentifier>

<int:EntityIdentifierDescription>50020</int:EntityIdentifierDescriptio
n>
</int:EntityIdentifier>
<int:EntityTradingName />
<bas:LocalAddress>
<int:Street1>Gångstigen 4</int:Street1>
</bas:LocalAddress>
<bas:ZIPPostalCode>414 65</bas:ZIPPostalCode>
<int:City>GÖTEBORG</int:City>
<bas:PhoneFaxNumbers>
<bas:Phone />
</bas:PhoneFaxNumbers>
</GeneralDocumentInformation>

And this is the result I get (removed the URIs for readability):

<?xml version="1.0" encoding="utf-8"?>
<xbrl xmlns:int="..." xmlns:bas="..." xmlns="...">
<GeneralDocumentInformation xmlns="...">
<int:DocumentLastEditDate xmlns:int="...">
1995-09-25
</int:DocumentLastEditDate>
<int:EntityCurrentLegalNameName xmlns:int="..." />
<int:EntityIdentifier xmlns:int="...">
<int:EntityIdentifierDescription>
50020
</int:EntityIdentifierDescription>
</int:EntityIdentifier>
<int:EntityTradingName xmlns:int="..." />
<bas:LocalAddress xmlns:bas="...">
<int:Street1 xmlns:int="...">Gångstigen 4</int:Street1>
</bas:LocalAddress>
<bas:ZIPPostalCode xmlns:bas="...">414 65</bas:ZIPPostalCode>
<int:City xmlns:int="...">GÖTEBORG</int:City>
<bas:PhoneFaxNumbers xmlns:bas="...">
<bas:Phone />
</bas:PhoneFaxNumbers>
</GeneralDocumentInformation>
</xbrl>

Thanks a lot for help.

Gustaf

Nov 12 '05 #6
"Marco A. Sánchez" <an*******@discussions.microsoft.com> wrote:
writer.WriteAttributeString("xmlns", null, "http://an-url")
writer.WriteAttributeString("xmlns", "a", null,

"http://another-url")

If found this way is a little shorter:

writer.WriteAttributeString("xmlns", "http://an-url")
writer.WriteAttributeString("xmlns:a", "http://another-url")

Both gives me exactly the same result.

Gustaf

Nov 12 '05 #7
The problem with

writer.WriteAttributeString("xmlns:a", "http://another-url")

is that the first parameter must be a local name, not a QName. Doing the previous call, the XML writer doesn't known that "a" is a namespace prefix, so it doesn't put the namespace declaration into the writer's namespace stack. Although the output XML is identicall, the method call is wrong, so you can't use the "a" prefix in subsequent calls.
Nov 12 '05 #8
I made an example to play with. Hope someone can help me getting it
right:

using System;
using System.Xml;

class XmlTest
{
private const string NS1 = "http://www.ns1.org";
private const string NS2 = "http://www.ns2.org";

static void Main(string[] args)
{
// Start document
XmlTextWriter w = new XmlTextWriter("output.xml",
System.Text.Encoding.UTF8);
w.WriteStartDocument();
w.WriteStartElement("root");
w.WriteAttributeString("xmlns", NS1);
w.WriteAttributeString("xmlns:ns2", NS2);

// Add chunk of XML from existing document
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("input.xml");
XmlNodeList xmlNodes = xmlDoc.GetElementsByTagName("chunk");
string chunk = xmlNodes.Item(0).OuterXml;
w.WriteRaw(chunk);

// End document
w.WriteEndElement();
w.WriteEndDocument();
w.Close();
}
}

The "input.xml" document looks like this:

<?xml version="1.0"?>
<document xmlns="http://ns1.org" xmlns:ns2="http://ns2.org">
<chunk>
<ns2:text>Some text here.</ns2:text>
</chunk>
</document>

The output I get looks liks this:

<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://www.ns1.org" xmlns:ns2="http://www.ns2.org">
<chunk xmlns="http://ns1.org">
<ns2:text xmlns:ns2="http://ns2.org">Some text here.</ns2:text>
</chunk>
</root>

And the output I want looks like this:

<?xml version="1.0" encoding="utf-8"?>
<root xmlns="http://www.ns1.org" xmlns:ns2="http://www.ns2.org">
<chunk>
<ns2:text>Some text here.</ns2:text>
</chunk>
</root>

Nov 12 '05 #9

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

Similar topics

3
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"...
6
by: Tjerk Wolterink | last post by:
When i open the following xml file in internetexplorer: <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE xc:content > <xc:xcontent...
4
by: Krishna Tulasi via .NET 247 | last post by:
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...
2
by: Dale Anderson | last post by:
I have a schema that I'm trying to read. The schema has an element named 'GrantApplication' and one with a namespace prefix named 'SF424:GrantApplication'. When I try to read this schema in, I...
2
by: tomek.romanowski | last post by:
Hi ! I have problem with validating of the document with multiple namespaces. The odd thing is, that my data work O'K when I test it under XMLSpy but it doesn't work with my C# code. My first...
8
by: Simon Brooke | last post by:
I was debugging a new XML generator tonight and trying to determine why it wasn't working; and realised my dom printer does not output XML namespace declarations. My method to output an Element...
7
by: Bilal | last post by:
Hello all, I came across this problem while working out the bugs in my identity trasnformation stylesheets but sidestepped it for later to see if there is an easier/better solution. This is...
1
by: Bardo | last post by:
Hi all, From what I can read, it doesn't look like I'm the only one getting confused with the elementFormDefault and attributeFormDefault properties of XML schemas. I am wanting to get a full...
1
by: dignan.tenenbaum | last post by:
Hello, I'm using the XmlReader.ReadOuterXml() method to return the string representation of an xml node. The XmlReader is created with a file path and a XmlReaderSettings object. This was...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.