469,913 Members | 2,043 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,913 developers. It's quick & easy.

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 1378

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by serge calderara | last post: by
2 posts views Thread by Nikhil Patel | last post: by
3 posts views Thread by ek03 | last post: by
3 posts views Thread by josh.kuo | last post: by
232 posts views Thread by robert maas, see http://tinyurl.com/uh3t | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.