Udostępnij za pośrednictwem


HowToUseXmlDataDocument

One test is better that thousand words...

[TestFixture]

public class XmlDataDemo

{

     [Test]

     public void LoadDataSet()

     {

           EmployeeDataSet ds = new EmployeeDataSet();

           ds.ReadXml(@".\..\..\Employee.xml");

           Assert.AreEqual(1,ds.employee.Rows.Count);

     }

     [Test]

     public void XmlDataDoc()

     {

           XmlDataDocument doc = new XmlDataDocument(new EmployeeDataSet());

           doc.Load (@".\..\..\Employee.xml");

           XmlDocument d = UntypeAndModifyAge(doc, "22");

                                                                               

           EmployeeDataSet ds2 = new EmployeeDataSet();

           ds2.ReadXml(CreateEmployeeValidatingReader(d.InnerXml));

           Assert.AreEqual(1,ds2.employee.Rows.Count);

           Console.WriteLine("After modify .....");

           Console.WriteLine(ds2.GetXml());

     }

     [Test, ExpectedException(typeof(XmlSchemaException))]

     public void XmlDataDocValidatingException()

     {

           XmlDataDocument doc = new XmlDataDocument(new EmployeeDataSet());

           doc.Load (@".\..\..\Employee.xml");

           XmlDocument d = UntypeAndModifyAge(doc, "22a");

                          

           EmployeeDataSet ds2 = new EmployeeDataSet();

           ds2.ReadXml(CreateEmployeeValidatingReader(d.InnerXml));

     }

     private XmlValidatingReader CreateEmployeeValidatingReader(string xml)

     {

           XmlValidatingReader vr = new XmlValidatingReader(xml, XmlNodeType.Document, null);

           vr.ValidationEventHandler +=new System.Xml.Schema.ValidationEventHandler(vr_ValidationEventHandler);

           XmlSchemaCollection sc = new XmlSchemaCollection();

           sc.Add("urn:rido.code/Employee", new XmlTextReader(@".\..\..\EmployeeDataSet.xsd"));

           vr.Schemas.Add(sc);

           return vr;

     }

     private XmlDocument UntypeAndModifyAge(XmlDocument doc, string ageToModify)

     {

          

           ((XmlDataDocument)doc).DataSet.EnforceConstraints = false;

           doc.GetElementsByTagName("Age")[0].InnerText = ageToModify;

           ((XmlDataDocument)doc).DataSet.EnforceConstraints = true;

          

           string s = doc.InnerXml;

           XmlDocument result = new XmlDocument();

           result.LoadXml(s);

           Console.WriteLine("modified from untyped.....");

           Console.WriteLine(s);

           return result;

     }

     [Test]

     public void LoadDataSetWithBadNamespace()

     {

           EmployeeDataSet ds = new EmployeeDataSet();

           ds.ReadXml(@".\..\..\EmployeeNoNs.xml");

           Assert.AreEqual(0,ds.employee.Rows.Count);

     }

     [Test]

     public void LoadDataSetWithBadXml()

     {

           EmployeeDataSet ds = new EmployeeDataSet();

           ds.ReadXml(@".\..\..\XmlFile1.xml");

           Assert.AreEqual(0,ds.employee.Rows.Count);

    

     }

     private void vr_ValidationEventHandler(object sender, System.Xml.Schema.ValidationEventArgs e)

     {

           //Assert.Fail(e.Message);

           throw e.Exception;

     }

}

<?xml version="1.0" encoding="utf-8" ?>

<xs:schema id="EmployeeDataSet" targetNamespace="urn:rido.code/Employee" elementFormDefault="qualified"

     attributeFormDefault="qualified" xmlns="urn:rido.code/Employee" xmlns:mstns="urn:rido.code/Employee"

     xmlns:xs="https://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

     <xs:element name="EmployeeDataSet" msdata:IsDataSet="true">

     <xs:complexType>

     <xs:sequence>

     <xs:element name="employee" type="Employee" minOccurs="1" />

     </xs:sequence>

     </xs:complexType>

     </xs:element>

     <xs:complexType name="Employee">

     <xs:sequence>

     <xs:element name="Name" type="xs:string" minOccurs="0" />

     <xs:element name="Age" type="xs:int" minOccurs="0" />

     </xs:sequence>

     </xs:complexType>

</xs:schema>

 

<?xml version="1.0" encoding="utf-8" ?>

<EmployeeDataSet xmlns="urn:rido.code/Employee">

     <employee>

     <Name>rido</Name>

     <Age>33</Age>

     </employee>

</EmployeeDataSet>