public void TestNormalization() { string intSubset = "<!ELEMENT root EMPTY>" + "<!ATTLIST root foo ID #REQUIRED" + " bar NMTOKEN #IMPLIED " + " baz NMTOKENS #IMPLIED " + " quux CDATA #IMPLIED >"; string dtd = "<!DOCTYPE root [" + intSubset + "]>"; string xml = dtd + "<root foo=' id1 ' bar=' nameToken ' baz=' list of\r\nname token' quux=' quuux\tquuux\t ' />"; dvr = PrepareXmlReader(xml); ((XmlTextReader)dvr.Reader).Normalization = true; dvr.EntityHandling = EntityHandling.ExpandEntities; dvr.Read(); // DTD dvr.Read(); Assert.AreEqual(XmlNodeType.Element, dvr.NodeType); Assert.AreEqual("root", dvr.Name); Assert.IsTrue(dvr.MoveToFirstAttribute()); Assert.AreEqual("foo", dvr.Name); Assert.AreEqual("id1", dvr.Value); Assert.IsTrue(dvr.MoveToNextAttribute()); Assert.AreEqual("bar", dvr.Name); Assert.AreEqual("nameToken", dvr.Value); Assert.IsTrue(dvr.MoveToNextAttribute()); Assert.AreEqual("baz", dvr.Name); Assert.AreEqual("list of name token", dvr.Value); Assert.IsTrue(dvr.MoveToNextAttribute()); Assert.AreEqual("quux", dvr.Name); Assert.AreEqual(" quuux quuux ", dvr.Value); }
static void Main(string[] args) { // Create a cache of schemas, and add two schemas XmlSchemaCollection sc = new XmlSchemaCollection(); sc.Add("urn:MyUri", "../../../doctors.xsd"); //sc.Add("", "../../../doctors.xsd"); // Create a validating reader object XmlTextReader tr = new XmlTextReader("../../../doctors.xml"); XmlValidatingReader vr = new XmlValidatingReader(tr); // Specify the type of validation required vr.ValidationType = ValidationType.Schema; // Tell the validating reader to use the schema collection vr.Schemas.Add(sc); // Register a validation event handler method vr.ValidationEventHandler += new ValidationEventHandler(MyHandler); // Read and validate the XML document try { int num = 0; float avg_age = 0; while (vr.Read()) { if (vr.NodeType == XmlNodeType.Element && vr.LocalName == "P") { num++; vr.MoveToFirstAttribute(); Console.WriteLine(vr.Value); vr.MoveToNextAttribute(); vr.MoveToNextAttribute(); string val = vr.Value; if (val != "male" && val != "female") { //Console.WriteLine(val); avg_age += Convert.ToInt32(vr.Value); } vr.MoveToElement(); } } Console.WriteLine("Number of Passengers: " + num + "\n"); Console.WriteLine("Average age: " + avg_age / num + "\n"); } catch (XmlException ex) { Console.WriteLine("XMLException occurred: " + ex.Message); } finally { vr.Close(); } }
static void Main(string[] args) { XmlSchemaCollection sc = new XmlSchemaCollection(); sc.Add("", "../../../../visual_studio_schema_test.xsd"); // Create a validating reader object XmlTextReader tr = new XmlTextReader("../../../../test_xml.xml"); //XmlTextReader tr = new XmlTextReader("../../../../xml_test_wrong_atrib.xml"); //XmlTextReader tr = new XmlTextReader("../../../../xml_test_wrong_root.xml"); XmlValidatingReader vr = new XmlValidatingReader(tr); // Specify the type of validation required vr.ValidationType = ValidationType.Schema; // Tell the validating reader to use the schema collection vr.Schemas.Add(sc); // Register a validation event handler method vr.ValidationEventHandler += new ValidationEventHandler(MyHandler); // Read and validate the XML document try { int num = 0; while (vr.Read()) { if (vr.NodeType == XmlNodeType.Element && vr.LocalName == "Games") { num++; vr.MoveToFirstAttribute(); Console.WriteLine(vr.Value); vr.MoveToNextAttribute(); Console.WriteLine(vr.Value); vr.MoveToNextAttribute(); Console.WriteLine(vr.Value); vr.MoveToNextAttribute(); Console.WriteLine(vr.Value); vr.MoveToNextAttribute(); Console.WriteLine(vr.Value); vr.MoveToElement(); } } Console.WriteLine("Number of strings: " + num + "\n"); } catch (XmlException ex) { Console.WriteLine("XMLException occurred: " + ex.Message); } finally { vr.Close(); } }
public static void Main(){ //Create the reader. XmlTextReader txtreader = new XmlTextReader("book4.xml"); XmlValidatingReader reader = new XmlValidatingReader(txtreader); reader.MoveToContent(); //Display each of the attribute nodes, including default attributes. while (reader.MoveToNextAttribute()){ if (reader.IsDefault) Console.Write("(default attribute) "); Console.WriteLine("{0} = {1}", reader.Name, reader.Value); } //Close the reader. reader.Close(); }
public void TestExpandEntity() { string intSubset = "<!ELEMENT root (#PCDATA)><!ATTLIST root foo CDATA 'foo-def' bar CDATA 'bar-def'><!ENTITY ent 'entity string'>"; string dtd = "<!DOCTYPE root [" + intSubset + "]>"; string xml = dtd + "<root foo='&ent;' bar='internal &ent; value'>&ent;</root>"; dvr = PrepareXmlReader(xml); dvr.EntityHandling = EntityHandling.ExpandEntities; dvr.Read(); // DTD dvr.Read(); Assert.AreEqual(XmlNodeType.Element, dvr.NodeType); Assert.AreEqual("root", dvr.Name); Assert.IsTrue(dvr.MoveToFirstAttribute()); Assert.AreEqual("foo", dvr.Name); Assert.AreEqual("entity string", dvr.Value); Assert.IsTrue(dvr.MoveToNextAttribute()); Assert.AreEqual("bar", dvr.Name); Assert.AreEqual("internal entity string value", dvr.Value); Assert.AreEqual("entity string", dvr.ReadString()); // ValidationType = None dvr = PrepareXmlReader(xml); dvr.EntityHandling = EntityHandling.ExpandEntities; dvr.ValidationType = ValidationType.None; dvr.Read(); // DTD dvr.Read(); Assert.AreEqual(XmlNodeType.Element, dvr.NodeType); Assert.AreEqual("root", dvr.Name); Assert.IsTrue(dvr.MoveToFirstAttribute()); Assert.AreEqual("foo", dvr.Name); Assert.AreEqual("entity string", dvr.Value); Assert.IsTrue(dvr.MoveToNextAttribute()); Assert.AreEqual("bar", dvr.Name); Assert.AreEqual("internal entity string value", dvr.Value); Assert.AreEqual("entity string", dvr.ReadString()); }
public static void ValidateXML(string xmlPath, string xsdPath) { XmlTextReader tr = new XmlTextReader(xmlPath); XmlValidatingReader vr = new XmlValidatingReader(tr); vr.Schemas.Add(null, xsdPath); vr.ValidationType = ValidationType.Schema; vr.ValidationEventHandler += new ValidationEventHandler(ValidationHandler); while (vr.Read()) { PrintTypeInfo(vr); if (vr.NodeType == XmlNodeType.Element) { while (vr.MoveToNextAttribute()) { PrintTypeInfo(vr); } } } Console.WriteLine("Validation finished"); }
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. }
// it used to be regarded as MS bug but it was not really. public void TestPreserveEntityNotOnDotNet() { string intSubset = "<!ELEMENT root EMPTY><!ATTLIST root foo CDATA 'foo-def' bar CDATA 'bar-def'><!ENTITY ent 'entity string'>"; string dtd = "<!DOCTYPE root [" + intSubset + "]>"; string xml = dtd + "<root foo='&ent;' bar='internal &ent; value' />"; dvr = PrepareXmlReader(xml); dvr.EntityHandling = EntityHandling.ExpandCharEntities; dvr.Read(); // DTD dvr.Read(); Assert.AreEqual(XmlNodeType.Element, dvr.NodeType); Assert.AreEqual("root", dvr.Name); Assert.IsTrue(dvr.MoveToFirstAttribute()); Assert.AreEqual("foo", dvr.Name); Assert.AreEqual("entity string", dvr.Value); // ReadAttributeValue() Assert.IsTrue(dvr.ReadAttributeValue()); Assert.AreEqual(XmlNodeType.EntityReference, dvr.NodeType); Assert.AreEqual("ent", dvr.Name); Assert.AreEqual(String.Empty, dvr.Value); Assert.IsTrue(!dvr.ReadAttributeValue()); // bar Assert.IsTrue(dvr.MoveToNextAttribute()); Assert.AreEqual("bar", dvr.Name); Assert.AreEqual("internal entity string value", dvr.Value); // ReadAttributeValue() Assert.IsTrue(dvr.ReadAttributeValue()); Assert.AreEqual(XmlNodeType.Text, dvr.NodeType); Assert.AreEqual(String.Empty, dvr.Name); Assert.AreEqual("internal ", dvr.Value); Assert.IsTrue(dvr.ReadAttributeValue()); Assert.AreEqual(XmlNodeType.EntityReference, dvr.NodeType); Assert.AreEqual("ent", dvr.Name); Assert.AreEqual(String.Empty, dvr.Value); Assert.IsTrue(dvr.ReadAttributeValue()); Assert.AreEqual(XmlNodeType.Text, dvr.NodeType); Assert.AreEqual(String.Empty, dvr.Name); Assert.AreEqual(" value", dvr.Value); // ValidationType = None dvr = PrepareXmlReader(xml); dvr.EntityHandling = EntityHandling.ExpandCharEntities; dvr.ValidationType = ValidationType.None; dvr.Read(); // DTD dvr.Read(); Assert.AreEqual(XmlNodeType.Element, dvr.NodeType); Assert.AreEqual("root", dvr.Name); // foo Assert.IsTrue(dvr.MoveToFirstAttribute()); Assert.AreEqual("foo", dvr.Name); Assert.AreEqual("entity string", dvr.Value); // ReadAttributeValue() Assert.IsTrue(dvr.ReadAttributeValue()); Assert.AreEqual(XmlNodeType.EntityReference, dvr.NodeType); Assert.AreEqual("ent", dvr.Name); Assert.AreEqual(String.Empty, dvr.Value); Assert.IsTrue(!dvr.ReadAttributeValue()); // bar Assert.IsTrue(dvr.MoveToNextAttribute()); Assert.AreEqual("bar", dvr.Name); Assert.AreEqual("internal entity string value", dvr.Value); // ReadAttributeValue() Assert.IsTrue(dvr.ReadAttributeValue()); Assert.AreEqual(XmlNodeType.Text, dvr.NodeType); Assert.AreEqual(String.Empty, dvr.Name); Assert.AreEqual("internal ", dvr.Value); Assert.IsTrue(dvr.ReadAttributeValue()); Assert.AreEqual(XmlNodeType.EntityReference, dvr.NodeType); Assert.AreEqual("ent", dvr.Name); Assert.AreEqual(String.Empty, dvr.Value); Assert.IsTrue(dvr.ReadAttributeValue()); Assert.AreEqual(XmlNodeType.Text, dvr.NodeType); Assert.AreEqual(String.Empty, dvr.Name); Assert.AreEqual(" value", dvr.Value); }
/// <summary> /// 运行函数,参数1为SLD结果路径,参数2为模板文件名 /// </summary> /// <param name="args"></param> public void Run(string[] args) { XmlValidatingReader reader = null; XmlSchemaCollection xsc = new XmlSchemaCollection(); try { xsc.Add(null, new XmlTextReader(args[1])); reader = new XmlValidatingReader(new XmlTextReader(args[0])); IXmlLineInfo lineInfo = (IXmlLineInfo)reader; ValidationEventHandler valdel = new ValidationEventHandler(ValidationEvent); reader.ValidationEventHandler += valdel; reader.Schemas.Add(xsc); reader.ValidationType = ValidationType.Schema; while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { while (reader.MoveToNextAttribute()) { m_iLinenumber = Convert.ToInt16(lineInfo.LineNumber.ToString()); m_cNodePosition = lineInfo.LinePosition.ToString(); m_cNodeType = reader.NodeType.ToString(); m_cNodeName = reader.Name.ToString(); } } } } catch (XmlSchemaException e) { m_cValidatMessage = m_cValidatMessage + "XML 模板错误" + "\r\n" + "\r\n"; m_cValidatMessage = m_cValidatMessage + "加载的XML不可用 " + "\r\n" + "后语为加载正确的命名空间, 错误信息: " + "\r\n" + e.Message + "\r\n" + "行: " + Convert.ToString(e.LinePosition) + "\r\n"; m_cValidatMessage = m_cValidatMessage + "\r\n" + "\r\n"; m_bValid = false; } catch (XmlException e) { m_cValidatMessage = m_cValidatMessage + "XML-SYNTAX EXCEPTION!" + "\r\n" + "\r\n"; m_cValidatMessage = m_cValidatMessage + "the file comprises an XML-Syntax error. " + "\r\n" + e.Message; m_cValidatMessage = m_cValidatMessage + "\r\n" + "\r\n"; m_bValid = false; } catch (Exception e) { m_cValidatMessage = m_cValidatMessage + "SERIOUS EXCEPTION!" + "\r\n" + "\r\n"; m_cValidatMessage = m_cValidatMessage + "Unknown error while validating SLD" + "\r\n" + e.Message; m_cValidatMessage = m_cValidatMessage + e.StackTrace + "\r\n"; m_cValidatMessage = m_cValidatMessage + "\r\n" + "\r\n"; m_bValid = false; } finally { if (!(reader == null)) { reader.Close(); } } }