Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / January 2008

Tip: Looking for answers? Try searching our database.

XML - xmlns

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ve - 19 Jan 2008 13:49 GMT
Hi,

I have XmlDocument object.

-how to get information, if the XmlObject have default schema
(xmlns=”something” in main node) ?
-how to get default schema (as a string or other object) ?
-how to delete default schema ?
-how to set default schema ?

Thanks for help
Martin Honnen - 19 Jan 2008 14:26 GMT
> I have XmlDocument object.
>
> -how to get information, if the XmlObject have default schema
> (xmlns=”something” in main node) ?

xmlns="something" is a "default namespace declaration", it usually has
nothing to do with a schema. And there is nothing like a "default schema".

If you want to apply XPath expressions to select elements that are in
the default namespace then you need to use an XmlNamespaceManager to
bind a prefix (in my example "df") to the namespace name e.g.
  XmlDocument doc = new XmlDocument();
  doc.Load("file.xml");
  XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable);
  nsMgr.AddNamespace("df", "something");
then you can use that prefix in XPath expressions e.g.
  XmlNodeList list = doc.SelectNodes("df:root/df:foo/df:bar", nsMgr);

Signature

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

Ve - 19 Jan 2008 20:52 GMT
Martin Honnen pisze:
>> I have XmlDocument object.
>> -how to get information, if the XmlObject have default schema
>> (xmlns=”something” in main node) ?
> xmlns="something" is a "default namespace declaration", it usually has
> nothing to do with a schema. And there is nothing like a "default schema".

Of course you are right!

> If you want to apply XPath expressions to select elements that are in
> the default namespace then you need to use an XmlNamespaceManager to
[quoted text clipped - 5 lines]
> then you can use that prefix in XPath expressions e.g.
>   XmlNodeList list = doc.SelectNodes("df:root/df:foo/df:bar", nsMgr);

But using this code I am able to select nodes in default namespace.

But how can I check if the xml is containing a default namespace ?
How to delete default namespace from XmlDocument?
How to get name of the default namespace ?

Thanks
Martin Honnen - 20 Jan 2008 12:45 GMT
> But how can I check if the xml is containing a default namespace ?
> How to delete default namespace from XmlDocument?
> How to get name of the default namespace ?

Generally for your DOM and XPath processing it should not matter whether
a default namespace (e.g. xmlns="http://example.com/ns1") or a normal
namespace declaration (e.g. xmlns:pf="http://example.com/ns1") is
applied, you simply want to know whether an element or attribute node is
in a certain namespace or not. For that in the DOM model you can check
the NamespaceURI property of an element or attribute node
<URL:http://msdn2.microsoft.com/en-us/library/system.xml.xmlnode.namespaceuri.aspx>.
The DOM implementation in the .NET framework also exposes two methods
GetNamespaceOfPrefix
<URL:http://msdn2.microsoft.com/en-us/library/system.xml.xmlnode.getnamespaceofprefix.aspx>
and GetPrefixOfNamespace
<URL:http://msdn2.microsoft.com/en-us/library/system.xml.xmlnode.getprefixofnamespace.aspx>
that help dealing with namespaces.
So to get the name (URI) of the default namespace defined on the root
element you can do
  doc.DocumentElement.GetNamespaceOfPrefix("")
and you can check the result against the empty string to find out
whether a default namespace declaration exists.

      doc.LoadXml(
"<root xmlns=\"http://example.com/ns1\"><foo>bar</foo></root>");
      Console.WriteLine("GetNamespaceOfPrefix(\"\"): \"{0}\".",
doc.DocumentElement.GetNamespaceOfPrefix(""));
      // GetNamespaceOfPrefix(""): "http://example.com/ns1".
      doc.LoadXml("<root><foo>bar</foo></root>");
      Console.WriteLine("GetNamespaceOfPrefix(\"\"): \"{0}\".",
doc.DocumentElement.GetNamespaceOfPrefix(""));
      // GetNamespaceOfPrefix(""): "".

As for removing a namespace, that is technically not possible, at least
not in the way that you can change the namespace a node is in, the
NamespaceURI property of a node is readonly. So you need to create new
nodes in no namespace. An XSLT stylesheet is good at implementing such
transformations:
  <xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="*">
      <xsl:element name="{local-name()}">
        <xsl:apply-templates select="@* | node()"/>
      </xsl:element>
    </xsl:template>
    <xsl:template match="@* | comment() | processing-instruction() |
text()">
       <xsl:copy/>
    </xsl:template>
  </xsl:stylesheet>
This stylesheet strips any namespace from element nodes in the input XML
document. XSLT transformations can be applied using
System.Xml.Xsl.XslCompiledTransform.

Signature

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.