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 static void Main (string [] args) { if (args.Length == 0) { Console.WriteLine ("usage: mono xmldatareader.exe filename"); return; } Console.WriteLine ("Target file: " + args [0]); DataSet ds = new DataSet (); // ds.InferXmlSchema (args [0], null); try { ds.ReadXml (args [0]); } catch (Exception ex) { Console.WriteLine ("ReadXml() borked: " + ex.Message); return; } Console.WriteLine ("---- DataSet ----------------"); StringWriter sw = new StringWriter (); PrintDataSet (ds, sw); PrintDataSet (ds, Console.Out); ds = new DataSet (); ds.InferXmlSchema (args [0], null); XmlDataReader.ReadXml (ds, new XmlTextReader (args [0])); Console.WriteLine ("---- XmlDataReader ----------------"); StringWriter sw2 = new StringWriter (); PrintDataSet (ds, sw2); if (sw.ToString () == sw2.ToString ()) Console.WriteLine ("Successful."); else Console.WriteLine ("Different *************************************************\n" + sw2); }
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); }
public static void Main(string [] args) { if (args.Length == 0) { Console.WriteLine("usage: mono xmldatareader.exe filename"); return; } Console.WriteLine("Target file: " + args [0]); DataSet ds = new DataSet(); // ds.InferXmlSchema (args [0], null); try { ds.ReadXml(args [0]); } catch (Exception ex) { Console.WriteLine("ReadXml() borked: " + ex.Message); return; } Console.WriteLine("---- DataSet ----------------"); StringWriter sw = new StringWriter(); PrintDataSet(ds, sw); PrintDataSet(ds, Console.Out); ds = new DataSet(); ds.InferXmlSchema(args [0], null); XmlDataReader.ReadXml(ds, new XmlTextReader(args [0])); Console.WriteLine("---- XmlDataReader ----------------"); StringWriter sw2 = new StringWriter(); PrintDataSet(ds, sw2); if (sw.ToString() == sw2.ToString()) { Console.WriteLine("Successful."); } else { Console.WriteLine("Different *************************************************\n" + sw2); } }
[Test] 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>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream,null); Assert.AreEqual("DocumentElement", ds.DataSetName, "DS84"); Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS85"); Assert.AreEqual(1, ds.Tables.Count, "DS86"); Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS87"); Assert.AreEqual("attr2", ds.Tables[0].Columns["attr2"].ColumnName, "DS88"); }
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>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream,null); Assert.AreEqual("DocumentElement", ds.DataSetName, "DS89"); Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS90"); Assert.AreEqual(1, ds.Tables.Count, "DS91"); Assert.AreEqual("Element1_Text", ds.Tables[0].Columns["Element1_Text"].ColumnName, "DS92"); }
[Test] public void InferXmlSchema_inferingTables1() { //Acroding to the msdn documantaion : //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm //Elements that have attributes specified in them will result in inferred tables // inferingTables1 StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1 attr1='value1'/>"); sb.Append("<Element1 attr1='value2'>Text1</Element1>"); sb.Append("</DocumentElement>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream,null); Assert.AreEqual("DocumentElement", ds.DataSetName, "DS70"); Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS71"); Assert.AreEqual(1, ds.Tables.Count, "DS72"); Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS73"); Assert.AreEqual("Element1_Text", ds.Tables[0].Columns["Element1_Text"].ColumnName, "DS74"); }
[Test] public void InferXmlSchema_inferingTables2() { //Acroding to the msdn documantaion : //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringtables.htm //Elements that have child elements will result in inferred tables // inferingTables2 StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1>"); sb.Append("<ChildElement1>Text1</ChildElement1>"); sb.Append("</Element1>"); sb.Append("</DocumentElement>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream,null); Assert.AreEqual("DocumentElement", ds.DataSetName, "DS75"); Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS76"); Assert.AreEqual(1, ds.Tables.Count, "DS77"); Assert.AreEqual("ChildElement1", ds.Tables[0].Columns["ChildElement1"].ColumnName, "DS78"); }
[Test] 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>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream,null); Assert.AreEqual("DocumentElement", ds.DataSetName, "DS149"); Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS150"); Assert.AreEqual(1, ds.Tables.Count, "DS151"); Assert.AreEqual("ChildElement1", ds.Tables["Element1"].Columns["ChildElement1"].ColumnName, "DS152"); Assert.AreEqual(MappingType.Element, ds.Tables["Element1"].Columns["ChildElement1"].ColumnMapping , "DS153"); Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["ChildElement1"].DataType , "DS154"); Assert.AreEqual(1, ds.Tables["Element1"].Columns.Count, "DS155"); }
public void 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 BeginCase("inferingTables4"); 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"); } catch (Exception ex) { exp = ex; } finally { EndCase(exp); } }
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); #if NET_1_1 Assembly asm = Assembly.LoadFile (asmName); #else Assembly asm = Assembly.LoadFrom (asmName); #endif 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 (); } }
[Test] 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>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream,null); Assert.AreEqual("DocumentElement", ds.DataSetName, "DS111"); Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS112"); Assert.AreEqual("ChildElement1", ds.Tables[1].TableName, "DS113"); Assert.AreEqual(2, ds.Tables.Count, "DS114"); Assert.AreEqual("Element1_Id", ds.Tables["Element1"].Columns["Element1_Id"].ColumnName, "DS115"); Assert.AreEqual(MappingType.Hidden, ds.Tables["Element1"].Columns["Element1_Id"].ColumnMapping , "DS116"); Assert.AreEqual(typeof(Int32), ds.Tables["Element1"].Columns["Element1_Id"].DataType , "DS117"); Assert.AreEqual("ChildElement2", ds.Tables["Element1"].Columns["ChildElement2"].ColumnName, "DS118"); Assert.AreEqual(MappingType.Element, ds.Tables["Element1"].Columns["ChildElement2"].ColumnMapping , "DS119"); Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["ChildElement2"].DataType , "DS120"); Assert.AreEqual("attr1", ds.Tables["ChildElement1"].Columns["attr1"].ColumnName, "DS121"); Assert.AreEqual(MappingType.Attribute, ds.Tables["ChildElement1"].Columns["attr1"].ColumnMapping , "DS122"); Assert.AreEqual(typeof(string), ds.Tables["ChildElement1"].Columns["attr1"].DataType , "DS123"); Assert.AreEqual("attr2", ds.Tables["ChildElement1"].Columns["attr2"].ColumnName, "DS124"); Assert.AreEqual(MappingType.Attribute, ds.Tables["ChildElement1"].Columns["attr2"].ColumnMapping , "DS125"); Assert.AreEqual(typeof(string), ds.Tables["ChildElement1"].Columns["attr2"].DataType , "DS126"); Assert.AreEqual("Element1_Id", ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnName, "DS127"); Assert.AreEqual(MappingType.Hidden, ds.Tables["ChildElement1"].Columns["Element1_Id"].ColumnMapping , "DS128"); Assert.AreEqual(typeof(Int32), ds.Tables["ChildElement1"].Columns["Element1_Id"].DataType , "DS129"); //Checking dataRelation : Assert.AreEqual("Element1", ds.Relations["Element1_ChildElement1"].ParentTable.TableName, "DS130"); Assert.AreEqual("Element1_Id", ds.Relations["Element1_ChildElement1"].ParentColumns[0].ColumnName, "DS131"); Assert.AreEqual("ChildElement1", ds.Relations["Element1_ChildElement1"].ChildTable.TableName, "DS132"); Assert.AreEqual("Element1_Id", ds.Relations["Element1_ChildElement1"].ChildColumns[0].ColumnName, "DS133"); Assert.AreEqual(true, ds.Relations["Element1_ChildElement1"].Nested, "DS134"); //Checking ForeignKeyConstraint ForeignKeyConstraint con = (ForeignKeyConstraint)ds.Tables["ChildElement1"].Constraints["Element1_ChildElement1"]; Assert.AreEqual("Element1_Id", con.Columns[0].ColumnName, "DS135"); Assert.AreEqual(Rule.Cascade, con.DeleteRule, "DS136"); Assert.AreEqual(AcceptRejectRule.None, con.AcceptRejectRule, "DS137"); Assert.AreEqual("Element1", con.RelatedTable.TableName, "DS138"); Assert.AreEqual("ChildElement1", con.Table.TableName, "DS139"); }
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 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 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); } }
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); } }
[Test] 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>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream,null); Assert.AreEqual("DocumentElement", ds.DataSetName, "DS93"); Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS94"); Assert.AreEqual(1, ds.Tables.Count, "DS95"); Assert.AreEqual("attr1", ds.Tables[0].Columns["attr1"].ColumnName, "DS96"); Assert.AreEqual("attr2", ds.Tables[0].Columns["attr2"].ColumnName, "DS97"); Assert.AreEqual(MappingType.Attribute, ds.Tables[0].Columns["attr1"].ColumnMapping , "DS98"); Assert.AreEqual(MappingType.Attribute, ds.Tables[0].Columns["attr2"].ColumnMapping , "DS99"); Assert.AreEqual(typeof(string), ds.Tables[0].Columns["attr1"].DataType , "DS100"); Assert.AreEqual(typeof(string), ds.Tables[0].Columns["attr2"].DataType , "DS101"); }
/* // 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; }
[Test] 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>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream,null); Assert.AreEqual("DocumentElement", ds.DataSetName, "DS102"); Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS103"); Assert.AreEqual(1, ds.Tables.Count, "DS104"); Assert.AreEqual("ChildElement1", ds.Tables[0].Columns["ChildElement1"].ColumnName, "DS105"); Assert.AreEqual("ChildElement2", ds.Tables[0].Columns["ChildElement2"].ColumnName, "DS106"); Assert.AreEqual(MappingType.Element, ds.Tables[0].Columns["ChildElement1"].ColumnMapping , "DS107"); Assert.AreEqual(MappingType.Element, ds.Tables[0].Columns["ChildElement2"].ColumnMapping , "DS108"); Assert.AreEqual(typeof(string), ds.Tables[0].Columns["ChildElement1"].DataType , "DS109"); Assert.AreEqual(typeof(string), ds.Tables[0].Columns["ChildElement2"].DataType , "DS110"); }
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); }
[Test] public void InferXmlSchema_elementText1() { //ms-help://MS.MSDNQTR.2003FEB.1033/cpguide/html/cpconinferringelementtext.htm // elementText1 StringBuilder sb = new StringBuilder(); sb.Append("<DocumentElement>"); sb.Append("<Element1 attr1='value1'>Text1</Element1>"); sb.Append("</DocumentElement>"); DataSet ds = new DataSet(); MemoryStream myStream = new MemoryStream(new ASCIIEncoding().GetBytes(sb.ToString())); ds.InferXmlSchema(myStream,null); Assert.AreEqual("DocumentElement", ds.DataSetName, "DS140"); Assert.AreEqual("Element1", ds.Tables[0].TableName, "DS141"); Assert.AreEqual(1, ds.Tables.Count, "DS142"); Assert.AreEqual("attr1", ds.Tables["Element1"].Columns["attr1"].ColumnName, "DS143"); Assert.AreEqual(MappingType.Attribute, ds.Tables["Element1"].Columns["attr1"].ColumnMapping , "DS144"); Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["attr1"].DataType , "DS145"); Assert.AreEqual("Element1_Text", ds.Tables["Element1"].Columns["Element1_Text"].ColumnName, "DS146"); Assert.AreEqual(MappingType.SimpleContent, ds.Tables["Element1"].Columns["Element1_Text"].ColumnMapping , "DS147"); Assert.AreEqual(typeof(string), ds.Tables["Element1"].Columns["Element1_Text"].DataType , "DS148"); }
public void test5() { 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())); DataSet ds = new DataSet(); // ds.ReadXml(myStream); ds.InferXmlSchema(myStream, new string[] {"urn:schemas-microsoft-com:officedata"}); Compare(ds.Tables.Count,3); Compare(ds.Tables[0].Columns.Count,3); Compare(ds.Tables[0].Columns["CategoryID"].ColumnName,"CategoryID"); Compare(ds.Tables[0].Columns["Categories_Id"].ColumnName,"Categories_Id");//Hidden Compare(ds.Tables[0].Columns["Description"].ColumnName,"Description"); Compare(ds.Tables[1].Columns.Count,3); Compare(ds.Tables[1].Columns["adotype"].ColumnName,"adotype"); Compare(ds.Tables[1].Columns["CategoryName_Text"].ColumnName,"CategoryName_Text"); Compare(ds.Tables[1].Columns["Categories_Id"].ColumnName,"Categories_Id");//Hidden Compare(ds.Tables[2].Columns.Count,3); Compare(ds.Tables[2].Columns["ProductID"].ColumnName,"ProductID"); Compare(ds.Tables[2].Columns["ReorderLevel"].ColumnName,"ReorderLevel"); Compare(ds.Tables[2].Columns["Discontinued"].ColumnName,"Discontinued"); }
[Test] public void InferXmlSchema_WithoutIgnoreNameSpaces() { 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"}); Assert.AreEqual(8, ds.Tables.Count, "DS52"); }
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 NullFileName () { DataSet ds = new DataSet (); ds.InferXmlSchema ((XmlReader) null, null); AssertDataSet ("null", ds, "NewDataSet", 0, 0); }
[Test] 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())); DataSet ds = new DataSet(); // ds.ReadXml(myStream); ds.InferXmlSchema(myStream, new string[] {"urn:schemas-microsoft-com:officedata"}); Assert.AreEqual(3, ds.Tables.Count, "DS53"); Assert.AreEqual(3, ds.Tables[0].Columns.Count, "DS54"); Assert.AreEqual("CategoryID", ds.Tables[0].Columns["CategoryID"].ColumnName, "DS55"); Assert.AreEqual("Categories_Id", ds.Tables[0].Columns["Categories_Id"].ColumnName, "DS56");//Hidden Assert.AreEqual("Description", ds.Tables[0].Columns["Description"].ColumnName, "DS57"); Assert.AreEqual(3, ds.Tables[1].Columns.Count, "DS58"); Assert.AreEqual("adotype", ds.Tables[1].Columns["adotype"].ColumnName, "DS59"); Assert.AreEqual("CategoryName_Text", ds.Tables[1].Columns["CategoryName_Text"].ColumnName, "DS60"); Assert.AreEqual("Categories_Id", ds.Tables[1].Columns["Categories_Id"].ColumnName, "DS61");//Hidden Assert.AreEqual(3, ds.Tables[2].Columns.Count, "DS62"); Assert.AreEqual("ProductID", ds.Tables[2].Columns["ProductID"].ColumnName, "DS63"); Assert.AreEqual("ReorderLevel", ds.Tables[2].Columns["ReorderLevel"].ColumnName, "DS64"); Assert.AreEqual("Discontinued", ds.Tables[2].Columns["Discontinued"].ColumnName, "DS65"); }
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); }
[Test] 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); DataSet ds = new DataSet(); ds.InferXmlSchema(myStream, new string[] {"http://www.xml.com/books","http://www.w3.org/HTML/1998/html4"}); //Assert.AreEqual(8, ds.Tables.Count, "DS66"); // string str1 = tempDs.GetXmlSchema(); //DataProvider.GetDSSchema(tempDs); // string str2 = ds.GetXmlSchema(); //DataProvider.GetDSSchema(ds); Assert.AreEqual(3, ds.Tables.Count, "DS67"); Assert.AreEqual("bookreview", ds.Tables[2].TableName, "DS68"); Assert.AreEqual(2, ds.Tables[2].Columns.Count, "DS69"); }