Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Removing Schema from XmlDocument

64 views
Skip to first unread message

Sharon

unread,
Feb 6, 2003, 11:24:41 AM2/6/03
to
Hi,

I do not know how to remove the schema details from an
XmlDocument. I have loaded my Xml document as follows:

XmlDocument doc = new XmlDocument();
doc.load("test.xml");

and I would like to remove the following details from my
XmlDocument:
xmlns="http://localhost/reportdownload/testSchema.xml

Is this possible??

The entire xml document is as follows:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE Properties>
<DefaultValues
xmlns="http://localhost/reportdownload/testSchema.xml">
<Project >
<ProxyEnable type="bool" value="False"/>
<ProxyName type="string" value=""/>
<ProxyPort type="long" value="80"/>
<KeepOneRecycledFile type="bool"
value="false"/>
<RecycleSize type="long" value="20"/>
<LogPath type="string"
value="(automatic)"/>
<LogEnable type="bool" value="true"/>
<CheckRobots type="bool" value="True"/>
<UseAbortiveTcpClose type="bool"
value="true"/>
<SocketTimeout type="long" value="120"/>
</Project>
<DynamicTest >
<Language type="string" value="VBScript"/>
<NumberOfThreads type="long" value="1"/>
<FollowRedirects type="bool" value="True"/>
<RedirectDepth type="long" value="15"/>
</DynamicTest>

</DefaultValues>


Thanks for any help,
Sharon

Dare Obasanjo [MSFT]

unread,
Feb 7, 2003, 5:21:40 PM2/7/03
to
The namespace is an integral part of the name of the element, it is not just
an attribute. When you remove the xmlns attribute, it is just getting put
back by the save code, which ensures that there are appropriate xmlns decls
for all in-scope names.

There is no easy way to 'remove' the namespace from an element or attribute.
The only way to do this is to write code to walk a tree and create a
duplicate tree with different namespace uris.

You could also write a fairly simple XSLT stylesheet to transform it. the
main guts would be:

<xsl:template match="*" mode="RemoveNamespace">
<xsl:element name="{local-name()}">
<xsl:copy-of select="@*"/>
</xsl:element>
</xsl:template>

This assumes that no attribute is in a namespace (which is typical, but
academic to change if it's not true).

--
This posting is provided "AS IS" with no warranties, and confers no rights.


"Sharon" <sharon...@totalise.net> wrote in message
news:011301c2cdfc$40a62c40$d2f82ecf@TK2MSFTNGXA09...

Oleg Tkachenko

unread,
Feb 7, 2003, 6:25:03 PM2/7/03
to
Dare Obasanjo [MSFT] wrote:

> You could also write a fairly simple XSLT stylesheet to transform it. the
> main guts would be:
>
> <xsl:template match="*" mode="RemoveNamespace">
> <xsl:element name="{local-name()}">
> <xsl:copy-of select="@*"/>

<!-- I believe you forgot apply-templates here -->
<xsl:apply-teampltes/>
> </xsl:element>
> </xsl:template>

Also XSL FAQ has full copy of such a stylesheet:
http://www.dpawson.co.uk/xsl/sect2/N5536.html#d4932e1708.
--
Oleg Tkachenko
Multiconn Technologies, Israel

Kirk Allen Evans

unread,
Feb 8, 2003, 8:22:12 AM2/8/03
to
I prefer the XSLT method as well, I think it is cleaner and suits the
purpose. However, if you have an aversion to XSLT, you can do this with a
little grunt work through the framework. Write the contents to an
XmlWriter, set the stream's position to 0, create an XmlReader using the
same stream, and indicate that the reader does not support namespace
bindings using the Namespaces property of the reader. Then use the reader
to re-load the XmlDocument.

XmlDocument doc = new XmlDocument();

doc.Load("yourxmlfile.xml");
System.IO.MemoryStream mem = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(mem, System.Text.Encoding.UTF8);
doc.WriteContentTo(writer);
writer.Flush();
mem.Position = 0;
XmlTextReader reader = new XmlTextReader(mem);
//The reader will not support namespaces, removing
//the xmlns from any namespace bound node
reader.Namespaces = false;
doc.Load(reader);

The contents of doc should contain only the local names of all of the nodes
without namespace bindings.

Kirk Allen Evans
Author, "XML And ASP.NET", New Riders Publishing
www.xmlandasp.net
Read my web log at http://www.dotnetweblogs.com/kaevans


"Oleg Tkachenko" <ol...@multiconn.com> wrote in message
news:ubHtQAwzCHA.1620@TK2MSFTNGP11...

0 new messages