all groups > dotnet xml > april 2005 >
You're in the

dotnet xml

group:

XMLValidatingReader.Read() question


XMLValidatingReader.Read() question jason
4/28/2005 12:48:09 PM
dotnet xml:
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
Re: XMLValidatingReader.Read() question Martin Honnen
4/29/2005 12:00:00 AM


[quoted text, click to view]

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.


[quoted text, click to view]

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
Re: XMLValidatingReader.Read() question Martin Honnen
4/29/2005 12:00:00 AM


[quoted text, click to view]


[quoted text, click to view]

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
Re: XMLValidatingReader.Read() question jason
4/29/2005 6:22:26 AM
ahhh, ReadElementContent methods, not Value properties. that was the
mistake i was making. Thank you for the reply.
AddThis Social Bookmark Button