472,784 Members | 853 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,784 software developers and data experts.

XmlDocument Performace & XmlWriter namespace oddity

Hi, 2 questions....

1. I'm parsing an XHTML document that contains both the default namespace
(xmlns="http://www.w3.org/1999/xhtml") and a custom one (xmlns:r="...") -
both of these being attributes of <html>. When I then insert new XmlNodes
into the DOM the HTML tags all end up getting an unnecesarry 'xmlns'
attribute added, elements with the 'r' prefix don't, which is correct. Is
there any way to stop the XmlNode/XmlWriter from inserting the unneeded
attributes to the html tags?

2. My program is reading in an XHTML file, adding/removing/changing elements
and then writing new ones back out again, when the document begins with:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
it runs very-very-very slowly, removing the DOCTYPE from the file speeds the
process up by around 1 minute! (this isn't a particularly large file, only
about 5-10 new elements are being added). This is obviously because of the
DTD (the slow down seems to be happening in
XmlDocument.CreateDocumentFragment and the XmlNode insertion methods mostly,
is there any way to turn off the DTD verification? It's so slow it looks like
it's downloading the url each time!

I'm running .net 1.1 on a very fast machine btw.

Thanks...
david
Nov 12 '05 #1
4 4112


David Grogan wrote:

1. I'm parsing an XHTML document that contains both the default namespace
(xmlns="http://www.w3.org/1999/xhtml") and a custom one (xmlns:r="...") -
both of these being attributes of <html>. When I then insert new XmlNodes
into the DOM the HTML tags all end up getting an unnecesarry 'xmlns'
attribute added, elements with the 'r' prefix don't, which is correct. Is
there any way to stop the XmlNode/XmlWriter from inserting the unneeded
attributes to the html tags?
Make sure you use the proper namespace URI when you create new XHTML
elements e.g.
XmlElement paragraph = xmlDocumentInstance.CreateElement("", "p",
"http://www.w3.org/1999/xhtml");

That way I do not have problems with additional xmlns attributes e.g.
complete example

const string xhtml1NamespaceURI = "http://www.w3.org/1999/xhtml";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(@"<html xmlns=""http://www.w3.org/1999/xhtml"">
<head>
<title>Example</title>
</head>
<body>
<h1>Example</h1>
</body>
</html>");
xmlDocument.Save(Console.Out);

XmlElement paragraph = xmlDocument.CreateElement("", "p",
xhtml1NamespaceURI);
paragraph.AppendChild(xmlDocument.CreateTextNode(" Kibology for
all."));
XmlElement body = xmlDocument.GetElementsByTagName("body",
xhtml1NamespaceURI)[0] as XmlElement;
if (body != null) {
body.AppendChild(paragraph);
xmlDocument.Save(Console.Out);
}

outputs

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Example</title>
</head>
<body>
<h1>Example</h1>
<p>Kibology for all.</p>
</body>
</html>

2. My program is reading in an XHTML file, adding/removing/changing elements
and then writing new ones back out again, when the document begins with:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
it runs very-very-very slowly, removing the DOCTYPE from the file speeds the
process up by around 1 minute!


You can set the XmlResolver property to a resolver which loads the DTD
from a locally cached file instead of loading from www.w3.org each time.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
"Martin Honnen" wrote:
Make sure you use the proper namespace URI when you create new XHTML
elements e.g.
XmlElement paragraph = xmlDocumentInstance.CreateElement("", "p",
"http://www.w3.org/1999/xhtml");
I'm not using XmlDocument.CreateElement, here's a more detailed example...

input xml:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:r="urn:test">
<head><title></title></head>
<body>
<br /><b>Heading</b><p>Hello</p><br />
</body>
</html>

If 'node' is the XmlNode for <body> then the following code is where my
problem lies...

Dim innerXml As String = node.InnerXml

looking at innerXml while debugging I see that it looks like...
<br xmlns="http://www.w3.org/1999/xhtml"/><b
xmlns="http://www.w3.org/1999/xhtml">Heading</b><p
xmlns="http://www.w3.org/1999/xhtml">Hello</p><br
xmlns="http://www.w3.org/1999/xhtml"/>

then I'm inserting various bits and pieces into innerXml followed by...

Dim newNode As XmlNode = node.OwnerDocument.CreateDocumentFragment
newNode.InnerXml = newInnerXml
node.ParentNode.ReplaceChild(newNode, node)

CreateDocumentFragment doesn't have a parameter for setting the namespace,
and newNode's NamespaceURI property is ReadOnly...
You can set the XmlResolver property to a resolver which loads the DTD
from a locally cached file instead of loading from www.w3.org each time.


setting the XmlResolver to Nothing fixed this, thanks.
David
Nov 13 '05 #3


David Grogan wrote:
input xml:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:r="urn:test">
<head><title></title></head>
<body>
<br /><b>Heading</b><p>Hello</p><br />
</body>
</html>

If 'node' is the XmlNode for <body> then the following code is where my
problem lies...

Dim innerXml As String = node.InnerXml

looking at innerXml while debugging I see that it looks like...
<br xmlns="http://www.w3.org/1999/xhtml"/><b
xmlns="http://www.w3.org/1999/xhtml">Heading</b><p
xmlns="http://www.w3.org/1999/xhtml">Hello</p><br
xmlns="http://www.w3.org/1999/xhtml"/>


It has to look like that, all those elements are in the XHTML namespace
with namespace URI http://www.w3.org/1999/xhtml and if they are
serialized alone then the xmlns="http://www.w3.org/1999/xhtml" has to be
added to serialize them properly. Only if there is a parent or ancestor
element which already declares xmlns="http://www.w3.org/1999/xhtml" then
the child or descendant elements do not need the declaration of the
namespace.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 13 '05 #4

"Martin Honnen" wrote:
looking at innerXml while debugging I see that it looks like...
<br xmlns="http://www.w3.org/1999/xhtml"/><b
xmlns="http://www.w3.org/1999/xhtml">Heading</b><p
xmlns="http://www.w3.org/1999/xhtml">Hello</p><br
xmlns="http://www.w3.org/1999/xhtml"/>


It has to look like that, all those elements are in the XHTML namespace
with namespace URI http://www.w3.org/1999/xhtml and if they are
serialized alone then the xmlns="http://www.w3.org/1999/xhtml" has to be
added to serialize them properly. Only if there is a parent or ancestor
element which already declares xmlns="http://www.w3.org/1999/xhtml" then
the child or descendant elements do not need the declaration of the
namespace.


but surely when newNode is inserted as a child of <html> it should strip out
the xmlns attributes because they are superfluous? all <html>'s child nodes
without a namespace prefix should inherit it's default namespace.

is there any way to remove these xmlns attributes without using
CreateElement for each element in the InnerXml?

david
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: James Thurley | last post by:
I'm creating an XmlDocument manually, adding content using the Xml classes such as XmlElement and XmlText, and I then write it out as as "text/xml" to the HttpResponse.Output TextWriter object...
1
by: Ian Lawton | last post by:
Hi, in a C# app, I have an XPathDocument that looks similar to this: <row uniqueid="4234" /> <row uniqueid="4365" /> <row uniqueid="3124" /> <row uniqueid="9879" /> <row uniqueid="1332" />...
2
by: Gabe Moothart | last post by:
Hi, I'm writing a program that loads an xml document, modifies it, validates it with an xsd schema, then send it on to another application for processing. I have no control over this aplication. ...
1
by: David | last post by:
I need to find a good online resource which teaches the use of the XmlDocument framework in more depth than is covered in MS's online doc. I need to create a multi-level XML document like the one...
5
by: Andy Fish | last post by:
Hi, I am using XmlDocument.WriteTo() and I find that the xml declaration is not included. at the moment I am doing this: XmlTextWriter xw = new XmlTextWriter(...); xw.WriteStartDocument();...
2
by: John Smith | last post by:
I'm writing webervice client using .Net 2.0. I have this class: public class MyWebService : SoapHttpClientProtocol { public XmlDocument validate(string url, XmlDocument xmlDocument) {...
2
by: Mr Flibble | last post by:
Hey Folks. If I want to create a simple XMLDocument that contains a few lines of XML which class should I use? I'm a bit confused of the differences and which class to use and when. Ta.
1
by: James Berry | last post by:
Is there a way I can write to an XmlDocument using an XmlWriter style interface efficiently in the framework 2.0? In 1.1, you could write to a MemoryStream then load that into an XmlDocument,...
5
by: Gustaf | last post by:
I've been trying to update my code to the new XslCompiledTransform class without success. The following method takes an XML and XSL file, and returns an XmlDocument object. Can anyone figure out...
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
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
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...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.