By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,693 Members | 1,132 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,693 IT Pros & Developers. It's quick & easy.

XMLValidatingReader.Read() question

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a


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

P: n/a


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

P: n/a
ahhh, ReadElementContent methods, not Value properties. that was the
mistake i was making. Thank you for the reply.

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.