By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,510 Members | 1,821 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,510 IT Pros & Developers. It's quick & easy.

XmlDocument gives error: "... is an undeclared namespace"

P: n/a
My application produces XML Data files which have XML namespace qualified XML
elements (nodes), but the namespace itself is not declared in the data file.

My task is to read these data files in a C# app for viewing purposes. I am
using "XmlDocument" and "DataSet" for displaying the XML data in the Data
grid.

Problem:
If I add the Namespace declaration to the data file itself (by manually
opening the file and adding a new Root element that defines the Namespace)
then I have no issues in displaying the data. But I want to programmatically
take care of declaring the Namespace in the DataViewer application without
tampering the Data files themselves.

Can someone indicate how I can overcome the problem.

Thanks.

--
Regards
Senthil SS.
Nov 12 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
SenthilSS wrote:
My application produces XML Data files which have XML namespace qualified XML
elements (nodes), but the namespace itself is not declared in the data file.


That means that's malformed XML. Fix it, that's the solution. And better
use standard XML API such as XmlTextWriter class to create XML - it
always knows better how to produce well-formed XML.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #2

P: n/a
* Oleg Tkachenko [MVP] wrote in microsoft.public.dotnet.xml:
My application produces XML Data files which have XML namespace qualified XML
elements (nodes), but the namespace itself is not declared in the data file.


That means that's malformed XML. Fix it, that's the solution. And better
use standard XML API such as XmlTextWriter class to create XML - it
always knows better how to produce well-formed XML.


Missing namespace declarations do not render XML documents malformed,
they are still well-formed but not namespace-well-formed. An XML pro-
cessor that does not support XML Namespaces would not fail to pro-
cess such resources. That does not help here though (unless you can
disable namespace support for System.Xml), so fixing the document one
way or the other is probably the best approach.
--
BjŲrn HŲhrmann ∑ mailto:bj****@hoehrmann.de ∑ http://bjoern.hoehrmann.de
Weinh. Str. 22 ∑ Telefon: +49(0)621/4309674 ∑ http://www.bjoernsworld.de
68309 Mannheim ∑ PGP Pub. KeyID: 0xA4357E78 ∑ http://www.websitedev.de/
Nov 12 '05 #3

P: n/a
Bjoern Hoehrmann wrote:
Missing namespace declarations do not render XML documents malformed,
they are still well-formed but not namespace-well-formed. An XML pro-
cessor that does not support XML Namespaces would not fail to pro-
cess such resources. That does not help here though (unless you can
disable namespace support for System.Xml), so fixing the document one
way or the other is probably the best approach.


Yeah, good catch.
But Senthil does use namespaces, just undeclared ones.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #4

P: n/a
Hi, I greatly appreciate your responses.

The application that generates the XML data does not declare the namespace
also in the document because during successive restarts of the application
the XML data will continue to get appended to the existing file itself.

I am afraid my original question, "whether it is possible to
programmatically take care of the XML Namespace declaration inside of the
viewer application", still remains unanswered.

Sample XML Data file:
<log4net:event
logger='Debug'
timestamp='2004-12-07T13:25:00.1590000+05:30'
level='DEBUG'
thread='2852'
domain='domain-Elind.CSF.UnitTests.exe'
username='ELIND\senthilss'>
<log4net:message>
<log4net:Object>System.String</log4net:Object>
<log4net:Field Name='String' Value='Severity [Debug] is set.'/>
</log4net:message>
</log4net:event>

<log4net:event
logger='Debug'
timestamp='2004-12-07T13:25:00.2527500+05:30'
level='DEBUG'
thread='2852'
domain='domain-Elind.CSF.UnitTests.exe'
username='ELIND\senthilss'>
<log4net:message>
<log4net:Object>Elind.CSF.UnitTests.BaseClass</log4net:Object>
<log4net:Field Name='ibc' Value='-1'/>
<log4net:Field Name='sbc' Value='Some String in BaseClass'/>
<log4net:Node Name='InData'>
<log4net:SubField Name='SomeLine' Value='zyxw'/>
</log4net:Node>
<log4net:Node Name='OutData'>
<log4net:SubField Name='SomeLine' Value='fed cba'/>
</log4net:Node>
<log4net:Field Name='i' Value='-2'/>
</log4net:message>
</log4net:event>

C# viewer code snippet:
public class frmLogViewer : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid logView;
DataSet logXMLDataSet = new DataSet("LogViewer");
logXMLDataSet.ReadXml("Data.xml");
logView.DataSource = logXMLDataSet;
}

I am new to XML, and to ask the question once again, Is there any way I can
avoid tampering the Data file for the sake of getting the XML namespace
declaration into the Data Viewer application?

Thanks,
Senthil SS.

"Bjoern Hoehrmann" wrote:
* Oleg Tkachenko [MVP] wrote in microsoft.public.dotnet.xml:
My application produces XML Data files which have XML namespace qualified XML
elements (nodes), but the namespace itself is not declared in the data file.


That means that's malformed XML. Fix it, that's the solution. And better
use standard XML API such as XmlTextWriter class to create XML - it
always knows better how to produce well-formed XML.


Missing namespace declarations do not render XML documents malformed,
they are still well-formed but not namespace-well-formed. An XML pro-
cessor that does not support XML Namespaces would not fail to pro-
cess such resources. That does not help here though (unless you can
disable namespace support for System.Xml), so fixing the document one
way or the other is probably the best approach.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/

Nov 12 '05 #5

P: n/a
SenthilSS wrote:
The application that generates the XML data does not declare the namespace
also in the document because during successive restarts of the application
the XML data will continue to get appended to the existing file itself.
You should understand that as per XML Namespaces spec undeclared
namespace prefix is a fatal error. That effectively means this document
is malformed and mustn't be processed by any XML (namespace-aware)
parser. Yes, XML is using such draconian rules by design for the sake of
interoperability.
You better fix that application - use standard XML API such as
XmlTextWriter - it takes care about all XML syntax issues for you (and
usually better than you).
I am afraid my original question, "whether it is possible to
programmatically take care of the XML Namespace declaration inside of the
viewer application", still remains unanswered.


Well, probably it could be feasible to add namespace declaration using
namespace-unaware XML parser, but .NET doesn't have such one. You still
can add appropriate namespace using non-XML tools - some trivial text
processing ones such as regexp.

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.