473,395 Members | 1,554 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,395 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 4154


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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.