473,385 Members | 1,661 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,385 software developers and data experts.

Namespace exploring with XmlDocument

If I have an XmlDocument and I want to discover which namespaces it has, how
should I do? And is the schemaLocation attribute automatically expanded
somewhere? (the schemaLocation has this format:
namespacename filename [namespacename filename] [namespacename filename]
.... )
I want to validate the XML file to its namespaces, but if I can't find which
namespaces it has, how should I do?
(and I need the name of the various namespaces for other reasons).

--- bye
Jun 9 '07 #1
4 2461
The namespaces are nothing more than unique identifiers for the schema
that you expect. Having the namespace itself doesn't do anything. Rather,
you need to have the schema beforehand if you are going to do any sort of
validation.

If you want to get all the namespaces that are in the document, then you
will have to cycle through all the nodes in the document and then get the
namespaces.

You ^might^ be able to get the namespaces after parsing by using the
XmlNameTable instance that the document exposes (it might be in there).
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"MaxMax" <no**@none.comwrote in message
news:u5*******************@twister2.libero.it...
If I have an XmlDocument and I want to discover which namespaces it has,
how should I do? And is the schemaLocation attribute automatically
expanded somewhere? (the schemaLocation has this format:
namespacename filename [namespacename filename] [namespacename filename]
... )
I want to validate the XML file to its namespaces, but if I can't find
which namespaces it has, how should I do?
(and I need the name of the various namespaces for other reasons).

--- bye

Jun 9 '07 #2
Hi Max,

I agree with Nicholas. For XML Schema validation, you should manually add
the schemaset you want to validate the document against before the
validation execute(through the XmlReader's Settings in .net framework 2.0).
e.g.

===================
XmlSchemaSet sc = new XmlSchemaSet();

// Add the schema to the collection.
sc.Add("urn:bookstore-schema", "books.xsd");

// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += new ValidationEventHandler
(ValidationCallBack);

// Create the XmlReader object.
XmlReader reader = XmlReader.Create("booksSchemaFail.xml", settings);

// Parse the file.
while (reader.Read());
======================

Also, if you need to dynamically locate all the declared xml schemas in a
xml document, you may consider use xmlReader to loop through the XML
document and find all the namespaces declareation in all the nodes before
perform validation. This is may require an additional loop through the
document.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.


Jun 11 '07 #3
Hi Max,

Have you got any further ideas on this or does the suggestion in former
messages help you some? If there is anything else we can help, please feel
free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
This posting is provided "AS IS" with no warranties, and confers no rights.

Jun 13 '07 #4
MaxMax wrote:
If I have an XmlDocument and I want to discover which namespaces it has, how
should I do? And is the schemaLocation attribute automatically expanded
somewhere? (the schemaLocation has this format:
namespacename filename [namespacename filename] [namespacename filename]
... )
I want to validate the XML file to its namespaces, but if I can't find which
namespaces it has, how should I do?
(and I need the name of the various namespaces for other reasons).
If you want to validate against the schemas listed in the schemaLocation
attribute then you need to use XmlReaderSettings with the
ValidationFlags set to include ProcessSchemaLocation so like this
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.ValdiationFlags |=
XmlSchemaValidationFlags.ProcessSchemaLocation;
readerSettings.ValidationEventHandler += delegate (object sender,
ValdiationEventArgs vargs) {
// handle validation errors here
}
using (XmlReader xmlReader = XmlReader.Create(@"file.xml",
readerSettings)) {
while (xmlReader.Read()) {}
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jun 13 '07 #5

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

Similar topics

1
by: Izvra | last post by:
How can i specify default XML namespace when it does not declared in the xml document I need it for validation against xml schema @@@I have a procedure Sub ValidateXMLDocument(ByRef XMLDocument...
5
by: jrmsmo | last post by:
Hi I have a document as follows: <?xml version="1.0"?> <metadata xml:lang="en"> </metadata> I want to change the document so it looks as follows: <?xml version="1.0"?> <metadata...
1
by: Seong-Tae Jeong | last post by:
for example, xml document is below, It has a default namespace "xmlns='qwer://test'". string xmlText = "<test xmlns='http://test'><clear/><clear/></test>"; I would like to select node list...
2
by: Matt Torline via .NET 247 | last post by:
I am trying to duplicate an xml document, and I am pretty new to xml. I am using hte Xml namespace and taking advantage of the xmldocument class. I would like to add the following to an element ...
3
by: JT | last post by:
Hi all, I have a pretty basic question... I have a method (below) which uses ExecuteXMLReader to load the output from a FOR XML Explicit stored procedure into an XML document, and then save the...
4
by: David Grogan | last post by:
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...
3
by: Andy Fish | last post by:
Hi, I am creating a new XmlDocument from scratch, but I can't see how I can make a namespace declaration and associate a prefix with it. For instance in the following example: XmlDocument doc...
0
by: XML newbie: Urgent pls help! | last post by:
I am using VB.Net. My program is to connect to a remote IPAddress. Once, it verifies the login information it should display the SessionID and enable some button . I appreciate your help and thanku...
4
by: XML newbie: Urgent pls help! | last post by:
I am using VB.Net. My program is to connect to a remote IPAddress. Once, it verifies the login information it should display the SessionID and enable some button . I appreciate your help and thanku...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.