472,782 Members | 1,125 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,782 software developers and data experts.

Removing the xmlns attribute from the DOM

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
7 2877
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
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


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
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
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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Matt | last post by:
Hello, I have an XML document similar to the following: <DataItems> <Data xmlns="http://www.me.com"> <DataInformation xmlns:a="http://www.me.com/ASettings" xsi:type="a:Stuff1">...
3
by: Keith Hill | last post by:
I am creating an XmlDocument in code and then using XmlTextWriter via doc.WriteTo(xwriter) to output the result to a text box. I have a root element that defines a default namespace. However, the...
1
by: Maziar Aflatoun | last post by:
Hello, I have a string variable that contains XML data with many different namespaces. I like to remove all the namespaces from my XML (clean the XML). What's the quickest way to do this? Ex....
11
by: EAI | last post by:
Hi All, I have a XML of the following form <?xml version="1.0"?> <xxxx xmlns="http://xxx.xxx.com"> .... </xxxx> When I try to read xml using SelectSingleNode, I am getting exception
0
by: jts2077 | last post by:
I am trying to create a large nested XML object using E4X methods. The problem is the, the XML I am trying to create can only have xmlns set at the top 2 element levels. Such as: <store ...
6
by: Chris Chiasson | last post by:
Hi, After reading and experimenting for a several hours, I have this stylesheet: <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"...
0
by: Abhinay | last post by:
hi, this is abhinay, i have data ie xml file Ex: <root> <child1> <cc1>aaa</cc1> </child1>
4
by: =?Utf-8?B?UmFqaXY=?= | last post by:
Hi, I have a webservices(developed in .net 2.0, using c#) with a webmethod that returns a XML document. The problem is that the XML returned from the webservice is having a attribute xmlns="" in...
2
by: MedIt | last post by:
Hi, I am new in using xsl.I am stuck in a simple task,which is trying to remove an atribute from a node,and keep the rest of the attributes of this element in the xslt. I have tried a couple of...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.