472,328 Members | 1,141 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

Sample code for how to load an XMLDocument with an XML file while simultaneously validating it against an XSD, AND how to read and search that same validated XML file.

Hi all,

Spent WAAAYYY too much time trying to figure this out because there's
not many good examples out there, so in the interest of sparing y'all
from suff'rin same, I've pasted it into eternity for you. Works like a

(I know, I know, I love you too)


//C# code

string filename = "whateverYouCalledYourXMLAndXSDfile"
XmlDocument loggingMessagesDefinition = new XmlDocument()
MessageDataXmlFile = filename + ".xml";
MessageDataXsdFile = filename + ".xsd";

// initialize the validation objects
XmlSchemaCollection myXmlSchemas = new XmlSchemaCollection();
new XmlTextReader(MessageDataXsdFile));
XmlValidatingReader myXmlValidatingReader =
new XmlValidatingReader(new XmlTextReader(MessageDataXmlFile));
myXmlValidatingReader.ValidationType = ValidationType.Schema;

//load and validate at same time. Throws exception if bad data
loggingMessagesDefinition.Load(myXmlValidatingRead er);

// set up namespace manager to enable XML searching
nsmgr = new XmlNamespaceManager(loggingMessagesDefinition.Name Table);
//chose df to represent default

//example of how to search and extract from XML doc
//notice the df prefixes to indicate the default namespace
string type = "2";
string messageSeverity =
loggingMessagesDefinition.DocumentElement.SelectSi ngleNode(
"descendant::df:LogMessage[df:ID='" + type +
"']", nsmgr).SelectSingleNode("df:Severity",
nsmgr).InnerText; // should return 'Not Good' as a result

// here's the XML file

<?xml version="1.0" encoding="UTF-8" ?>
<Data xmlns="http://tempuri.org/LoggingMessagesDefinition.xsd">
<Description>Unknown memory error</Description>
<Action>Phone appropriate personnel</Action>
<Description>Out of memory</Description>
<Severity>Not Good</Severity>
<Action>Phone appropriate personnel</Action>
// and here's the XSD file I used, little overly detailed for this
example but typically these can be auto-generated anyway (In visual
studio, look at you XML file in 'Data' mode and then right click the
grid and choose generate schema, which pumps out an XSD for ya)
<?xml version="1.0" ?>
<xs:schema id="Data"
attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="Data" msdata:IsDataSet="true"
<xs:choice maxOccurs="unbounded">
<xs:element name="LogMessage">
<xs:element name="ID" type="xs:positiveInteger" minOccurs="0" />
<xs:element name="Description" type="xs:string" minOccurs="0" />
<xs:element name="Class" minOccurs="0" msdata:Ordinal="2">
<xs:restriction base="xs:string">
<xs:enumeration value="Memory" />
<xs:enumeration value="Thread" />
<xs:enumeration value="File" />
<xs:enumeration value="Data" />
<xs:enumeration value="Network" />
<xs:enumeration value="Database" />
<xs:enumeration value="Application" />
<xs:element name="Severity" minOccurs="0" msdata:Ordinal="3">
<xs:restriction base="xs:string">
<xs:enumeration value="Error" />
<xs:enumeration value="Warning" />
<xs:enumeration value="Status" />
<xs:enumeration value="Metric" />
<xs:enumeration value="Debug" />
<xs:element name="Action" minOccurs="0" msdata:Ordinal="4">
<xs:restriction base="xs:string">
<xs:enumeration value="Phone appropriate personnel" />
<xs:enumeration value="None" />

May 5 '06 #1
0 1468

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

Similar topics

by: srijit | last post by:
Hello, Here is an example code of xml writer in Python+PythonNet, Ironpython and Boo. The codes look very similar. Regards, Srijit Python +...
by: serge calderara | last post by:
dear all, I have benn going through a .net possibility such as amanging XMLDocument and xml transmorf function. In that set of features I am...
by: Nikhil Patel | last post by:
Hi all, I use a Word Xml Template in ASP.Net application. Basically when a user clicks on a certain button on a web form, I open the template using...
by: Brian Miller | last post by:
I've been constructing an ASP.Net application using the 1.1 framework, and have been using Web Matrix for development purposes. Now that my...
by: ek03 | last post by:
I have a web application that saves/loads XML documents. On occasion, an error is logged on the call to XmlDocument.Load: "process cannot access the...
by: ss | last post by:
hi, can anybody gives me a sample code where the sql injection attack is validated. how can i do that in business logic layer and pass the...
by: josh.kuo | last post by:
Sorry about the subject, I can't think of a better one. I recently wrote some PHP classes that I think might be of interest to this group. Since...
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the...
by: engwar | last post by:
The title says it all. How do I use XMLDocument.Validate() against a DTD. In all my Googling about this I only find examples of how to validate...
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.