473,835 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ 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:MainDocumen t xmlns:cc="http://tempuri.org/Customer.xsd"
xmlns:xv="http://tempuri.org/Product.xsd"
xmlns:xf="http://tempuri.org/Order.xsd">

<cc:Customer>

<cc:CustomerCod e>EFEREFFF</cc:CustomerCode >

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

<cc:Address>asd fsdf asdf asdf </cc:Address>

<cc:ZipCode>323 23</cc:ZipCode>

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

<cc:ProvinceCod e>TV</cc:ProvinceCode >

<cc:PricelistCo de>PR1</cc:PricelistCod e>

</cc:Customer>

<xv:Product>

<xv:ProductCode ></xv:ProductCode>

<xv:Price>120.0 0</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 5175
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!P LEASEtkachenko. com> wrote in message
news:OU******** ******@TK2MSFTN GP09.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 NamespaceCllect ingXmlReader : XmlTextReader
{
Hashtable namespaces = new Hashtable();

//Add constructors as needed
public NamespaceCllect ingXmlReader(st ring url) : base(url) {}

public Hashtable CollectedNamesp aces
{
get { return namespaces; }
}

public override bool Read()
{
bool baseRead = base.Read();
if (base.NodeType == XmlNodeType.Ele ment && base.NamespaceU RI
!= ""
&& !namespaces.Con tainsKey(base.N amespaceURI))
namespaces.Add( base.NamespaceU RI, "");
return baseRead;
}
}

and here is its usage:

XmlDocument doc = new XmlDocument();
NamespaceCllect ingXmlReader ncr = new
NamespaceCllect ingXmlReader("f oo.xml");
doc.Load(ncr);
foreach (object ns in ncr.CollectedNa mespaces.Keys)
Console.WriteLi ne(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!P LEASEtkachenko. com> wrote in message
news:#i******** *****@tk2msftng p13.phx.gbl...
Albertone wrote:
Have you any helpful examples?


Here is simple custom XmlReader:

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

//Add constructors as needed
public NamespaceCllect ingXmlReader(st ring url) : base(url) {}

public Hashtable CollectedNamesp aces
{
get { return namespaces; }
}

public override bool Read()
{
bool baseRead = base.Read();
if (base.NodeType == XmlNodeType.Ele ment && base.NamespaceU RI
!= ""
&& !namespaces.Con tainsKey(base.N amespaceURI))
namespaces.Add( base.NamespaceU RI, "");
return baseRead;
}
}

and here is its usage:

XmlDocument doc = new XmlDocument();
NamespaceCllect ingXmlReader ncr = new
NamespaceCllect ingXmlReader("f oo.xml");
doc.Load(ncr);
foreach (object ns in ncr.CollectedNa mespaces.Keys)
Console.WriteLi ne(ns);

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

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


Here is simple custom XmlReader:

public class NamespaceCllect ingXmlReader : 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:styleshe et 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="namespa ce-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
1475
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 I am trying to use XML Schemas to tell more about what kind of data are allowed on each element/attribute, but I can't understand how to mix Namespaces and Schemas. Can anyone give me tips, starting points, about how to specify the
1
11362
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 string to input into a legacy system. I've put together a VB.Net class that does this using DOM commands such as SelectSingleNode(). It works fine but I've noticed that it can not read past inbedded namespaces that are found throghout the...
3
2404
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 the posts I have seen is of the form: --------------------------------------------- XmlDocument doc = new XmlDocument(); doc.Load(@"c:\foobar.xml"); XmlNamepaceManager nmMgr = newNamespaceManager(doc); nmMgr.AddNamespace("abc", "www.my.uri");
4
15789
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 there an "ApplyToAll" kind of feature? I also tried the defaultNamespace, but it didn't work.
19
16238
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" xmlns:sns="http://www.test.org/sub" xmlns:mns="http://www.test.org/mini"> <data>
36
4057
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) <DOM Element: href at 0x1443e68> >>> document.toxml() '<?xml version="1.0" ?>\n<href/>' Note that the namespace wasn't emitted. If I have PyXML,
6
3015
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 0/Nothing respectively. Anyone have any thoughts? Thanks in advance VB.Net 2005
5
1520
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> <destinations xmlns:destination0="http://mydomain.com/destinations/dest0"
1
1984
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 whenever the namespaces are referenced later in the document have an empty string appear "". Having the namespaces appear at all times is messy and clutters the document. How can I achieve this? I am using System.Xml.XmlDocument, System.Xml.NameTable nt,...
0
9653
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
10815
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10563
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
10237
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
9348
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
6970
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
5640
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...
1
4435
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
2
3999
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.