本文整理匯總了C#中System.Xml.XmlValidatingReader.ReadTypedValue方法的典型用法代碼示例。如果您正苦於以下問題:C# XmlValidatingReader.ReadTypedValue方法的具體用法?C# XmlValidatingReader.ReadTypedValue怎麽用?C# XmlValidatingReader.ReadTypedValue使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Xml.XmlValidatingReader
的用法示例。
在下文中一共展示了XmlValidatingReader.ReadTypedValue方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Page_Load
protected void Page_Load(object sender, EventArgs e)
{
string xmlFile = Server.MapPath("~/Customers1.xml");
string xsdFile = Server.MapPath("~/Customers.xsd");
XmlTextReader textReader = new XmlTextReader(xmlFile);
XmlValidatingReader validatingReader = new XmlValidatingReader(textReader);
validatingReader.Schemas.Add(null, xsdFile);
validatingReader.ValidationType = ValidationType.Schema;
validatingReader.ValidationEventHandler += new ValidationEventHandler(validatingReader_ValidationEventHandler);
while (validatingReader.Read())
{
if (validatingReader.NodeType == XmlNodeType.Element)
{
if (validatingReader.SchemaType is XmlSchemaComplexType)
{
XmlSchemaComplexType complexType = (XmlSchemaComplexType)validatingReader.SchemaType;
Response.Write(validatingReader.Name + " " + complexType.Name);
}
else
{
object innerText = validatingReader.ReadTypedValue();
Response.Write(validatingReader.Name + " : " + innerText.ToString() + " <br />");
}
}
}
validatingReader.Close();
}
示例2: TestReadTypedValueSimple
public void TestReadTypedValueSimple ()
{
string xml = "<root>12</root>";
XmlSchema schema = new XmlSchema ();
XmlSchemaElement elem = new XmlSchemaElement ();
elem.Name = "root";
elem.SchemaTypeName = new XmlQualifiedName ("integer", XmlSchema.Namespace);
schema.Items.Add (elem);
// Lap 1:
xvr = PrepareXmlReader (xml);
xvr.Schemas.Add (schema);
// Read directly from root.
object o = xvr.ReadTypedValue ();
Assert.AreEqual (ReadState.Initial, xvr.ReadState);
Assert.IsNull (o);
xvr.Read (); // element root
Assert.AreEqual (XmlNodeType.Element, xvr.NodeType);
Assert.IsNotNull (xvr.SchemaType);
Assert.IsTrue (xvr.SchemaType is XmlSchemaDatatype);
o = xvr.ReadTypedValue (); // read "12"
Assert.AreEqual (XmlNodeType.EndElement, xvr.NodeType);
Assert.IsNotNull (o);
Assert.AreEqual (typeof (decimal), o.GetType ());
decimal n = (decimal) o;
Assert.AreEqual (12, n);
Assert.IsTrue (!xvr.EOF);
Assert.AreEqual ("root", xvr.Name);
Assert.IsNull (xvr.SchemaType); // EndElement's type
// Lap 2:
xvr = PrepareXmlReader (xml);
xvr.Schemas.Add (schema);
xvr.Read (); // root
XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
Assert.IsNotNull (dt);
Assert.AreEqual (typeof (decimal), dt.ValueType);
Assert.AreEqual (XmlTokenizedType.None, dt.TokenizedType);
xvr.Read (); // text "12"
Assert.IsNull (xvr.SchemaType);
o = xvr.ReadTypedValue ();
// ReadTypedValue is different from ReadString().
Assert.IsNull (o);
}
示例3: ReadTypedValueWhitespaces
public void ReadTypedValueWhitespaces ()
{
string xml = "<root> </root><!-- after -->";
string xsd = @"
<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
<xs:element name='root'>
<xs:simpleType>
<xs:restriction base='xs:string'>
<xs:minLength value='2' />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>";
XmlTextReader xir =
new XmlTextReader (xml, XmlNodeType.Document, null);
XmlTextReader xsr =
new XmlTextReader (xsd, XmlNodeType.Document, null);
XmlValidatingReader vr = new XmlValidatingReader (xir);
vr.Schemas.Add (XmlSchema.Read (xsr, null));
vr.Read (); // root
Assert.AreEqual (" ", vr.ReadTypedValue ());
Assert.AreEqual (XmlNodeType.EndElement, vr.NodeType);
}
示例4: TestReadTypedAttributeValue
public void TestReadTypedAttributeValue ()
{
string xml = "<root attr='12'></root>";
XmlSchema schema = new XmlSchema ();
XmlSchemaElement elem = new XmlSchemaElement ();
elem.Name = "root";
XmlSchemaComplexType ct = new XmlSchemaComplexType ();
XmlSchemaAttribute attr = new XmlSchemaAttribute ();
attr.Name = "attr";
attr.SchemaTypeName = new XmlQualifiedName ("int", XmlSchema.Namespace);
ct.Attributes.Add (attr);
elem.SchemaType = ct;
schema.Items.Add (elem);
xvr = PrepareXmlReader (xml);
xvr.Schemas.Add (schema);
xvr.Read ();
Assert.AreEqual ("root", xvr.Name);
Assert.IsTrue (xvr.MoveToNextAttribute ()); // attr
Assert.AreEqual ("attr", xvr.Name);
XmlSchemaDatatype dt = xvr.SchemaType as XmlSchemaDatatype;
Assert.IsNotNull (dt);
Assert.AreEqual (typeof (int), dt.ValueType);
Assert.AreEqual (XmlTokenizedType.None, dt.TokenizedType);
object o = xvr.ReadTypedValue ();
Assert.AreEqual (XmlNodeType.Attribute, xvr.NodeType);
Assert.AreEqual (typeof (int), o.GetType ());
int n = (int) o;
Assert.AreEqual (12, n);
Assert.IsTrue (xvr.ReadAttributeValue ()); // can read = seems not proceed.
}
示例5: Parse
public override object Parse (string value, XmlReader reader)
{
// Now we create XmlValidatingReader to handle
// simple-type based validation (since there is no
// other way, because of sucky XmlSchemaSimpleType
// design).
if (value != null)
value = value.Replace("&", "&").Replace("<", "<").Replace(">", ">");
XmlValidatingReader v = new XmlValidatingReader (
new XmlTextReader (
String.Concat ("<root>", value, "</root>"),
XmlNodeType.Document,
null));
v.Schemas.Add (schema);
v.Read (); // <root>
try {
return v.ReadTypedValue ();
} finally {
v.Read (); // </root>
}
}