By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,691 Members | 1,143 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,691 IT Pros & Developers. It's quick & easy.

Removing the xmlns attribute from the DOM

P: n/a
Hi,

I have a requirement to remove the xmlns from the DOM in order to pass over
to MS CRM 3.0 Fetch method.It seems the fetch method blows up if there is a
xmlns present!?!
The reason I have a xmlns present is because the Xml I am passing to CRM is
a node from a bigger file that does require a xmlns and using the DOM
..OuterXml seems to set the xmlns for you automatically - which I don't want.

Any help would be great.
Simon.
May 14 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Sorry, I only just saw the other post, ignore this one!

Thanks
Simon.

"Simon Hart" <srhartone[no spam]@yahoo.com> wrote in message
news:Ow**************@TK2MSFTNGP04.phx.gbl...
Hi,

I have a requirement to remove the xmlns from the DOM in order to pass
over to MS CRM 3.0 Fetch method.It seems the fetch method blows up if
there is a xmlns present!?!
The reason I have a xmlns present is because the Xml I am passing to CRM
is a node from a bigger file that does require a xmlns and using the DOM
.OuterXml seems to set the xmlns for you automatically - which I don't
want.

Any help would be great.
Simon.

May 14 '06 #2

P: n/a
Reaging the other post. I tried using XmlNamespaceManager but this didn't
work after trying many things. So in the end the trusty string class came to
the rescue:

try
{
XmlDocument docNs = new XmlDocument();
docNs.LoadXml(xmlToSearch);
XmlNodeList fetch = docNs.GetElementsByTagName("fetch");
XmlAttribute att = fetch[0].Attributes["xmlns"];
if (att != null)
{
//Then we have a namespace we need to remove.
string ns = fetch[0].NamespaceURI;
string nsToRemove = "xmlns=\"" + ns + "\"";
xmlToSearch = xmlToSearch.Replace(nsToRemove,"");
}
}
catch{}

Regards
Simon.

"Simon Hart" <srhartone[no spam]@yahoo.com> wrote in message
news:Ow**************@TK2MSFTNGP04.phx.gbl...
Hi,

I have a requirement to remove the xmlns from the DOM in order to pass
over to MS CRM 3.0 Fetch method.It seems the fetch method blows up if
there is a xmlns present!?!
The reason I have a xmlns present is because the Xml I am passing to CRM
is a node from a bigger file that does require a xmlns and using the DOM
.OuterXml seems to set the xmlns for you automatically - which I don't
want.

Any help would be great.
Simon.

May 14 '06 #3

P: n/a


Simon Hart wrote:
I have a requirement to remove the xmlns from the DOM in order to pass over
to MS CRM 3.0 Fetch method.It seems the fetch method blows up if there is a
xmlns present!?!
The reason I have a xmlns present is because the Xml I am passing to CRM is
a node from a bigger file that does require a xmlns and using the DOM
.OuterXml seems to set the xmlns for you automatically - which I don't want.


With the DOM a DOM node gets its namespaceURI when it is created and you
can't change that afterwards. So you would need to create a new node in
no namespace and replace the old one with the new. And if you have e.g.
<root xmlns="http://example.com/2006/ns1">
<child />
</root>
then when the XML is parsed each element node, so both root and child
are created in the namespace with namespace URI
http://example.com/2006/ns1. That means if you wanted to use the DOM to
change the namespace (or remove it) that you have to recreate each
element node.

Fortunately there is XSLT where you can write a short stylesheet that
removes all namespaces e.g.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" />

<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>

<xsl:template match="@*">
<xsl:attribute name="{local-name()}"><xsl:value-of select="."
/></xsl:attribute>
</xsl:template>

<xsl:template match="text() | comment() | processing-instruction()">
<xsl:copy />
</xsl:template>

</xsl:stylesheet>

Then you can apply that transformation with XslTransform in .NET 1.x and
XslCompiled Transform in .NET 2.0.

On the other hand most if not all attempts of people to strip an XML
document of its namespaces are usually caused by misunderstanding or
lack of knowledge on how to use namespace aware tools. I don't know MS
CRM 3.0 but I would suggest to first check in detail whether it does not
have settings or APIs to properly process XML with namespaces.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 14 '06 #4

P: n/a
Martin,

Many thanks for your post.

Yes I did check the API's and certain method calls expect namespaces while
others do not. Unfortionately, the method I need does not seem to handle an
Xml with a namespace. I am calling a Fetch method which uses Xml to do a
search on a given table in the database.
I generally prefer to use OO classes rather than Xml, but in this case I
have no choice but to the the Xml methods the CRM 3.0 API provides.

I have another slight problem which I am sure you would know the answer to.
How do you iterate a DOM deleting nodes using RemoveChild(). Let me
rephrase, I know how to delete a node but the counters will get messed up
when the first one is deleted. Consider the following:

XmlNodeList searchEntity = doc.GetElementsByTagName("EntityID");

for(int i = 0; i < searchEntity.Count; i++)
{
XmlNode entity = searchEntity[i];
XmlNode valNode = doc.CreateNode(XmlNodeType.Element, "Value",
entity.NamespaceURI);
string id = GetID(entity.ChildNodes[0].OuterXml);
if (id == null)
{
//Then we couldn't find this value so remove the property rather than
//raise an exception.
properties[0].RemoveChild(searchEntity[i].ParentNode);
}
else
{
valNode.InnerText = id;
XmlNode parentNode = entity.ParentNode;
parentNode.ReplaceChild(valNode, entity);
}
}

I hope the above snip makes sense as of course its not all there, but I hope
you understand where I am comming from.

As soon as the first properties[0].RemoveChild() is called, the
searchEntity.Count will be one less. Of course the above works when removing
only one item, but there may be many.

Regards
Simon.
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:ei**************@TK2MSFTNGP04.phx.gbl...


Simon Hart wrote:
I have a requirement to remove the xmlns from the DOM in order to pass
over to MS CRM 3.0 Fetch method.It seems the fetch method blows up if
there is a xmlns present!?!
The reason I have a xmlns present is because the Xml I am passing to CRM
is a node from a bigger file that does require a xmlns and using the DOM
.OuterXml seems to set the xmlns for you automatically - which I don't
want.


With the DOM a DOM node gets its namespaceURI when it is created and you
can't change that afterwards. So you would need to create a new node in no
namespace and replace the old one with the new. And if you have e.g.
<root xmlns="http://example.com/2006/ns1">
<child />
</root>
then when the XML is parsed each element node, so both root and child are
created in the namespace with namespace URI http://example.com/2006/ns1.
That means if you wanted to use the DOM to change the namespace (or remove
it) that you have to recreate each element node.

Fortunately there is XSLT where you can write a short stylesheet that
removes all namespaces e.g.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" />

<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>

<xsl:template match="@*">
<xsl:attribute name="{local-name()}"><xsl:value-of select="."
/></xsl:attribute>
</xsl:template>

<xsl:template match="text() | comment() | processing-instruction()">
<xsl:copy />
</xsl:template>

</xsl:stylesheet>

Then you can apply that transformation with XslTransform in .NET 1.x and
XslCompiled Transform in .NET 2.0.

On the other hand most if not all attempts of people to strip an XML
document of its namespaces are usually caused by misunderstanding or lack
of knowledge on how to use namespace aware tools. I don't know MS CRM 3.0
but I would suggest to first check in detail whether it does not have
settings or APIs to properly process XML with namespaces.
--

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

May 14 '06 #5

P: n/a
I have just marked them for delete (ArrayList). Seems to do the trick just
nicely.

Simon.
"Simon Hart" <srhartone[no spam]@yahoo.com> wrote in message
news:uc**************@TK2MSFTNGP05.phx.gbl...
Martin,

Many thanks for your post.

Yes I did check the API's and certain method calls expect namespaces while
others do not. Unfortionately, the method I need does not seem to handle
an Xml with a namespace. I am calling a Fetch method which uses Xml to do
a search on a given table in the database.
I generally prefer to use OO classes rather than Xml, but in this case I
have no choice but to the the Xml methods the CRM 3.0 API provides.

I have another slight problem which I am sure you would know the answer
to.
How do you iterate a DOM deleting nodes using RemoveChild(). Let me
rephrase, I know how to delete a node but the counters will get messed up
when the first one is deleted. Consider the following:

XmlNodeList searchEntity = doc.GetElementsByTagName("EntityID");

for(int i = 0; i < searchEntity.Count; i++)
{
XmlNode entity = searchEntity[i];
XmlNode valNode = doc.CreateNode(XmlNodeType.Element, "Value",
entity.NamespaceURI);
string id = GetID(entity.ChildNodes[0].OuterXml);
if (id == null)
{
//Then we couldn't find this value so remove the property rather than
//raise an exception.
properties[0].RemoveChild(searchEntity[i].ParentNode);
}
else
{
valNode.InnerText = id;
XmlNode parentNode = entity.ParentNode;
parentNode.ReplaceChild(valNode, entity);
}
}

I hope the above snip makes sense as of course its not all there, but I
hope you understand where I am comming from.

As soon as the first properties[0].RemoveChild() is called, the
searchEntity.Count will be one less. Of course the above works when
removing only one item, but there may be many.

Regards
Simon.
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:ei**************@TK2MSFTNGP04.phx.gbl...


Simon Hart wrote:
I have a requirement to remove the xmlns from the DOM in order to pass
over to MS CRM 3.0 Fetch method.It seems the fetch method blows up if
there is a xmlns present!?!
The reason I have a xmlns present is because the Xml I am passing to CRM
is a node from a bigger file that does require a xmlns and using the DOM
.OuterXml seems to set the xmlns for you automatically - which I don't
want.


With the DOM a DOM node gets its namespaceURI when it is created and you
can't change that afterwards. So you would need to create a new node in
no namespace and replace the old one with the new. And if you have e.g.
<root xmlns="http://example.com/2006/ns1">
<child />
</root>
then when the XML is parsed each element node, so both root and child are
created in the namespace with namespace URI http://example.com/2006/ns1.
That means if you wanted to use the DOM to change the namespace (or
remove it) that you have to recreate each element node.

Fortunately there is XSLT where you can write a short stylesheet that
removes all namespaces e.g.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:output method="xml" />

<xsl:template match="*">
<xsl:element name="{local-name()}">
<xsl:apply-templates select="@* | node()" />
</xsl:element>
</xsl:template>

<xsl:template match="@*">
<xsl:attribute name="{local-name()}"><xsl:value-of select="."
/></xsl:attribute>
</xsl:template>

<xsl:template match="text() | comment() | processing-instruction()">
<xsl:copy />
</xsl:template>

</xsl:stylesheet>

Then you can apply that transformation with XslTransform in .NET 1.x and
XslCompiled Transform in .NET 2.0.

On the other hand most if not all attempts of people to strip an XML
document of its namespaces are usually caused by misunderstanding or lack
of knowledge on how to use namespace aware tools. I don't know MS CRM 3.0
but I would suggest to first check in detail whether it does not have
settings or APIs to properly process XML with namespaces.
--

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


May 14 '06 #6

P: n/a


Simon Hart wrote:

I have another slight problem which I am sure you would know the answer to.
How do you iterate a DOM deleting nodes using RemoveChild(). Let me
rephrase, I know how to delete a node but the counters will get messed up
when the first one is deleted. Consider the following:

XmlNodeList searchEntity = doc.GetElementsByTagName("EntityID");

for(int i = 0; i < searchEntity.Count; i++) As soon as the first properties[0].RemoveChild() is called, the
searchEntity.Count will be one less.


Yes, GetElementsByTagName gives a "live collection" that is updated
whenever the document it is based on changes.
Using GetElementsByTagName is not recommended at all in the .NET
framework, see
<http://support.microsoft.com/kb/823928/en-us>
so using
doc.SelectNodes("//EntityID")
is a better way, and the collection that gives is not "live".

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
May 14 '06 #7

P: n/a
Martin,

Thanks again and thanks for link, interesting article.

The benefit of using GetElementsByTagName() in my case is the structure of
the Xml will be different in some cases. If using XPath, I won't be able to
process slightly different structure Xml files using the same code, or
certainly requiring alot more work.

Regards
Simon.

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:Ox**************@TK2MSFTNGP03.phx.gbl...


Simon Hart wrote:

I have another slight problem which I am sure you would know the answer
to.
How do you iterate a DOM deleting nodes using RemoveChild(). Let me
rephrase, I know how to delete a node but the counters will get messed up
when the first one is deleted. Consider the following:

XmlNodeList searchEntity = doc.GetElementsByTagName("EntityID");

for(int i = 0; i < searchEntity.Count; i++)

As soon as the first properties[0].RemoveChild() is called, the
searchEntity.Count will be one less.


Yes, GetElementsByTagName gives a "live collection" that is updated
whenever the document it is based on changes.
Using GetElementsByTagName is not recommended at all in the .NET
framework, see
<http://support.microsoft.com/kb/823928/en-us>
so using
doc.SelectNodes("//EntityID")
is a better way, and the collection that gives is not "live".

--

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

May 14 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.