473,467 Members | 2,491 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Retrieve namespaces from xml document


Hi all,

We are developing a .net application that can accept xml documents.

Every xml document can match a single schema, or it may be composed by
multiple different elements and each of these can match a different xsd
schema.

Is there a way to retrieve the namespace collection of all the document
namespaces for which there is at least one element in the document?

Example:

<?xml version="1.0" encoding="utf-8"?>

<xv:MainDocument xmlns:cc="http://tempuri.org/Customer.xsd"
xmlns:xv="http://tempuri.org/Product.xsd"
xmlns:xf="http://tempuri.org/Order.xsd">

<cc:Customer>

<cc:CustomerCode>EFEREFFF</cc:CustomerCode>

<cc:BusinessName>fg sdg sdgh sgfhsdgh sdfg</cc:BusinessName>

<cc:Address>asdfsdf asdf asdf </cc:Address>

<cc:ZipCode>32323</cc:ZipCode>

<cc:CountryCode>IT</cc:CountryCode>

<cc:ProvinceCode>TV</cc:ProvinceCode>

<cc:PricelistCode>PR1</cc:PricelistCode>

</cc:Customer>

<xv:Product>

<xv:ProductCode></xv:ProductCode>

<xv:Price>120.00</xv:Price>

</xv:Product>

</xv:MainDocument>

For example, in this XML document there are two elements matching two
different xsd schemas. For this document We want to retrieve the namespaces
"http://tempuri.org/Customer.xsd" and http://tempuri.org/Product.xsd"

Thanks in advance,

Daniele_
Nov 12 '05 #1
6 5157
Daniele wrote:
Is there a way to retrieve the namespace collection of all the document
namespaces for which there is at least one element in the document?


Well, that's easy, but what's the best way depend on your application.
Perf and memory friendly way is to collect namespaces using simple
custom XmlReader, while XML is being read. Otherwise it can be done
easily using XSLT.
--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:OU**************@TK2MSFTNGP09.phx.gbl...
Daniele wrote:
Is there a way to retrieve the namespace collection of all the document
namespaces for which there is at least one element in the document?
Well, that's easy, but what's the best way depend on your application.
Perf and memory friendly way is to collect namespaces using simple
custom XmlReader, while XML is being read. Otherwise it can be done
easily using XSLT.


Have you any helpful examples?
Thanks, Alberto.
--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #3
Albertone wrote:
Have you any helpful examples?


Here is simple custom XmlReader:

public class NamespaceCllectingXmlReader : XmlTextReader
{
Hashtable namespaces = new Hashtable();

//Add constructors as needed
public NamespaceCllectingXmlReader(string url) : base(url) {}

public Hashtable CollectedNamespaces
{
get { return namespaces; }
}

public override bool Read()
{
bool baseRead = base.Read();
if (base.NodeType == XmlNodeType.Element && base.NamespaceURI
!= ""
&& !namespaces.ContainsKey(base.NamespaceURI))
namespaces.Add(base.NamespaceURI, "");
return baseRead;
}
}

and here is its usage:

XmlDocument doc = new XmlDocument();
NamespaceCllectingXmlReader ncr = new
NamespaceCllectingXmlReader("foo.xml");
doc.Load(ncr);
foreach (object ns in ncr.CollectedNamespaces.Keys)
Console.WriteLine(ns);

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4
Ok Oleg, your code seems to work fine.
Thanks, Alberto.

"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:#i*************@tk2msftngp13.phx.gbl...
Albertone wrote:
Have you any helpful examples?


Here is simple custom XmlReader:

public class NamespaceCllectingXmlReader : XmlTextReader
{
Hashtable namespaces = new Hashtable();

//Add constructors as needed
public NamespaceCllectingXmlReader(string url) : base(url) {}

public Hashtable CollectedNamespaces
{
get { return namespaces; }
}

public override bool Read()
{
bool baseRead = base.Read();
if (base.NodeType == XmlNodeType.Element && base.NamespaceURI
!= ""
&& !namespaces.ContainsKey(base.NamespaceURI))
namespaces.Add(base.NamespaceURI, "");
return baseRead;
}
}

and here is its usage:

XmlDocument doc = new XmlDocument();
NamespaceCllectingXmlReader ncr = new
NamespaceCllectingXmlReader("foo.xml");
doc.Load(ncr);
foreach (object ns in ncr.CollectedNamespaces.Keys)
Console.WriteLine(ns);

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com

Nov 12 '05 #5
"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:%2***************@tk2msftngp13.phx.gbl...
Albertone wrote:
Have you any helpful examples?


Here is simple custom XmlReader:

public class NamespaceCllectingXmlReader : XmlTextReader
... Oleg Tkachenko [XML MVP]


Oh yeah, this works fine!
In previous message you say something about XSLT. It could be faster? Excuse
me if I put this question, but I am a newbie with XML and I don't know well
XSLT syntax... Maybe you have an example with XSLT?
Thank you.
Nov 12 '05 #6
Daniele wrote:
Oh yeah, this works fine!
In previous message you say something about XSLT. It could be faster? Excuse
me if I put this question, but I am a newbie with XML and I don't know well
XSLT syntax... Maybe you have an example with XSLT?


In XSLT it's going to be something like

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="nsKey" match="*" use="namespace-uri()"/>
<xsl:template match="/">
<xsl:for-each select="//*[generate-id() = generate-id(key('nsKey',
namespace-uri())[1])]">
<xsl:value-of select="namespace-uri()"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

but it won't be faster XmlReader anyway. It requires at least two full
XML tree traversals.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #7

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

Similar topics

6
by: inerte | last post by:
Hello all! I need to build an XML file structure so a client can import data to one of our systems. Totally new to XML, I learned about Namespaces and DTD, and built a nice spec using them. Now...
1
by: Greg Rothlander | last post by:
I posted this a few days ago and didn't get any response. I try again but ask it a little differently. I'm recieving any XML document from a client and I need to convert it to an ASCII delimited...
3
by: Colin Green | last post by:
I have come across what seems like a failing in the .Net XML classes. Many people have posted requesting how to write an XPath query with namespace prefixes that works. The solution shown in all...
4
by: Alexis | last post by:
Hello, Is there a way of telling the XmlSerializer to ignore all namespaces when deserializing. I'm using XmlAttributeOverrides, but I have to do it for every class the OutputObject usses. Is...
19
by: David Thielen | last post by:
Hi; If there are no namespaces this works fine for me. But if the xml has namespaces, then I get either no node back or an exception. Here is the sample xml: <root xmlns="http://www.test.org"...
36
by: Wilfredo Sánchez Vega | last post by:
I'm having some issues around namespace handling with XML: >>> document = xml.dom.minidom.Document() >>> element = document.createElementNS("DAV:", "href") >>> document.appendChild(element)...
6
by: AMDRIT | last post by:
Hello Everyone, I am having an issue with xml and namespaces, at least I think it is namespaces. When I use namespaces, I cannot use SelectSingleNode / SelectNodes as they always return...
5
by: printdude1968 | last post by:
Hi Everyone... I'm a newbie to this stuff so bear with me. I have a well formed xml document (I use jdom to verify) <?xml version="1.0" encoding="ISO-8859-1"?> <print-control>...
1
by: gotclout | last post by:
I've generated an XML document, and throughout the document the namespaces are displayed for each element. I would like to have all of the namespaces declared at the top of the document, and...
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.