473,569 Members | 2,870 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.dt d">
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.Cre ateDocumentFrag ment 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 4171


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 = xmlDocumentInst ance.CreateElem ent("", "p",
"http://www.w3.org/1999/xhtml");

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

const string xhtml1Namespace URI = "http://www.w3.org/1999/xhtml";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa dXml(@"<html xmlns=""http://www.w3.org/1999/xhtml"">
<head>
<title>Exampl e</title>
</head>
<body>
<h1>Example</h1>
</body>
</html>");
xmlDocument.Sav e(Console.Out);

XmlElement paragraph = xmlDocument.Cre ateElement("", "p",
xhtml1Namespace URI);
paragraph.Appen dChild(xmlDocum ent.CreateTextN ode("Kibology for
all."));
XmlElement body = xmlDocument.Get ElementsByTagNa me("body",
xhtml1Namespace URI)[0] as XmlElement;
if (body != null) {
body.AppendChil d(paragraph);
xmlDocument.Sav e(Console.Out);
}

outputs

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Exampl e</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.dt d">
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 = xmlDocumentInst ance.CreateElem ent("", "p",
"http://www.w3.org/1999/xhtml");
I'm not using XmlDocument.Cre ateElement, here's a more detailed example...

input xml:
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:r="urn:te st">
<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.OwnerDocum ent.CreateDocum entFragment
newNode.InnerXm l = newInnerXml
node.ParentNode .ReplaceChild(n ewNode, node)

CreateDocumentF ragment 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:te st">
<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
1633
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 under IIS. The problem I get is when I create an XmlElement with an XmlAttribute whose value contains the "&" character. When the xml is written the...
1
4373
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" /> <row uniqueid="4322" />
2
15759
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. The problem is this: for some reason, this application rejects any xml documents with a namespace (as crazy as that sounds). So I need to strip...
1
1608
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 listed below. Using XmlDocument, I can create the <books> and the individual <name> and <price> elements, but I am unsure how to create the...
5
2516
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(); doc.WriteTo(xw);
2
13566
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) { this.Url = url;
2
4286
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
5945
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, but this was pretty inefficient. The XmlNodeWriter sample helped here and I was expecting that to appear in the 2.0 framework but it doesn't appear...
5
6180
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 how to write the parameters to the XslCompiledTransform.Transform() method in this case? static XmlDocument Transform(string xml, string xsl) {...
0
7693
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2105
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
933
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.