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

XMLValidatingReader.Read() question

i am using an XMLValidatingReader in a manner similar to the following
code example:

string sReturn = "";

XmlValidatingReader oXML = new XmlValidatingReader(sXMLString,
XmlNodeType.Document, null);
oXML.Schemas.Add("", sSchemaURL);
oXML.ValidationType = ValidationType.Schema;
oXML.ValidationEventHandler += new
ValidationEventHandler(ValidationHandler);

while(oXML.Read())
{
if (oXML.NodeType == XmlNodeType.Element)
{
sReturn = sReturn + "Name: " + oXML.LocalName + "<br>";
sReturn = sReturn + "Value: " + oXML.Value + "<br><br>";
}
}

using the following sample schema:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="myobject">
<xs:complexType>
<xs:sequence>
<xs:element name="objectid" type="xs:integer" />
<xs:element name="objectdescription" type="xs:string" />
</xs:sequence>
</xs:complextType>
</xs:element>

and the following sample instance document:

<?xml version="1.0" encoding="utf-8" ?>
<myobject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Schema.xsd">
<objectid>1</objectid>
<objectdescription>Red</objectdescription>
</myobject>

i am getting the following results as output:

Name: myobject
Value:

Name: objectid
Value:

Name:
Value: 1

Name: objectid
Value:

Name: objectdescription
Value:

Name:
Value: Red

Name: objectdescription
Value:

Name: myobject
Value:

my question is: is there a way to write the xml elements such that the
names of elements are matched up with their values? as it is, i can fix
this with an extra Read statement like so:

while(oXML.Read())
{
if (oXML.NodeType == XmlNodeType.Element)
{
sReturn = sReturn + "Name: " + oXML.LocalName + "<br>";
oXML.Read();
sReturn = sReturn + "Value: " + oXML.Value + "<br><br>";
}
}

but that seems like it should be unnecessary. it seems like the element
name should already be matched with the element value?

thanks for any help / guidance,

jason

Nov 12 '05 #1
3 2520


jason wrote:
i am using an XMLValidatingReader in a manner similar to the following
code example:

string sReturn = "";

XmlValidatingReader oXML = new XmlValidatingReader(sXMLString,
XmlNodeType.Document, null);
oXML.Schemas.Add("", sSchemaURL);
oXML.ValidationType = ValidationType.Schema;
oXML.ValidationEventHandler += new
ValidationEventHandler(ValidationHandler);

while(oXML.Read())
{
if (oXML.NodeType == XmlNodeType.Element)
{
sReturn = sReturn + "Name: " + oXML.LocalName + "<br>";
sReturn = sReturn + "Value: " + oXML.Value + "<br><br>";
I think you misunderstand what the value of an element node is, here is
the documentation
<http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlxmltextreaderclassvaluetopic.asp>
there you will see that for an element node the Value property is an
empty string.

<xs:element name="objectid" type="xs:integer" />
<xs:element name="objectdescription" type="xs:string" />
</xs:sequence> <myobject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Schema.xsd">
<objectid>1</objectid>
<objectdescription>Red</objectdescription>
</myobject> my question is: is there a way to write the xml elements such that the
names of elements are matched up with their values? as it is, i can fix
this with an extra Read statement like so:

while(oXML.Read())
{
if (oXML.NodeType == XmlNodeType.Element)
{
sReturn = sReturn + "Name: " + oXML.LocalName + "<br>";


If it is an element with simple content (no child elements just text
content) then you can use the method ReadElementString e.g.
Console.Write("Text content is {0}.", oXML.ReadElementString());
but note that the method throws an exception if the content is not simple.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2


jason wrote:

using the following sample schema:

<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="myobject">
<xs:complexType>
<xs:sequence>
<xs:element name="objectid" type="xs:integer" />
<xs:element name="objectdescription" type="xs:string" />
</xs:sequence>
</xs:complextType>
</xs:element>

and the following sample instance document:

<?xml version="1.0" encoding="utf-8" ?>
<myobject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="Schema.xsd">
<objectid>1</objectid>
<objectdescription>Red</objectdescription>
</myobject>


Just as a note, the .NET framework in version 2.0 has better type
support there you could do for instance

string xmlURL = args.Length > 1 ? args[0] : @"test2005042902.xml";
XmlReaderSettings xmlReaderSettings = new XmlReaderSettings();
xmlReaderSettings.ValidationType = ValidationType.Schema;
xmlReaderSettings.ValidationFlags =
XmlSchemaValidationFlags.ProcessSchemaLocation;

XmlReader xmlReader = XmlReader.Create(xmlURL, xmlReaderSettings);

while (xmlReader.Read()) {
if (xmlReader.NodeType == XmlNodeType.Element) {
Console.WriteLine("Reading element with name \"{0}\".",
xmlReader.Name);
switch (xmlReader.Name) {
case "objectid":
Console.WriteLine("Integer value is: {0}.",
xmlReader.ReadElementContentAsInt());
break;
case "objectdescription":
Console.WriteLine("String value is {0}.",
xmlReader.ReadElementContentAsString());
break;
}
}
}

xmlReader.Close();

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #3
ahhh, ReadElementContent methods, not Value properties. that was the
mistake i was making. Thank you for the reply.

Nov 12 '05 #4

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

Similar topics

1
by: Dmitry Martynov | last post by:
Hi I have a question whether XmlValidatingReader doesn't check keyref constrain (the same with key constraint) or I do smth wrong. I have the following schema <?xml version="1.0"...
18
by: Vlad | last post by:
I have the following code: I have a local copy of the DTD that I need to validate incoming XML documents against. The XML document has the <!DOCTYPE myname SYSTEM "myfile.dtd"> define. When the...
3
by: Nuevo Registrado | last post by:
All, I am having a tough time figuring out how to code a simple app to read an xml file and an xsd file and validate the xml file using the xsd without using a namespace for the schema. Help?...
1
by: George W. | last post by:
When attempting to validate an XML file, If the file is valid, it validates correctly, and it will catch most ValidationEvents without problem. However, I keep getting the following Exception:...
9
by: jason | last post by:
how do you use the XmlValidatingReader to validate an XML document that is passed into the XmlValidatingReader constructor? it looks like the normal process is to use an underlying reader, as...
5
by: Geoff | last post by:
I am using an XMLValidatingReader to validate an XML file received via a web service. I want to verify that the incoming file matches the XML schema. When testing the validation routine, the...
1
by: Plop69 | last post by:
need some help on following: xml file 1 <TEST xmlns="http://test" > <OK>mlkddflmkj</OK> </TEST> xml file 2
12
by: Plop69 | last post by:
need some help on following: xml file 1 <TEST xmlns="http://test" > <OK>mlkddflmkj</OK> </TEST>
1
by: JoeZ | last post by:
Hi all, I have a question about using XMLValidatingReader. I have a schema files (xsd), and a xml data file. In the xml data file, if I don't specify the schema file path, XMLValidatingReader...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.