public void test2() //Ignoring 2 namespaces { Exception exp = null; try { StringBuilder sb = new StringBuilder(); sb.Append("<h:html xmlns:xdc='http://www.xml.com/books' xmlns:h='http://www.w3.org/HTML/1998/html4'>"); sb.Append("<h:head><h:title>Book Review</h:title></h:head>"); sb.Append("<h:body>"); sb.Append("<xdc:bookreview>"); sb.Append("<xdc:title h:attrib1='1' xdc:attrib2='2' >XML: A Primer</xdc:title>"); sb.Append("</xdc:bookreview>"); sb.Append("</h:body>"); sb.Append("</h:html>"); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); DataSet tempDs = new DataSet(); tempDs.ReadXml(myStream); myStream.Seek(0, SeekOrigin.Begin); DataSet ds = new DataSet(); ds.InferXmlSchema(myStream, new string[] { "http://www.xml.com/books", "http://www.w3.org/HTML/1998/html4" }); //Compare(ds.Tables.Count,8); // string str1 = tempDs.GetXmlSchema(); //DataProvider.GetDSSchema(tempDs); // string str2 = ds.GetXmlSchema(); //DataProvider.GetDSSchema(ds); Compare(ds.Tables.Count, 3); Compare(ds.Tables[2].TableName, "bookreview"); Compare(ds.Tables[2].Columns.Count, 2); } catch (Exception ex) { exp = ex; } }
public void inferringColumns1() { //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringcolumns.htm BeginCase("inferringColumns1"); Exception exp = null; StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1 attr1='value1' attr2='value2'/>"); sb.Append("</DocumentElement>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); try { ds.InferXmlSchema(myStream, null); Compare(ds.DataSetName, "DocumentElement"); Compare(ds.Tables[0].TableName, "Element1"); Compare(ds.Tables.Count, 1); Compare(ds.Tables[0].Columns["attr1"].ColumnName, "attr1"); Compare(ds.Tables[0].Columns["attr2"].ColumnName, "attr2"); Compare(ds.Tables[0].Columns["attr1"].ColumnMapping, MappingType.Attribute); Compare(ds.Tables[0].Columns["attr2"].ColumnMapping, MappingType.Attribute); Compare(ds.Tables[0].Columns["attr1"].DataType, typeof(string)); Compare(ds.Tables[0].Columns["attr2"].DataType, typeof(string)); } catch (Exception ex) { exp = ex; } finally { EndCase(exp); } }
public void inferingTables5() { //Acroding to the msdn documantaion : //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm //Elements that repeat will result in a single inferred table BeginCase("inferingTables5"); Exception exp = null; StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1>Text1</Element1>"); sb.Append("<Element1>Text2</Element1>"); sb.Append("</DocumentElement>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); try { ds.InferXmlSchema(myStream, null); Compare(ds.DataSetName, "DocumentElement"); Compare(ds.Tables[0].TableName, "Element1"); Compare(ds.Tables.Count, 1); Compare(ds.Tables[0].Columns["Element1_Text"].ColumnName, "Element1_Text"); } catch (Exception ex) { exp = ex; } finally { EndCase(exp); } }
/* * // simple diffgram * string xml23 = @"<set> * <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'> * <xs:element name='table'> * <xs:complexType> * <xs:choice> * <xs:any /> * </xs:choice> * </xs:complexType> * </xs:element> * </xs:schema> * <diffgr:diffgram * xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' * xmlns:diffgr='urn:schemas-microsoft-com:xml-diffgram-v1'> * <table> * <col>1</col> * </table> * </diffgr:diffgram> * </set>"; * // just deep table * string xml24 = "<p1><p2><p3><p4><p5><p6/></p5></p4></p3></p2></p1>"; */ private DataSet GetDataSet(string xml, string[] nss) { DataSet ds = new DataSet(); ds.InferXmlSchema(new XmlTextReader(xml, XmlNodeType.Document, null), nss); return(ds); }
public void NullFileName() { DataSet ds = new DataSet(); ds.InferXmlSchema((XmlReader)null, null); DataSetAssertion.AssertDataSet("null", ds, "NewDataSet", 0, 0); }
public void test1() { StringBuilder sb = new StringBuilder(); sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>"); sb.Append("<Categories>"); sb.Append("<CategoryID od:adotype='3'>1</CategoryID>"); sb.Append("<CategoryName od:maxLength='15' od:adotype='130'>Beverages</CategoryName>"); sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>"); sb.Append("</Categories>"); sb.Append("<Products>"); sb.Append("<ProductID od:adotype='20'>1</ProductID>"); sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>"); sb.Append("<Discontinued od:adotype='11'>0</Discontinued>"); sb.Append("</Products>"); sb.Append("</NewDataSet>"); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); DataSet ds = new DataSet(); //ds.ReadXml(myStream); ds.InferXmlSchema(myStream, new string[] { "urn:schemas-microsoft-com:officedata1" }); Compare(ds.Tables.Count, 8); }
public void SimpleLoad() { string xml001 = "<root/>"; XmlDataDocument doc = new XmlDataDocument(); DataSet ds = new DataSet(); ds.InferXmlSchema(new StringReader(xml001), null); doc.LoadXml(xml001); string xml002 = "<root><child/></root>"; doc = new XmlDataDocument(); ds = new DataSet(); ds.InferXmlSchema(new StringReader(xml002), null); doc.LoadXml(xml002); string xml003 = "<root><col1>test</col1><col1></col1></root>"; doc = new XmlDataDocument(); ds = new DataSet(); ds.InferXmlSchema(new StringReader(xml003), null); doc.LoadXml(xml003); string xml004 = "<set><tab1><col1>test</col1><col1>test2</col1></tab1><tab2><col2>test3</col2><col2>test4</col2></tab2></set>"; doc = new XmlDataDocument(); ds = new DataSet(); ds.InferXmlSchema(new StringReader(xml004), null); doc.LoadXml(xml004); }
public void LoadXml(String xmlFile, String xsdFile) { XmlSchema xSchema = new XmlSchema(); xSchema.SourceUri = ""; DataSet ds = new DataSet("Test"); ds.ReadXmlSchema(xsdFile); ds.InferXmlSchema(xsdFile, null); }
static void Main() { DataSet dataSet = new DataSet(); string[] ignoreNamesapces = { "http://www.shop.com/pml", "http://www.shop.com/cml" }; dataSet.InferXmlSchema(@"..\..\ch13\orderlist.xml", ignoreNamesapces); DisplaySchema.displayDataSetSchema(dataSet); }
protected List <DataSet> ReadData(string[] xmlFilePathList, string[] nsArray) { List <DataSet> ret = new List <DataSet>(); foreach (string item in xmlFilePathList) { DataSet itemDataSet = new DataSet(); itemDataSet.InferXmlSchema(HttpContext.Current.Server.MapPath(item), nsArray); itemDataSet.ReadXml(HttpContext.Current.Server.MapPath(item)); ret.Add(itemDataSet); } return(ret); }
private void button1_Click(object sender, EventArgs e) { DataSet ds = new DataSet(); if (radioButton1.Checked) { ds.ReadXmlSchema(textBox1.Text); } if (radioButton2.Checked) { ds.InferXmlSchema(textBox1.Text, null); } MessageBox.Show(ds.GetXmlSchema()); }
protected List <DataSet> ReadData(Stream[] xmlFilePathList, string[] nsArray) { List <DataSet> ret = new List <DataSet>(); foreach (Stream item in xmlFilePathList) { DataSet itemDataSet = new DataSet(); item.Position = 0; itemDataSet.InferXmlSchema(item, nsArray); item.Position = 0; itemDataSet.ReadXml(item); ret.Add(itemDataSet); } return(ret); }
public void IgnoredNamespaces() { string xml = "<root attr='val' xmlns:a='urn:foo' a:foo='hogehoge' />"; DataSet ds = new DataSet(); ds.InferXmlSchema(new StringReader(xml), null); DataSetAssertion.AssertDataSet("ds", ds, "NewDataSet", 1, 0); DataSetAssertion.AssertDataTable("dt", ds.Tables[0], "root", 2, 0, 0, 0, 0, 0); ds = new DataSet(); ds.InferXmlSchema(new StringReader(xml), new string[] { "urn:foo" }); DataSetAssertion.AssertDataSet("ds", ds, "NewDataSet", 1, 0); // a:foo is ignored DataSetAssertion.AssertDataTable("dt", ds.Tables[0], "root", 1, 0, 0, 0, 0, 0); }
public void SignificantWhitespaceIgnored2() { // To make sure, create pure significant whitespace element // using XmlNodeReader (that does not have xml:space attribute // column). DataSet ds = new DataSet(); XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.CreateElement("root")); doc.DocumentElement.AppendChild(doc.CreateSignificantWhitespace (" \n\n")); XmlReader xr = new XmlNodeReader(doc); ds.InferXmlSchema(xr, null); DataSetAssertion.AssertDataSet("pure_whitespace", ds, "root", 0, 0); }
/// <summary> /// Display the results of inferring schema from four types of XML structures /// </summary> private static void InferDataSetSchemaFromXml() { String[] xmlFileNames = { @"XMLFiles\ElementsWithOnlyAttributes.xml", @"XMLFiles\ElementsWithAttributes.xml", @"XMLFiles\RepeatingElements.xml", @"XMLFiles\ElementsWithChildElements.xml" }; foreach (String xmlFileName in xmlFileNames) { Console.WriteLine("Result of {0}", Path.GetFileNameWithoutExtension(xmlFileName)); DataSet newSchool = new DataSet(); newSchool.InferXmlSchema(xmlFileName, null); DataTableHelper.ShowDataSetSchema(newSchool); Console.WriteLine(); } }
public void inferringColumns2() { //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringcolumns.htm //If an element has no child elements or attributes, it will be inferred as a column. //The ColumnMapping property of the column will be set to MappingType.Element. //The text for child elements is stored in a row in the table BeginCase("inferringColumns2"); Exception exp = null; StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1>"); sb.Append("<ChildElement1>Text1</ChildElement1>"); sb.Append("<ChildElement2>Text2</ChildElement2>"); sb.Append("</Element1>"); sb.Append("</DocumentElement>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); try { ds.InferXmlSchema(myStream, null); Compare(ds.DataSetName, "DocumentElement"); Compare(ds.Tables[0].TableName, "Element1"); Compare(ds.Tables.Count, 1); Compare(ds.Tables[0].Columns["ChildElement1"].ColumnName, "ChildElement1"); Compare(ds.Tables[0].Columns["ChildElement2"].ColumnName, "ChildElement2"); Compare(ds.Tables[0].Columns["ChildElement1"].ColumnMapping, MappingType.Element); Compare(ds.Tables[0].Columns["ChildElement2"].ColumnMapping, MappingType.Element); Compare(ds.Tables[0].Columns["ChildElement1"].DataType, typeof(string)); Compare(ds.Tables[0].Columns["ChildElement2"].DataType, typeof(string)); } catch (Exception ex) { exp = ex; } finally { EndCase(exp); } }
private void button1_Click(object sender, EventArgs e) { if (!System.IO.File.Exists(textBox1.Text)) { MessageBox.Show("Invalid XML file name"); return; } DataSet ds = new DataSet(); if (radioButton1.Checked) { ds.ReadXmlSchema(textBox1.Text); } if (radioButton2.Checked) { ds.InferXmlSchema(textBox1.Text, null); } MessageBox.Show(ds.GetXmlSchema()); }
public void elementText2() { //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringelementtext.htm BeginCase("elementText1"); Exception exp = null; StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1>"); sb.Append("Text1"); sb.Append("<ChildElement1>Text2</ChildElement1>"); sb.Append("Text3"); sb.Append("</Element1>"); sb.Append("</DocumentElement>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); try { ds.InferXmlSchema(myStream, null); Compare(ds.DataSetName, "DocumentElement"); Compare(ds.Tables[0].TableName, "Element1"); Compare(ds.Tables.Count, 1); Compare(ds.Tables["Element1"].Columns["ChildElement1"].ColumnName, "ChildElement1"); Compare(ds.Tables["Element1"].Columns["ChildElement1"].ColumnMapping, MappingType.Element); Compare(ds.Tables["Element1"].Columns["ChildElement1"].DataType, typeof(string)); Compare(ds.Tables["Element1"].Columns.Count, 1); } catch (Exception ex) { exp = ex; } finally { EndCase(exp); } }
public void inferingTables3() { //Acroding to the msdn documantaion : //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm //The document, or root, element will result in an inferred table if it has attributes //or child elements that will be inferred as columns. //If the document element has no attributes and no child elements that would be inferred as columns, the element will be inferred as a DataSet BeginCase("inferingTables3"); Exception exp = null; StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1>Text1</Element1>"); sb.Append("<Element2>Text2</Element2>"); sb.Append("</DocumentElement>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); try { ds.InferXmlSchema(myStream, null); Compare(ds.DataSetName, "NewDataSet"); Compare(ds.Tables[0].TableName, "DocumentElement"); Compare(ds.Tables.Count, 1); Compare(ds.Tables[0].Columns["Element1"].ColumnName, "Element1"); Compare(ds.Tables[0].Columns["Element2"].ColumnName, "Element2"); } catch (Exception ex) { exp = ex; } finally { EndCase(exp); } }
public void InferXmlSchema_IgnoreNameSpace() { StringBuilder sb = new StringBuilder(); sb.Append("<NewDataSet xmlns:od='urn:schemas-microsoft-com:officedata'>"); sb.Append("<Categories>"); sb.Append("<CategoryID od:adotype='3'>1</CategoryID>"); sb.Append("<CategoryName od:maxLength='15' adotype='130'>Beverages</CategoryName>"); sb.Append("<Description od:adotype='203'>Soft drinks and teas</Description>"); sb.Append("</Categories>"); sb.Append("<Products>"); sb.Append("<ProductID od:adotype='20'>1</ProductID>"); sb.Append("<ReorderLevel od:adotype='3'>10</ReorderLevel>"); sb.Append("<Discontinued od:adotype='11'>0</Discontinued>"); sb.Append("</Products>"); sb.Append("</NewDataSet>"); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); var ds = new DataSet(); // ds.ReadXml(myStream); ds.InferXmlSchema(myStream, new string[] { "urn:schemas-microsoft-com:officedata" }); Assert.Equal(3, ds.Tables.Count); Assert.Equal(3, ds.Tables[0].Columns.Count); Assert.Equal("CategoryID", ds.Tables[0].Columns["CategoryID"].ColumnName); Assert.Equal("Categories_Id", ds.Tables[0].Columns["Categories_Id"].ColumnName); Assert.Equal("Description", ds.Tables[0].Columns["Description"].ColumnName); Assert.Equal(3, ds.Tables[1].Columns.Count); Assert.Equal("adotype", ds.Tables[1].Columns["adotype"].ColumnName); Assert.Equal("CategoryName_Text", ds.Tables[1].Columns["CategoryName_Text"].ColumnName); Assert.Equal("Categories_Id", ds.Tables[1].Columns["Categories_Id"].ColumnName); Assert.Equal(3, ds.Tables[2].Columns.Count); Assert.Equal("ProductID", ds.Tables[2].Columns["ProductID"].ColumnName); Assert.Equal("ReorderLevel", ds.Tables[2].Columns["ReorderLevel"].ColumnName); Assert.Equal("Discontinued", ds.Tables[2].Columns["Discontinued"].ColumnName); }
public void InferXmlSchema_IgnoreNameSpaces() //Ignoring 2 namespaces { StringBuilder sb = new StringBuilder(); sb.Append("<h:html xmlns:xdc='http://www.xml.com/books' xmlns:h='http://www.w3.org/HTML/1998/html4'>"); sb.Append("<h:head><h:title>Book Review</h:title></h:head>"); sb.Append("<h:body>"); sb.Append("<xdc:bookreview>"); sb.Append("<xdc:title h:attrib1='1' xdc:attrib2='2' >XML: A Primer</xdc:title>"); sb.Append("</xdc:bookreview>"); sb.Append("</h:body>"); sb.Append("</h:html>"); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); DataSet tempDs = new DataSet(); tempDs.ReadXml(myStream); myStream.Seek(0, SeekOrigin.Begin); var ds = new DataSet(); ds.InferXmlSchema(myStream, new string[] { "http://www.xml.com/books", "http://www.w3.org/HTML/1998/html4" }); //Assert.Equal(8, ds.Tables.Count); // string str1 = tempDs.GetXmlSchema(); //DataProvider.GetDSSchema(tempDs); // string str2 = ds.GetXmlSchema(); //DataProvider.GetDSSchema(ds); Assert.Equal(3, ds.Tables.Count); Assert.Equal("bookreview", ds.Tables[2].TableName); Assert.Equal(2, ds.Tables[2].Columns.Count); }
public void IgnoredNamespaces() { string xml = "<root attr='val' xmlns:a='urn:foo' a:foo='hogehoge' />"; DataSet ds = new DataSet(); ds.InferXmlSchema(new StringReader(xml), null); AssertDataSet("ds", ds, "NewDataSet", 1, 0); AssertDataTable("dt", ds.Tables[0], "root", 2, 0, 0, 0, 0, 0); ds = new DataSet(); ds.InferXmlSchema(new StringReader(xml), new string[] { "urn:foo" }); AssertDataSet("ds", ds, "NewDataSet", 1, 0); // a:foo is ignored AssertDataTable("dt", ds.Tables[0], "root", 1, 0, 0, 0, 0, 0); }
public void NullFileName() { DataSet ds = new DataSet(); ds.InferXmlSchema((XmlReader)null, null); AssertDataSet("null", ds, "NewDataSet", 0, 0); }
public void InferXmlSchema_inferingTables5() { //Acroding to the msdn documantaion : //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm //Elements that repeat will result in a single inferred table // inferingTables5 StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1>Text1</Element1>"); sb.Append("<Element1>Text2</Element1>"); sb.Append("</DocumentElement>"); var ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream, null); Assert.Equal("DocumentElement", ds.DataSetName); Assert.Equal("Element1", ds.Tables[0].TableName); Assert.Equal(1, ds.Tables.Count); Assert.Equal("Element1_Text", ds.Tables[0].Columns["Element1_Text"].ColumnName); }
public void InferXmlSchema_inferringColumns1() { //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringcolumns.htm // inferringColumns1 StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1 attr1='value1' attr2='value2'/>"); sb.Append("</DocumentElement>"); var ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream, null); Assert.Equal("DocumentElement", ds.DataSetName); Assert.Equal("Element1", ds.Tables[0].TableName); Assert.Equal(1, ds.Tables.Count); Assert.Equal("attr1", ds.Tables[0].Columns["attr1"].ColumnName); Assert.Equal("attr2", ds.Tables[0].Columns["attr2"].ColumnName); Assert.Equal(MappingType.Attribute, ds.Tables[0].Columns["attr1"].ColumnMapping); Assert.Equal(MappingType.Attribute, ds.Tables[0].Columns["attr2"].ColumnMapping); Assert.Equal(typeof(string), ds.Tables[0].Columns["attr1"].DataType); Assert.Equal(typeof(string), ds.Tables[0].Columns["attr2"].DataType); }
public void InferXmlSchema_inferringColumns2() { //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringcolumns.htm //If an element has no child elements or attributes, it will be inferred as a column. //The ColumnMapping property of the column will be set to MappingType.Element. //The text for child elements is stored in a row in the table // inferringColumns2 StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1>"); sb.Append("<ChildElement1>Text1</ChildElement1>"); sb.Append("<ChildElement2>Text2</ChildElement2>"); sb.Append("</Element1>"); sb.Append("</DocumentElement>"); var ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream, null); Assert.Equal("DocumentElement", ds.DataSetName); Assert.Equal("Element1", ds.Tables[0].TableName); Assert.Equal(1, ds.Tables.Count); Assert.Equal("ChildElement1", ds.Tables[0].Columns["ChildElement1"].ColumnName); Assert.Equal("ChildElement2", ds.Tables[0].Columns["ChildElement2"].ColumnName); Assert.Equal(MappingType.Element, ds.Tables[0].Columns["ChildElement1"].ColumnMapping); Assert.Equal(MappingType.Element, ds.Tables[0].Columns["ChildElement2"].ColumnMapping); Assert.Equal(typeof(string), ds.Tables[0].Columns["ChildElement1"].DataType); Assert.Equal(typeof(string), ds.Tables[0].Columns["ChildElement2"].DataType); }
public void InferXmlSchema_inferringRelationships1() { //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringrelationships.htm // inferringRelationships1 StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1>"); sb.Append("<ChildElement1 attr1='value1' attr2='value2'/>"); sb.Append("<ChildElement2>Text2</ChildElement2>"); sb.Append("</Element1>"); sb.Append("</DocumentElement>"); var ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream, null); Assert.Equal("DocumentElement", ds.DataSetName); Assert.Equal("Element1", ds.Tables[0].TableName); Assert.Equal("ChildElement1", ds.Tables[1].TableName); Assert.Equal(2, ds.Tables.Count); Assert.Equal("Element1_Id", ds.Tables["Element1"].Columns["Element1_Id"].ColumnName); Assert.Equal(MappingType.Hidden, ds.Tables["Element1"].Columns["Element1_Id"].ColumnMapping); Assert.Equal(typeof(int), ds.Tables["Element1"].Columns["Element1_Id"].DataType); Assert.Equal("ChildElement2", ds.Tables["Element1"].Columns["ChildElement2"].ColumnName); Assert.Equal(MappingType.Element, ds.Tables["Element1"].Columns["ChildElement2"].ColumnMapping); Assert.Equal(typeof(string), ds.Tables["Element1"].Columns["ChildElement2"].DataType); Assert.Equal("attr1", ds.Tables["ChildElement1"].Columns["attr1"].ColumnName); Assert.Equal(MappingType.Attribute, ds.Tables["ChildElement1"].Columns["attr1"].ColumnMapping); Assert.Equal(typeof(string), ds.Tables["ChildElement1"].Columns["attr1"].DataType); Assert.Equal("attr2", ds.Tables["ChildElement1"].Columns["attr2"].ColumnName); Assert.Equal(MappingType.Attribute, ds.Tables["ChildElement1"].Columns["attr2"].ColumnMapping); Assert.Equal(typeof(string), ds.Tables["ChildElement1"].Columns["attr2"].DataType); Assert.Equal("Element1_Id", ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnName); Assert.Equal(MappingType.Hidden, ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnMapping); Assert.Equal(typeof(int), ds.Tables["ChildElement1"].Columns["Element1_Id"].DataType); //Checking dataRelation : Assert.Equal("Element1", ds.Relations["Element1_ChildElement1"].ParentTable.TableName); Assert.Equal("Element1_Id", ds.Relations["Element1_ChildElement1"].ParentColumns[0].ColumnName); Assert.Equal("ChildElement1", ds.Relations["Element1_ChildElement1"].ChildTable.TableName); Assert.Equal("Element1_Id", ds.Relations["Element1_ChildElement1"].ChildColumns[0].ColumnName); Assert.Equal(true, ds.Relations["Element1_ChildElement1"].Nested); //Checking ForeignKeyConstraint ForeignKeyConstraint con = (ForeignKeyConstraint)ds.Tables["ChildElement1"].Constraints["Element1_ChildElement1"]; Assert.Equal("Element1_Id", con.Columns[0].ColumnName); Assert.Equal(Rule.Cascade, con.DeleteRule); Assert.Equal(AcceptRejectRule.None, con.AcceptRejectRule); Assert.Equal("Element1", con.RelatedTable.TableName); Assert.Equal("ChildElement1", con.Table.TableName); }
/* // simple diffgram string xml23 = @"<set> <xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'> <xs:element name='table'> <xs:complexType> <xs:choice> <xs:any /> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <diffgr:diffgram xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' xmlns:diffgr='urn:schemas-microsoft-com:xml-diffgram-v1'> <table> <col>1</col> </table> </diffgr:diffgram> </set>"; // just deep table string xml24 = "<p1><p2><p3><p4><p5><p6/></p5></p4></p3></p2></p1>"; */ private DataSet GetDataSet(string xml, string[] nss) { DataSet ds = new DataSet(); ds.InferXmlSchema(new XmlTextReader(xml, XmlNodeType.Document, null), nss); return ds; }
public void Run(string[] args) { ArrayList unknownFiles = new ArrayList(); bool generateClasses = false; bool readingFiles = true; bool schemasOptions = false; bool assemblyOptions = false; bool generateDataset = false; bool inference = false; foreach (string arg in args) { if (!arg.StartsWith("--") && !arg.StartsWith("/") || (arg.StartsWith("/") && arg.IndexOfAny(Path.InvalidPathChars) == -1) ) { if ((arg.EndsWith(".dll") || arg.EndsWith(".exe")) && !arg.Substring(1).StartsWith("generator:") && !arg.Substring(1).StartsWith("g:")) { if (!readingFiles) { throw new ApplicationException(incorrectOrder); } assemblies.Add(arg); assemblyOptions = true; continue; } else if (arg.EndsWith(".xsd")) { if (!readingFiles) { Error(incorrectOrder); } schemaNames.Add(arg); schemasOptions = true; continue; } else if (arg.EndsWith(".xml")) { if (generateClasses || generateDataset) { Error(duplicatedParam); } inferenceNames.Add(arg); inference = true; continue; } else if (!arg.StartsWith("/")) { if (!readingFiles) { Error(incorrectOrder); } unknownFiles.Add(arg); continue; } } readingFiles = false; int i = arg.IndexOf(":"); if (i == -1) { i = arg.Length; } string option = arg.Substring(1, i - 1); string param = (i < arg.Length - 1) ? arg.Substring(i + 1) : ""; if (option == "classes" || option == "c") { if (generateClasses || generateDataset || inference) { Error(duplicatedParam, option); } generateClasses = true; schemasOptions = true; } else if (option == "dataset" || option == "d") { if (generateClasses || generateDataset || inference) { Error(duplicatedParam, option); } generateDataset = true; schemasOptions = true; } else if (option == "element" || option == "e") { elements.Add(param); schemasOptions = true; } else if (option == "language" || option == "l") { if (provider != null) { Error(duplicatedParam, option); } if (language != null) { Error(duplicatedParam, option); } language = param; schemasOptions = true; } else if (option == "namespace" || option == "n") { if (namesp != null) { Error(duplicatedParam, option); } namesp = param; schemasOptions = true; } else if (option == "outputdir" || option == "o") { if (outputDir != null) { Error(duplicatedParam, option); } outputDir = param; } else if (option == "uri" || option == "u") { if (uri != null) { Error(duplicatedParam, option); } uri = param; schemasOptions = true; } else if (option == "type" || option == "t") { lookupTypes.Add(param); assemblyOptions = true; } else if (option == "generator" || option == "g") { providerOption = param; } else if (option == "help" || option == "h") { Console.WriteLine(helpString); return; } else if (option == "nologo") { // ignore, since we do not output a logo anyway } else { Error(unknownOption, option); } } if (!schemasOptions && !assemblyOptions && !inference) { Error(invalidParams); } if (schemasOptions && assemblyOptions) { Error(incompatibleArgs); } if (assemblies.Count > 1) { Error(tooManyAssem); } if (outputDir == null) { outputDir = "."; } string typename = null; Type generatorType = null; if (language != null) { switch (language) { case "CS": provider = new CSharpCodeProvider(); break; case "VB": provider = new VBCodeProvider(); break; default: typename = StripQuot(language); generatorType = Type.GetType(typename); if (generatorType == null) { Error(generatorTypeNotFound, typename); } break; } } if (providerOption != null) { string param = providerOption; int comma = param.IndexOf(','); if (comma < 0) { typename = StripQuot(param); generatorType = Type.GetType(param); } else { typename = param.Substring(0, comma); string asmName = param.Substring(comma + 1); Assembly asm = Assembly.LoadFile(asmName); if (asm == null) { Error(generatorAssemblyNotFound, asmName); } generatorType = asm.GetType(typename); } if (generatorType == null) { Error(generatorTypeNotFound, typename); } } if (generatorType != null) { if (!generatorType.IsSubclassOf(typeof(CodeDomProvider))) { Error(generatorTypeIsNotCodeGenerator, typename); } try { provider = (CodeDomProvider)Activator.CreateInstance(generatorType, null); } catch (Exception ex) { Error(generatorThrewException, generatorType.AssemblyQualifiedName.ToString() + " --> " + ex.Message); } Console.WriteLine("Loaded custom generator type " + generatorType + " ."); } if (provider == null) { provider = new CSharpCodeProvider(); } if (schemasOptions) { if (!generateClasses && !generateDataset) { Error(missingOutputForXsdInput); } schemaNames.AddRange(unknownFiles); if (generateClasses) { GenerateClasses(); } else if (generateDataset) { GenerateDataset(); } } else if (inference) { foreach (string xmlfile in inferenceNames) { string genFile = Path.Combine(outputDir, Path.GetFileNameWithoutExtension(xmlfile) + ".xsd"); DataSet ds = new DataSet(); ds.InferXmlSchema(xmlfile, null); ds.WriteXmlSchema(genFile); Console.WriteLine("Written file " + genFile); } } else { assemblies.AddRange(unknownFiles); GenerateSchemas(); } }
public void SignificantWhitespaceIgnored2() { // To make sure, create pure significant whitespace element // using XmlNodeReader (that does not have xml:space attribute // column). DataSet ds = new DataSet(); XmlDocument doc = new XmlDocument(); doc.AppendChild(doc.CreateElement("root")); doc.DocumentElement.AppendChild(doc.CreateSignificantWhitespace (" \n\n")); XmlReader xr = new XmlNodeReader(doc); ds.InferXmlSchema(xr, null); AssertDataSet("pure_whitespace", ds, "root", 0, 0); }
public void InferXmlSchema_inferingTables4() { //Acroding to the msdn documantaion : //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm //The document, or root, element will result in an inferred table if it has attributes //or child elements that will be inferred as columns. //If the document element has no attributes and no child elements that would be inferred as columns, the element will be inferred as a DataSet // inferingTables4 StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1 attr1='value1' attr2='value2'/>"); sb.Append("</DocumentElement>"); var ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream, null); Assert.Equal("DocumentElement", ds.DataSetName); Assert.Equal("Element1", ds.Tables[0].TableName); Assert.Equal(1, ds.Tables.Count); Assert.Equal("attr1", ds.Tables[0].Columns["attr1"].ColumnName); Assert.Equal("attr2", ds.Tables[0].Columns["attr2"].ColumnName); }
public void inferringRelationships1() { //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringrelationships.htm BeginCase("inferringRelationships1"); Exception exp = null; StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1>"); sb.Append("<ChildElement1 attr1='value1' attr2='value2'/>"); sb.Append("<ChildElement2>Text2</ChildElement2>"); sb.Append("</Element1>"); sb.Append("</DocumentElement>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); try { ds.InferXmlSchema(myStream, null); Compare(ds.DataSetName, "DocumentElement"); Compare(ds.Tables[0].TableName, "Element1"); Compare(ds.Tables[1].TableName, "ChildElement1"); Compare(ds.Tables.Count, 2); Compare(ds.Tables["Element1"].Columns["Element1_Id"].ColumnName, "Element1_Id"); Compare(ds.Tables["Element1"].Columns["Element1_Id"].ColumnMapping, MappingType.Hidden); Compare(ds.Tables["Element1"].Columns["Element1_Id"].DataType, typeof(Int32)); Compare(ds.Tables["Element1"].Columns["ChildElement2"].ColumnName, "ChildElement2"); Compare(ds.Tables["Element1"].Columns["ChildElement2"].ColumnMapping, MappingType.Element); Compare(ds.Tables["Element1"].Columns["ChildElement2"].DataType, typeof(string)); Compare(ds.Tables["ChildElement1"].Columns["attr1"].ColumnName, "attr1"); Compare(ds.Tables["ChildElement1"].Columns["attr1"].ColumnMapping, MappingType.Attribute); Compare(ds.Tables["ChildElement1"].Columns["attr1"].DataType, typeof(string)); Compare(ds.Tables["ChildElement1"].Columns["attr2"].ColumnName, "attr2"); Compare(ds.Tables["ChildElement1"].Columns["attr2"].ColumnMapping, MappingType.Attribute); Compare(ds.Tables["ChildElement1"].Columns["attr2"].DataType, typeof(string)); Compare(ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnName, "Element1_Id"); Compare(ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnMapping, MappingType.Hidden); Compare(ds.Tables["ChildElement1"].Columns["Element1_Id"].DataType, typeof(Int32)); //Checking dataRelation : Compare(ds.Relations["Element1_ChildElement1"].ParentTable.TableName, "Element1"); Compare(ds.Relations["Element1_ChildElement1"].ParentColumns[0].ColumnName, "Element1_Id"); Compare(ds.Relations["Element1_ChildElement1"].ChildTable.TableName, "ChildElement1"); Compare(ds.Relations["Element1_ChildElement1"].ChildColumns[0].ColumnName, "Element1_Id"); Compare(ds.Relations["Element1_ChildElement1"].Nested, true); //Checking ForeignKeyConstraint ForeignKeyConstraint con = (ForeignKeyConstraint)ds.Tables["ChildElement1"].Constraints["Element1_ChildElement1"]; Compare(con.Columns[0].ColumnName, "Element1_Id"); Compare(con.DeleteRule, Rule.Cascade); Compare(con.AcceptRejectRule, AcceptRejectRule.None); Compare(con.RelatedTable.TableName, "Element1"); Compare(con.Table.TableName, "ChildElement1"); } catch (Exception ex) { exp = ex; } finally { EndCase(exp); } }
public void InferXmlSchema_elementText2() { //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringelementtext.htm // elementText1 StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1>"); sb.Append("Text1"); sb.Append("<ChildElement1>Text2</ChildElement1>"); sb.Append("Text3"); sb.Append("</Element1>"); sb.Append("</DocumentElement>"); var ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream, null); Assert.Equal("DocumentElement", ds.DataSetName); Assert.Equal("Element1", ds.Tables[0].TableName); Assert.Equal(1, ds.Tables.Count); Assert.Equal("ChildElement1", ds.Tables["Element1"].Columns["ChildElement1"].ColumnName); Assert.Equal(MappingType.Element, ds.Tables["Element1"].Columns["ChildElement1"].ColumnMapping); Assert.Equal(typeof(string), ds.Tables["Element1"].Columns["ChildElement1"].DataType); Assert.Equal(1, ds.Tables["Element1"].Columns.Count); }