473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 2931
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.