Problems validating an xml file without specifying the default namespace.

Hi All,

I'm trying to validate an xml document. I'm having trouble setting the
default namespace of the xml document. If I hard encode the namespace
in the xml file then everything works fine. But I can't do this
because it breaks our old tools which validate the xml to a dtd (error
saying the dtd doens't allow xmlns attribute on the root element). So
I'm trying to add the default namespace to the xml file by code. As
follows:


string DefaultNameSpace = "http://www.mydefaultnamespace.com";
NameTable nt = new NameTable();
	
XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace(string.Empty, DefaultNameSpace);
			
XmlSchemaCollection myXmlSchemaCollection = new
XmlSchemaCollection(nt);
myXmlSchemaCollection.ValidationEventHandler += new
ValidationEventHandler (this.ValidationEventHandle);
			
myXmlSchemaCollection.Add(DefaultNameSpace, new
XmlTextReader(lblSchemaLocation.Text));

XmlParserContext context = new XmlParserContext(nt, nsmgr, null,
XmlSpace.None);

myXmlValidatingReader = new
XmlValidatingReader(xmlstringtakenfromelsewhere, XmlNodeType.Document,
context);

myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);
myXmlValidatingReader.ValidationType = ValidationType.Schema;


I thought that by using the namespace manager that it would set the
default namespace of the xml document. When I run the above code I get
errors saying that it couldn't find the schema information for the
element defaultnamespace:elementname. No schema found to enforce
validation.
The default namespace is being used because it displays it in the
error message but not when it is trying to resolve the elements to the
schema.

I'm very confused can anyone point me in the right direction I seem to
be going round in circles.

Thanks in advance Adam
0
2/5/2004 10:55:54 AM
dotnet.xml 7266 articles. 0 followers. Follow

5 Replies
564 Views

Similar Articles

[PageSpeed] 27

As a very quick test, try associating the default namespace with some
string...
    nsmgr.AddNamespace("myns", DefaultNameSpace);

This is important in XPath selections, so it may do the trick for validation
too.

If not we can look further.

steven

"Adam Child" <adam.child@oxon.blackwellpublishing.com> wrote in message
news:93115510.0402050255.593d76ea@posting.google.com...
> Hi All,
>
> I'm trying to validate an xml document. I'm having trouble setting the
> default namespace of the xml document. If I hard encode the namespace
> in the xml file then everything works fine. But I can't do this
> because it breaks our old tools which validate the xml to a dtd (error
> saying the dtd doens't allow xmlns attribute on the root element). So
> I'm trying to add the default namespace to the xml file by code. As
> follows:
>
>
> string DefaultNameSpace = "http://www.mydefaultnamespace.com";
> NameTable nt = new NameTable();
>
> XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt);
> nsmgr.AddNamespace(string.Empty, DefaultNameSpace);
>
> XmlSchemaCollection myXmlSchemaCollection = new
> XmlSchemaCollection(nt);
> myXmlSchemaCollection.ValidationEventHandler += new
> ValidationEventHandler (this.ValidationEventHandle);
>
> myXmlSchemaCollection.Add(DefaultNameSpace, new
> XmlTextReader(lblSchemaLocation.Text));
>
> XmlParserContext context = new XmlParserContext(nt, nsmgr, null,
> XmlSpace.None);
>
> myXmlValidatingReader = new
> XmlValidatingReader(xmlstringtakenfromelsewhere, XmlNodeType.Document,
> context);
>
> myXmlValidatingReader.Schemas.Add(myXmlSchemaCollection);
> myXmlValidatingReader.ValidationType = ValidationType.Schema;
>
>
> I thought that by using the namespace manager that it would set the
> default namespace of the xml document. When I run the above code I get
> errors saying that it couldn't find the schema information for the
> element defaultnamespace:elementname. No schema found to enforce
> validation.
> The default namespace is being used because it displays it in the
> error message but not when it is trying to resolve the elements to the
> schema.
>
> I'm very confused can anyone point me in the right direction I seem to
> be going round in circles.
>
> Thanks in advance Adam


0
2/5/2004 11:08:34 AM
Adam Child wrote:

> myXmlValidatingReader = new
> XmlValidatingReader(xmlstringtakenfromelsewhere, XmlNodeType.Document,
> context);

I think the mistake is here. Try instead
myXmlValidatingReader = new
XmlValidatingReader(xmlstringtakenfromelsewhere, XmlNodeType.Element,
context);

-- 
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
0
Oleg
2/5/2004 1:12:13 PM
Sorry guys neither of these two methods seemed to fix it.

Not sure how giving the default namespace a name would change it
because the xml wouldn't have this prefix on any of the elements.

Changing the nodetype to element throws up an error about the dtd.
Sorry didn't make it very clear about what the xml string was.

Here is a simplified version of the xml document that i'm trying to
validate:

<?xml version='1.0'?>
<!DOCTYPE content PUBLIC "//BLACKWELL PUBLISHING GROUP//DTD 4.0//EN"
"http://www.mydefaultnamespace.com/conent.dtd" []>
<rootnode>
<element1></element1>
<element2></element2>
</rootnode>

If I change the document to the following then it all works fine. But
when I try to parse it to the dtd then this complains that the
attribute xmlns is not allowed. So this isn't an option.

<?xml version='1.0'?>
<!DOCTYPE content PUBLIC "//BLACKWELL PUBLISHING GROUP//DTD 4.0//EN"
"http://www.mydefaultnamespace.com/conent.dtd" []>
<rootnode xmlns="http://www.mydefaultnamespace.com">
<element1></element1>
<element2></element2>
</rootnode>

Sorry won't include the schema it's very complicated but the general
structure is as follows:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
		targetNamespace="http://www.mydefaultnamespace.com/"
            xmlns="http://www.mydefaultnamespace.com/"
		elementFormDefault="qualified">
</xsd:schema>

Is there some rule about the XmlValidatingReader not using the default
namespace from the XmlNamespaceManager when validating against the
schema?


"Oleg Tkachenko [MVP]" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message news:<#M58dl#6DHA.1428@TK2MSFTNGP12.phx.gbl>...
> Adam Child wrote:
> 
> > myXmlValidatingReader = new
> > XmlValidatingReader(xmlstringtakenfromelsewhere, XmlNodeType.Document,
> > context);
> 
> I think the mistake is here. Try instead
> myXmlValidatingReader = new
> XmlValidatingReader(xmlstringtakenfromelsewhere, XmlNodeType.Element,
> context);
0
2/6/2004 11:58:55 AM
Adam Child wrote:

> Sorry guys neither of these two methods seemed to fix it.

Actually I just tried it and it works. Should be some silly typo somewhere.

> Not sure how giving the default namespace a name would change it
> because the xml wouldn't have this prefix on any of the elements.
foo element in a namespace and foo element in no namespace have nothing 
in common except local name.

> Sorry won't include the schema it's very complicated but the general
> structure is as follows:
> 
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> 		targetNamespace="http://www.mydefaultnamespace.com/"
>             xmlns="http://www.mydefaultnamespace.com/"

Aha!!!

 > string DefaultNameSpace = "http://www.mydefaultnamespace.com";

See the difference? That should be the reason.
-- 
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
0
Oleg
2/8/2004 10:29:02 AM
Adam Child wrote:

> I'm trying to validate an xml document. I'm having trouble setting the
> default namespace of the xml document. If I hard encode the namespace
> in the xml file then everything works fine.

brw, why don't you make your schema chameleon one (no target namespace)?
It's really weird your schema describes a namespace, while instance 
documents are in no namespace.
-- 
Oleg Tkachenko [XML MVP, XmlInsider]
http://blog.tkachenko.com
0
Oleg
2/9/2004 12:45:55 PM
Reply:

Similar Artilces:

Problem shortcuts in Word
Hello, I work with Word 2008. In the beginning there is no problem, but suddenly i have problem with shortcuts. When i want to do "cmd c" it make an other shortcuts same thing for "cmd s", "cmd v" and surly some other. But when i write there is no problem "c" is "c"... If somebody have an idea please help me. Thank you. Unless you indicate your specific update level of Office as well as OS X & describe what the keystrokes *are* doing contrary to what you expect there isn't much we can offer. -- Regards |:>)...

encoding problem in Outlook 2007 importing Outlook 2000 personal folders
I use Outlook 2007 to import the pst file of Outlook 2000. The Chinese characters in the subject became unreadable but it's ok to display in message body. In Outlook 2000, there's no problem to display the Chinese characters in the subject. What's wrong with Outlook 2007? What setttings should I change to fix the problem? Thanks. cyl <u8526505@gmail.com> wrote: > I use Outlook 2007 to import the pst file of Outlook 2000. The Chinese > characters in the subject became unreadable but it's ok to display in > message body. In Outlook 2000, there's no problem t...

Outlook 2003 PST problems
I just installed Office 2003 professional on a new computer & am having problems with outlook. I copied my Outlook.pst file into the appropriate directory & although it is recognized the calendar is not working properly. I cannot attach any of the colored labels to my appointments - so everything is pretty much in black & white. I totally uninstalled & reinstalled office... first removing my pst file... but there appears to be an ini file... or something that is retaining some information... does anyone know of a fix for the labels... or how to TOTALLY uninstall office (so I...

Problem with Authentication
The CRM application was running smooth. But now I can't logging. It ask my credentials and than return a 401.1 error. I had already reinstalled the CRM server but it won't work. I'm accessing it in the same local network. I'm running CRM 3.0 in SBS. Best regards, Erico Hi When you open a Internet explorer and type the name of the CRM server, it prompts for user and password ? Are you logged in with domain credentials ( AD user ) ? If you are, so add url of the crm server in Local intranet. Enable "Automatic logon only in intranet", this is under security,c...

Vista blue screen problem...please help!
I've seen some other threads, here are the dump files: 'RapidShare: 1-CLICK Web hosting - Easy Filehosting' (http://rapidshare.com/files/347103149/Minidump.rar.html) Please, help me out here. -- ceVil It might be better to know at what point the bsod occurs and what the actual err.msg given is "ceVil" <guest@unknown-email.com> wrote in message news:0b9a0c60b90bf6c0be88bda30f2820ab@nntp-gateway.com... > > I've seen some other threads, here are the dump files: > 'RapidShare: 1-CLICK Web hosting - Easy Filehosting' > (...

calendar problem
Dear all, I am using both mail and calendar function in outlook 2002. Everything is ok until recently, when I try to make a new appointment or checking any old appointments, an error message "out of memory or resource, try to close some windows..." pop up. The mail function is still ok. Can anyone tell me how can I solve this? I have already installed sp2. The version of office is the professional one with frontpage. Thanks. tp ...

format of emailed excel file
In excel 2003 when I email a a particulat worksheet in a workbook I lose all the formating when it is recieved in Outlook 2003. Is there anyway to preserve the excel "look"? Thnaks, Craig S ...

problem #4
Hi, I am facing this new problem... i.e a user has installed Office 2003 on win2k prof platform.. whenever she is working on a outlook.. she gets a Microsoft error message... saying to send the report or don't send the report... Usually I have seen this problem in IE... But for outlook it is new to me... after clicking on send or don't send report.. the outlook closes. And there are no IE open..... and if atall it is open.. the IE does not close... Need your help regards, KAH What is logged to the Event Viewer regarding this? Try one of the following already; http://www.howt...

Show file location in title bar?
Is there a way to make Excel or Word show the location of a file in the title bar. Right now, it just shows the name of the file. I was thinking in 2003, it showed the file location, or perhaps there was an option to show it? I can't find the same thing in 2007 and it would be extremely helpful to have it! I use template files with the same name (for easy searching) but save in different client folders, which is why I want it to display the full path. Otherwise, I wouldn't care. Thanks!! Add "Document Location" to the QAT. You'll find it in the "Com...

95 and 97 problem
For some unknown reason there is Excel 95 and 97 on this pc.When i double click on an Excel file, the pc defaults to the 95 program (Which incidently doesn't work properly). So my question is how do i get the pc to default to 97. Cheers Craig Craig, Have a look at Windows Help. From Windows 2000 Help (Index under Programs Associating with File Types) To change which program starts when you open a file In My Computer or Windows Explorer, on the View menu, click Folder Options. Click the File Types tab. In the list of file types, click the one you want to change. Click Edit. In Act...

Date problem
I can't believe there's no post on this, but I can't find it. I've tried the solutions I could find (see below) and still get the error: "The expression is typed incorrectly or it is too complex to be evaluated. For example....Try simplifying...." I have a date field, formatted as Date/Time, General Date, default Value = Now(). I like having the date and the time - in case we need it. I want to run a report on calls taken just for one day. I have CallDate: CallDateAndTime in my query, with "criteria" as Between ([Forms]![frmReports]![txtStartDate]) A...

sync problems
I have all of my info on an HP Ipac. I had to reformat the hard drive and I did not make a back-up file. I am trying to move everything back to the computer but it says there is an error with syncing it. How can I move everything from the PDA back to Outlook again? Please help- thanks ...

how to read XML with entities using C#
Hi there, I have two xml files, one is a master file and the other is just a fragment of xml. Master xml file uses 'DOCTYPE' to define the other file as an entity. Then, the master uses entity references that are supposed to be expanded into real content at parsing time. Examples are provided below. When I open master xml file in InternetExplorer , IE shows correct content. All the entities are transformed into right xml. So far I have been unsuccessfull in getting similar result when I try to do the same programmatically using C#. I was trying to use XmlDocument and XmlVal...

Office 2007 - HELP
I have used Word for many years and love it - not too thrilled with the changes in Office 2007, but my bigger issue is with Publisher 2007. I have an image that I have used in Word just fine - meaning, the on screen colors are correct and when printed the colors are correct. Someone sent me a Publisher file and asked me to add the image - I did this - now the image is messed up - the colors are correct on screen, but when printed FROM MY computer the image colors are all wrong - but if I transport the file to a different computer it prints fine I have reinstalled Office - no good Any id...

Installation Problem!!
I am having a problem installing MSCRM 1.2. My configuration consists of a Domain Controller running Win 2000 Server SP4 . I am trying to install MSCRM on a member of the domain running Windows 2000 Server SP4. There is a DNS Server on the DC which is integrated with AD, and a SQL Server 2000 running on the Win 2K machine. The install errors out when CRM creates the root business with the message: Setup was unable to install Microsoft CRM Server. Setup was unable to provision your organization. Setup was unable to create the root business. The configuration of ASP.NET seems to be ok, the pr...

Problems Creating a disclaimer in Exchange 2003
I am trying to create a server based disclaimer that will stamp all of my smtp emails going to the internet. I have been following KB article 317680 with no luck. I get an error like the one below. Binding Display Name Specified: smtpscriptinghost ** Registration Failed ** Err.Number (HRESULT) = 0x1AD Err.Description = ActiveX component can't create object ProgID = cdo.ss_smtponarrival COM Category = {FF3CAA23-00B9-11d2-9DFB-00C04FA322BA} Corresponding Event = onarrival ** Have you registered your sink COM class on this machine? I am puttin...

Resource editor problems
Hello all, Under VC++ 7.1... Please consider these two lines: IDC_ARROW_ADD_CANCEL DISCARDABLE "res\\cur00004.cur" IDC_ARROW_ADD_CANCEL CURSOR DISCARDABLE "res\\cur00004.cur" The second is a hand-edited change to the first. Both will compile just fine in the resource compiler. However, after I use a text editor to make this change, I cannot open the .rc file in the resource editor. The resource editor issues the following error open attempting to open the .rc file: error RC2135 : file not found: CURSOR Can anyone shed light on what's going on here? Thanks, Dave ...

Directory Replication Problem #2
Hi, recently my Exchange Server directory database receives changes from other servers but does not send out its own changes. Check from the knowledge base, to correct the USN discrepancy need to use Authrest.exe (need to amke changes for about 100+ users). Does anybody know where to get this file 'Authrest.exe' for exchange server 5.5? Exchange server 5.5 CD only provide this file for ver.4.0. Have anyone use it before? Regards, "Sharon Tan" <sharon_tansk@yahoo.com.sg> wrote: >Hi, recently my Exchange Server directory database >receives changes from ot...

Software of scripting to grab data from MS word file and fill in Excel fields?
I have a 90 page financial report from MS word that I get each month. I would like to find a software package or scripting tool that will allow me to automate the process of grabbing #s from the ms word file and filling in fields in an excel file. I have a feel that there may be quite of programming involved in doing this. But perhaps there are some more efficient solutions out there. I would appreciate any advice. Thanks Hi, Do the numbers appear in the same part of the word document each tim (eg always in the same row/column of the same table). If so, copyin the data into excel should be ...

Cell Format
I have a spreadsheet with cells that I'm trying to type the date into. I type in the date exactly like this: 09/26/03. When I hit "enter" MS Excel displays "37890" in the cell. No matter what I do I can't the cell to show: 09/26/03. I think that somehow I need to "strip" the cell of what it's original format is, but I can't figure out how. I try to use the Date option in the format cell screen but it doesn't work. Curtis I tried this and it didn't work. I also tried the "Text to columns" feature. No success. >...

User Defined Variables in MS query cause a problem to import data
If I use user defined variables in MS query to import data into excel sheet, the query executes but no data gets imported into the excel sheet. If I get rid of the variable the results can be imported into the Excel sheet. ...

How do I keep the active file open after saving copy, please?
I am using Excel 97 with OS WINNT. I have recorded a macro which copies the first sheet called "Daily from my source workbook to a new workbook, names this new workbook "PO Commencements (Today's date)".xls, and closes the new workbook. It i also closing the source workbook. I would like to keep the source workbook open after the copyin process. Alternatively, automatically reopen the source workbook a there is another step to be undertaken before I finish with the boo for the day. If I perform these steps manually, only the destinatio workbook closes and I can continue w...

publisher file type
Publisher 2007 files open as WORD files. Files saved in Publisher save as WORD files even though Publisher is selected in Save As Well only for you. But before we try and guess what you are doing wrong maybe you could supply a little more information about the version you are using and what you are really doing because it seems to be a problem unique to you. John G. On Tue, 26 Jan 2010 18:54:01 -0800, RandyP <RandyP@discussions.microsoft.com> wrote: >Publisher 2007 files open as WORD files. Files saved in Publisher save as >WORD files even though Publisher is...

outlook web access redirect problem
hi, I have been trying to redirect outlook web access (OWA) from the IIS root folder to the /exchange folder. I have followed the instructions on going into the web site and redirecting to a folder but I just get the following URL when I browse to the root of my webserver : http://webmail/exchange/exchange - it's like it's doubling up somehow. I have it set to : A redirection to a URL redirect to: /exchange client will be sent to: A directory below URL entered I just can't work it out... any ideas... cheers Baronne Which instructions did you follow? I wrote the below articl...

Problem after sorting
In my Excel 2007 workbook I have two sheets The "Master Names" sheet has columns: (A) First|(B) Last (C) First (B) Last (concatenated) The "Selected Names" sheet contains (A) cells which link to selected (C) First Last (concatenated) cells in the Master Names sheet. Everything links and displays fine except when I add names to the bottom of the Master Names sheet and then sort using (B) Last name column. When I do this I get a 0 (zero) in the (A) First Second cells in the Selected names sheet and other cells in this sheet have the wrong name. Obviously I am doin...