本文整理汇总了C#中DataSet.WriteXml方法的典型用法代码示例。如果您正苦于以下问题:C# DataSet.WriteXml方法的具体用法?C# DataSet.WriteXml怎么用?C# DataSet.WriteXml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataSet
的用法示例。
在下文中一共展示了DataSet.WriteXml方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ReadWriteXml
public void ReadWriteXml()
{
var ds = new DataSet();
ds.ReadXml(new StringReader(DataProvider.region));
TextWriter writer = new StringWriter();
ds.WriteXml(writer);
string TextString = writer.ToString();
string substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal("<Root>", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" <Region>", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" <RegionID>1</RegionID>", substring);
// Here the end of line is text markup "\n"
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" <RegionDescription>Eastern", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" </RegionDescription>", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" </Region>", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" <Region>", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" <RegionID>2</RegionID>", substring);
// Here the end of line is text markup "\n"
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" <RegionDescription>Western", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" </RegionDescription>", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" </Region>", substring);
Assert.Equal("</Root>", TextString);
}
示例2: ReadWriteXmlDiffGram
public void ReadWriteXmlDiffGram()
{
var ds = new DataSet();
// It is not a diffgram, so no data loading should be done.
ds.ReadXml(new StringReader(DataProvider.region), XmlReadMode.DiffGram);
TextWriter writer = new StringWriter();
ds.WriteXml(writer);
string TextString = writer.ToString();
Assert.Equal("<NewDataSet />", TextString);
ds.WriteXml(writer, XmlWriteMode.DiffGram);
TextString = writer.ToString();
Assert.Equal("<NewDataSet /><diffgr:diffgram xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\" />", TextString);
ds = new DataSet();
ds.ReadXml(new StringReader(DataProvider.region));
DataTable table = ds.Tables["Region"];
table.Rows[0][0] = "64";
ds.ReadXml(new StringReader(DataProvider.region), XmlReadMode.DiffGram);
ds.WriteXml(writer, XmlWriteMode.DiffGram);
TextString = writer.ToString();
string substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal("<NewDataSet /><diffgr:diffgram xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\" /><diffgr:diffgram xmlns:msdata=\"urn:schemas-microsoft-com:xml-msdata\" xmlns:diffgr=\"urn:schemas-microsoft-com:xml-diffgram-v1\">", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" <Root>", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" <Region diffgr:id=\"Region1\" msdata:rowOrder=\"0\" diffgr:hasChanges=\"inserted\">", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" <RegionID>64</RegionID>", substring);
// not '\n' but literal '\n'
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" <RegionDescription>Eastern", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" </RegionDescription>", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" </Region>", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" <Region diffgr:id=\"Region2\" msdata:rowOrder=\"1\" diffgr:hasChanges=\"inserted\">", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" <RegionID>2</RegionID>", substring);
// not '\n' but literal '\n'
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" <RegionDescription>Western", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" </RegionDescription>", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" </Region>", substring);
substring = TextString.Substring(0, TextString.IndexOfAny(new[] { '\r', '\n' }));
TextString = TextString.Substring(TextString.IndexOf('\n') + 1);
Assert.Equal(" </Root>", substring);
Assert.Equal("</diffgr:diffgram>", TextString);
}
示例3: WriteXmlModeSchema1
public void WriteXmlModeSchema1()
{
string SerializedDataTable =
@"<rdData>
<MyDataTable CustomerID='VINET' CompanyName='Vins et alcools Chevalier' ContactName='Paul Henriot' />
</rdData>";
string expected =
@"<rdData>
<xs:schema id=""rdData"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
<xs:element name=""rdData"" msdata:IsDataSet=""true"" " +
@"msdata:Locale=""en-US"">" +
@"
<xs:complexType>
<xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
<xs:element name=""MyDataTable"">
<xs:complexType>
<xs:attribute name=""CustomerID"" type=""xs:string"" />
<xs:attribute name=""CompanyName"" type=""xs:string"" />
<xs:attribute name=""ContactName"" type=""xs:string"" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<MyDataTable CustomerID=""VINET"" CompanyName=""Vins et alcools Chevalier"" ContactName=""Paul Henriot"" />
</rdData>";
DataSet set;
set = new DataSet();
set.ReadXml(new StringReader(SerializedDataTable));
StringWriter w = new StringWriter();
set.WriteXml(w, XmlWriteMode.WriteSchema);
string result = w.ToString();
Assert.Equal(expected.Replace("\r", ""), result.Replace("\r", ""));
}
示例4: WriteXmlEscapeName
public void WriteXmlEscapeName()
{
// create dataset
DataSet data = new DataSet();
DataTable mainTable = data.Tables.Add("main");
DataColumn mainkey = mainTable.Columns.Add("mainkey", typeof(Guid));
mainTable.Columns.Add("col.2<hi/>", typeof(string));
mainTable.Columns.Add("#col3", typeof(string));
// populate data
mainTable.Rows.Add(new object[] { Guid.NewGuid(), "hi there", "my friend" });
mainTable.Rows.Add(new object[] { Guid.NewGuid(), "what is", "your name" });
mainTable.Rows.Add(new object[] { Guid.NewGuid(), "I have", "a bean" });
// write xml
StringWriter writer = new StringWriter();
data.WriteXml(writer, XmlWriteMode.WriteSchema);
string xml = writer.ToString();
Assert.True(xml.IndexOf("name=\"col.2_x003C_hi_x002F__x003E_\"") > 0);
Assert.True(xml.IndexOf("name=\"_x0023_col3\"") > 0);
Assert.True(xml.IndexOf("<col.2_x003C_hi_x002F__x003E_>hi there</col.2_x003C_hi_x002F__x003E_>") > 0);
// read xml
DataSet data2 = new DataSet();
data2.ReadXml(new StringReader(
writer.GetStringBuilder().ToString()));
}
示例5: WriteXml_Stream
public void WriteXml_Stream()
{
{
var ds = new DataSet();
string input = "<a><b><c>2</c></b></a>";
StringReader sr = new StringReader(input);
XmlTextReader xReader = new XmlTextReader(sr);
ds.ReadXml(xReader);
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter xWriter = new XmlTextWriter(sw);
ds.WriteXml(xWriter);
string output = sb.ToString();
Assert.Equal(input, output);
}
{
var ds = new DataSet();
string input = "<?xml version=\"1.0\" encoding=\"utf-8\" ?><a><b><c>2</c></b></a>";
string expectedOutput = "<a><b><c>2</c></b></a>";
StringReader sr = new StringReader(input);
XmlTextReader xReader = new XmlTextReader(sr);
ds.ReadXml(xReader);
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter xWriter = new XmlTextWriter(sw);
ds.WriteXml(xWriter);
string output = sb.ToString();
Assert.Equal(expectedOutput, output);
}
{
DataSet ds = new DataSet("DSName");
StringWriter sr = new StringWriter();
ds.WriteXml(sr);
Assert.Equal("<DSName />", sr.ToString());
}
{
var ds = new DataSet();
DataTable dt;
//Create parent table.
dt = ds.Tables.Add("ParentTable");
dt.Columns.Add("ParentTable_Id", typeof(int));
dt.Columns.Add("ParentTableCol", typeof(int));
dt.Rows.Add(new object[] { 0, 1 });
//Create child table.
dt = ds.Tables.Add("ChildTable");
dt.Columns.Add("ParentTable_Id", typeof(int));
dt.Columns.Add("ChildTableCol", typeof(string));
dt.Rows.Add(new object[] { 0, "aa" });
//Add a relation between parent and child table.
ds.Relations.Add("ParentTable_ChildTable", ds.Tables["ParentTable"].Columns["ParentTable_Id"], ds.Tables["ChildTable"].Columns["ParentTable_Id"], true);
ds.Relations["ParentTable_ChildTable"].Nested = true;
//Reomve the Parent_Child relation.
dt = ds.Tables["ChildTable"];
dt.ParentRelations.Remove("ParentTable_ChildTable");
//Remove the constraint created automatically to enforce the "ParentTable_ChildTable" relation.
dt.Constraints.Remove("ParentTable_ChildTable");
//Remove the child table from the dataset.
ds.Tables.Remove("ChildTable");
//Get the xml representation of the dataset.
StringWriter sr = new StringWriter();
ds.WriteXml(sr);
string xml = sr.ToString();
Assert.Equal(-1, xml.IndexOf("<ChildTable>"));
}
}
示例6: WriteXmlModeSchema
public void WriteXmlModeSchema()
{
// This is the MS output of WriteXmlSchema().
string xml = @"<Example>
<xs:schema id=""Example"" xmlns="""" xmlns:xs=""http://www.w3.org/2001/XMLSchema"" xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata"">
" +
@" <xs:element name=""Example"" msdata:IsDataSet=""true"" msdata:UseCurrentLocale=""true"">"
+ @"
<xs:complexType>
<xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
<xs:element name=""Dimension"">
<xs:complexType>
<xs:sequence>
<xs:element name=""Number"" type=""xs:int"" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name=""Element"">
<xs:complexType>
<xs:sequence>
<xs:element name=""Dimension"" type=""xs:int"" />
<xs:element name=""Number"" type=""xs:int"" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name=""PK_Dimension"" msdata:PrimaryKey=""true"">
<xs:selector xpath="".//Dimension"" />
<xs:field xpath=""Number"" />
</xs:unique>
<xs:unique name=""PK_Element"" msdata:PrimaryKey=""true"">
<xs:selector xpath="".//Element"" />
<xs:field xpath=""Dimension"" />
<xs:field xpath=""Number"" />
</xs:unique>
<xs:keyref name=""FK_Element_To_Dimension"" refer=""PK_Dimension"">
<xs:selector xpath="".//Element"" />
<xs:field xpath=""Dimension"" />
</xs:keyref>
</xs:element>
</xs:schema>
<Dimension>
<Number>0</Number>
</Dimension>
<Dimension>
<Number>1</Number>
</Dimension>
<Element>
<Dimension>0</Dimension>
<Number>0</Number>
</Element>
<Element>
<Dimension>0</Dimension>
<Number>1</Number>
</Element>
<Element>
<Dimension>0</Dimension>
<Number>2</Number>
</Element>
<Element>
<Dimension>0</Dimension>
<Number>3</Number>
</Element>
<Element>
<Dimension>1</Dimension>
<Number>0</Number>
</Element>
<Element>
<Dimension>1</Dimension>
<Number>1</Number>
</Element>
</Example>";
DataSet ds = new DataSet("Example");
// Dimension DataTable
DataTable dt1 = new DataTable("Dimension");
ds.Tables.Add(dt1);
dt1.Columns.Add(new DataColumn("Number", typeof(int)));
dt1.Columns["Number"].AllowDBNull = false;
dt1.Constraints.Add("PK_Dimension", dt1.Columns["Number"], true);
// Element DataTable
DataTable dt2 = new DataTable("Element");
ds.Tables.Add(dt2);
dt2.Columns.Add(new DataColumn("Dimension", typeof(int)));
dt2.Columns["Dimension"].AllowDBNull = false;
dt2.Columns.Add(new DataColumn("Number", typeof(int)));
dt2.Columns["Number"].AllowDBNull = false;
dt2.Constraints.Add("PK_Element", new DataColumn[] {
dt2.Columns ["Dimension"],
dt2.Columns ["Number"] },
true);
//.........这里部分代码省略.........
示例7: ReadWriteXml2
public void ReadWriteXml2()
{
string xml = "<FullTextResponse><Domains><AvailResponse info='y' name='novell-ximian-group' /><AvailResponse info='n' name='ximian' /></Domains></FullTextResponse>";
var ds = new DataSet();
ds.ReadXml(new StringReader(xml));
AssertDataSet("ds", ds, "FullTextResponse", 2, 1);
DataTable dt = ds.Tables[0];
AssertDataTable("dt1", dt, "Domains", 1, 1, 0, 1, 1, 1);
dt = ds.Tables[1];
AssertDataTable("dt2", dt, "AvailResponse", 3, 2, 1, 0, 1, 0);
StringWriter sw = new StringWriter();
XmlTextWriter xtw = new XmlTextWriter(sw);
xtw.QuoteChar = '\'';
ds.WriteXml(xtw);
Assert.Equal(xml, sw.ToString());
}
示例8: WriteXmlToStream
public void WriteXmlToStream()
{
string xml = "<set><table1><col1>sample text</col1><col2/></table1><table2 attr='value'><col3>sample text 2</col3></table2></set>";
var ds = new DataSet();
ds.ReadXml(new StringReader(xml));
MemoryStream ms = new MemoryStream();
ds.WriteXml(ms);
MemoryStream ms2 = new MemoryStream(ms.ToArray());
StreamReader sr = new StreamReader(ms2, Encoding.UTF8);
string result = @"<set>
<table1>
<col1>sample text</col1>
<col2 />
</table1>
<table2 attr=""value"">
<col3>sample text 2</col3>
</table2>
</set>";
Assert.Equal(sr.ReadToEnd().Replace("\r\n", "\n"), result.Replace("\r\n", "\n"));
}
示例9: DataSetExtendedPropertiesTest
public void DataSetExtendedPropertiesTest()
{
DataSet dataSet1 = new DataSet();
dataSet1.ExtendedProperties.Add("DS1", "extended0");
DataTable table = new DataTable("TABLE1");
table.ExtendedProperties.Add("T1", "extended1");
table.Columns.Add("C1", typeof(int));
table.Columns.Add("C2", typeof(string));
table.Columns[1].MaxLength = 20;
table.Columns[0].ExtendedProperties.Add("C1Ext1", "extended2");
table.Columns[1].ExtendedProperties.Add("C2Ext1", "extended3");
dataSet1.Tables.Add(table);
table.LoadDataRow(new object[] { 1, "One" }, false);
table.LoadDataRow(new object[] { 2, "Two" }, false);
string file = Path.Combine(Path.GetTempPath(), "schemas-test.xml");
try
{
dataSet1.WriteXml(file, XmlWriteMode.WriteSchema);
}
catch (Exception ex)
{
Assert.False(true);
}
finally
{
File.Delete(file);
}
DataSet dataSet2 = new DataSet();
dataSet2.ReadXml(new StringReader(
@"<?xml version=""1.0"" standalone=""yes""?>
<NewDataSet>
<xs:schema id=""NewDataSet"" xmlns=""""
xmlns:xs=""http://www.w3.org/2001/XMLSchema""
xmlns:msdata=""urn:schemas-microsoft-com:xml-msdata""
xmlns:msprop=""urn:schemas-microsoft-com:xml-msprop"">
<xs:element name=""NewDataSet"" msdata:IsDataSet=""true""
msdata:UseCurrentLocale=""true"" msprop:DS1=""extended0"">
<xs:complexType>
<xs:choice minOccurs=""0"" maxOccurs=""unbounded"">
<xs:element name=""TABLE1"" msprop:T1=""extended1"">
<xs:complexType>
<xs:sequence>
<xs:element name=""C1"" type=""xs:int"" minOccurs=""0""
msprop:C1Ext1=""extended2"" />
<xs:element name=""C2"" type=""xs:string"" minOccurs=""0""
msprop:C2Ext1=""extended3"" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<TABLE1>
<C1>1</C1>
<C2>One</C2>
</TABLE1>
<TABLE1>
<C1>2</C1>
<C2>Two</C2>
</TABLE1>
</NewDataSet>"), XmlReadMode.ReadSchema);
Assert.Equal(dataSet1.ExtendedProperties["DS1"], dataSet2.ExtendedProperties["DS1"]);
Assert.Equal(dataSet1.Tables[0].ExtendedProperties["T1"], dataSet2.Tables[0].ExtendedProperties["T1"]);
Assert.Equal(dataSet1.Tables[0].Columns[0].ExtendedProperties["C1Ext1"],
dataSet2.Tables[0].Columns[0].ExtendedProperties["C1Ext1"]);
Assert.Equal(dataSet1.Tables[0].Columns[1].ExtendedProperties["C2Ext1"],
dataSet2.Tables[0].Columns[1].ExtendedProperties["C2Ext1"]);
}
示例10: WtiteXmlEncodedXml
public void WtiteXmlEncodedXml()
{
string xml = @"<an_x0020_example_x0020_dataset.>
<WOW_x0021__x0020_that_x0027_s_x0020_nasty...>
<URL_x0020_is_x0020_http_x003A__x002F__x002F_www.go-mono.com>content string.</URL_x0020_is_x0020_http_x003A__x002F__x002F_www.go-mono.com>
</WOW_x0021__x0020_that_x0027_s_x0020_nasty...>
</an_x0020_example_x0020_dataset.>";
DataSet ds = new DataSet("an example dataset.");
ds.Tables.Add(new DataTable("WOW! that's nasty..."));
ds.Tables[0].Columns.Add("URL is http://www.go-mono.com");
ds.Tables[0].Rows.Add(new object[] { "content string." });
StringWriter sw = new StringWriter();
ds.WriteXml(sw);
Assert.Equal(sw.ToString().Replace("\r\n", "\n"), xml.Replace("\r\n", "\n"));
}
示例11: Serialize
public void Serialize()
{
MemoryStream fs = new MemoryStream();
// Construct a BinaryFormatter and use it
// to serialize the data to the stream.
BinaryFormatter formatter = new BinaryFormatter();
// Create an array with multiple elements refering to
// the one Singleton object.
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Id", typeof(string)));
dt.Columns.Add(new DataColumn("ContactName", typeof(string)));
dt.Columns.Add(new DataColumn("ContactTitle", typeof(string)));
dt.Columns.Add(new DataColumn("ContactAreaCode", typeof(string)));
dt.Columns.Add(new DataColumn("ContactPhone", typeof(string)));
DataRow loRowToAdd;
loRowToAdd = dt.NewRow();
loRowToAdd[0] = "a";
loRowToAdd[1] = "b";
loRowToAdd[2] = "c";
loRowToAdd[3] = "d";
loRowToAdd[4] = "e";
dt.Rows.Add(loRowToAdd);
DataTable[] dtarr = new DataTable[] { dt };
// Serialize the array elements.
formatter.Serialize(fs, dtarr);
// Deserialize the array elements.
fs.Position = 0;
DataTable[] a2 = (DataTable[])formatter.Deserialize(fs);
var ds = new DataSet();
ds.Tables.Add(a2[0]);
StringWriter sw = new StringWriter();
ds.WriteXml(sw);
XmlDocument doc = new XmlDocument();
doc.LoadXml(sw.ToString());
Assert.Equal(5, doc.DocumentElement.FirstChild.ChildNodes.Count);
}
示例12: WriteXml_Morethan2Relations
public void WriteXml_Morethan2Relations()
{
DataSet ds = new DataSet();
DataTable p1 = ds.Tables.Add("parent1");
DataTable p2 = ds.Tables.Add("parent2");
DataTable p3 = ds.Tables.Add("parent3");
DataTable c1 = ds.Tables.Add("child");
c1.Columns.Add("col1");
c1.Columns.Add("col2");
c1.Columns.Add("col3");
c1.Columns.Add("col4");
p1.Columns.Add("col1");
p2.Columns.Add("col1");
p3.Columns.Add("col1");
ds.Relations.Add("rel1", p1.Columns[0], c1.Columns[0], false);
ds.Relations.Add("rel2", p2.Columns[0], c1.Columns[1], false);
ds.Relations.Add("rel3", p3.Columns[0], c1.Columns[2], false);
ds.Relations[2].Nested = true;
p1.Rows.Add(new object[] { "p1" });
p2.Rows.Add(new object[] { "p2" });
p3.Rows.Add(new object[] { "p3" });
c1.Rows.Add(new object[] { "p1", "p2", "p3", "c1" });
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
ds.WriteXml(xw);
string dataset_xml = sw.ToString();
string child_xml = "<child><col1>p1</col1><col2>p2</col2><col3>p3</col3><col4>c1</col4></child>";
//the child table data must not be repeated.
Assert.Equal(dataset_xml.IndexOf(child_xml), dataset_xml.LastIndexOf(child_xml));
}
示例13: ReadXml_Diffgram_MissingSchema
public void ReadXml_Diffgram_MissingSchema()
{
DataSet ds = new DataSet();
ds.Tables.Add("table");
ds.Tables[0].Columns.Add("col1");
ds.Tables[0].Columns.Add("col2");
ds.Tables[0].Rows.Add(new object[] { "a", "b" });
ds.Tables[0].Rows.Add(new object[] { "a", "b" });
MemoryStream ms = new MemoryStream();
ds.WriteXml(ms, XmlWriteMode.DiffGram);
DataSet ds1 = new DataSet();
ds1.Tables.Add("table");
ds1.Tables[0].Columns.Add("col1");
// When table schema is missing, it shud load up the data
// for the existing schema
ds1.ReadXml(new MemoryStream(ms.GetBuffer()), XmlReadMode.DiffGram);
Assert.Equal(2, ds1.Tables[0].Rows.Count);
Assert.Equal(1, ds1.Tables[0].Columns.Count);
Assert.Equal("a", ds1.Tables[0].Rows[0][0]);
Assert.Equal("a", ds1.Tables[0].Rows[1][0]);
}
示例14: IgnoreColumnEmptyNamespace
public void IgnoreColumnEmptyNamespace()
{
DataColumn col = new DataColumn("TEST");
col.Namespace = "urn:foo";
DataSet ds = new DataSet("DS");
ds.Namespace = "urn:foo";
DataTable dt = new DataTable("tab");
ds.Tables.Add(dt);
dt.Columns.Add(col);
dt.Rows.Add(new object[] { "test" });
StringWriter sw = new StringWriter();
ds.WriteXml(new XmlTextWriter(sw));
string xml = @"<DS xmlns=""urn:foo""><tab><TEST>test</TEST></tab></DS>";
Assert.Equal(xml, sw.ToString());
}
示例15: ReadWriteXml3
public void ReadWriteXml3()
{
string input = @"<FullTextResponse>
<Domains>
<AvailResponse info='y' name='novell-ximian-group' />
<AvailResponse info='n' name='ximian' />
</Domains>
</FullTextResponse>";
var ds = new DataSet();
ds.ReadXml(new StringReader(input));
StringWriter sw = new StringWriter();
XmlTextWriter xtw = new XmlTextWriter(sw);
xtw.Formatting = Formatting.Indented;
xtw.QuoteChar = '\'';
ds.WriteXml(xtw);
xtw.Flush();
Assert.Equal(input.Replace("\r\n", "\n"), sw.ToString().Replace("\r\n", "\n"));
}