Imported Upstream version 3.6.0

Former-commit-id: da6be194a6b1221998fc28233f2503bd61dd9d14
This commit is contained in:
Jo Shields
2014-08-13 10:39:27 +01:00
commit a575963da9
50588 changed files with 8155799 additions and 0 deletions

View File

@@ -0,0 +1,66 @@
using System;
using System.IO;
using System.Xml.Serialization;
public class PurchaseOrder
{
public Item[] ItemsOrdered;
}
public class Item
{
public string ItemID;
public decimal ItemPrice;
/* Needed so it can be serialized */
public Item()
{}
public Item(string id, decimal price)
{
ItemID=id;
ItemPrice=price;
}
}
public class Test
{
public static void Main()
{
Test t=new Test();
t.Create("array.xml");
t.Read("array.xml");
}
private void Create(string filename)
{
XmlSerializer ser=new XmlSerializer(typeof(PurchaseOrder));
PurchaseOrder po=new PurchaseOrder();
Item item1=new Item("aaa111", (decimal)34.22);
Item item2=new Item("bbb222", (decimal)2.89);
po.ItemsOrdered=new Item[2];
po.ItemsOrdered[0]=item1;
po.ItemsOrdered[1]=item2;
TextWriter writer=new StreamWriter(filename);
ser.Serialize(writer, po);
writer.Close();
}
private void Read(string filename)
{
XmlSerializer ser=new XmlSerializer(typeof(PurchaseOrder));
FileStream fs=new FileStream(filename, FileMode.Open);
PurchaseOrder po;
po=(PurchaseOrder)ser.Deserialize(fs);
fs.Close();
foreach(Item item in po.ItemsOrdered)
{
Console.WriteLine("Item: "+item.ItemID);
Console.WriteLine("Price: "+item.ItemPrice);
}
}
}

View File

@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<PurchaseOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ItemsOrdered>
<Item>
<ItemID>aaa111</ItemID>
<ItemPrice>34.22</ItemPrice>
</Item>
<Item>
<ItemID>bbb222</ItemID>
<ItemPrice>2.89</ItemPrice>
</Item>
</ItemsOrdered>
</PurchaseOrder>

View File

@@ -0,0 +1,48 @@
using System;
using System.IO;
using System.Xml.Serialization;
public class PurchaseOrder
{
public Address MyAddress;
}
public class Address
{
public string FirstName;
}
public class Test
{
public static void Main()
{
Test t=new Test();
t.Create("complex.xml");
t.Read("complex.xml");
}
private void Create(string filename)
{
XmlSerializer ser=new XmlSerializer(typeof(PurchaseOrder));
PurchaseOrder po=new PurchaseOrder();
Address addr=new Address();
addr.FirstName="George";
po.MyAddress=addr;
TextWriter writer=new StreamWriter(filename);
ser.Serialize(writer, po);
writer.Close();
}
private void Read(string filename)
{
XmlSerializer ser=new XmlSerializer(typeof(PurchaseOrder));
FileStream fs=new FileStream(filename, FileMode.Open);
PurchaseOrder po;
po=(PurchaseOrder)ser.Deserialize(fs);
fs.Close();
Console.WriteLine("Name: "+po.MyAddress.FirstName);
}
}

View File

@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<PurchaseOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<MyAddress>
<FirstName>George</FirstName>
</MyAddress>
</PurchaseOrder>

View File

@@ -0,0 +1,70 @@
using System;
using System.IO;
using System.Xml.Serialization;
using System.Data;
public class Test
{
public static void Main()
{
Test t=new Test();
t.Create("dataset.xml");
t.Read("dataset.xml");
}
private void Create(string filename)
{
XmlSerializer ser=new XmlSerializer(typeof(DataSet));
/* Create a new DataSet; add a table, column and ten rows */
DataSet ds=new DataSet("myDataSet");
DataTable t=new DataTable("table1");
DataColumn c=new DataColumn("thing");
t.Columns.Add(c);
ds.Tables.Add(t);
DataRow r;
for(int i=0; i<10; i++) {
r=t.NewRow();
r[0]="Thing "+i;
t.Rows.Add(r);
}
TextWriter writer=new StreamWriter(filename);
ser.Serialize(writer, ds);
writer.Close();
}
private void Read(string filename)
{
XmlSerializer ser=new XmlSerializer(typeof(DataSet));
FileStream fs=new FileStream(filename, FileMode.Open);
DataSet ds;
ds=(DataSet)ser.Deserialize(fs);
fs.Close();
Console.WriteLine("DataSet name: "+ds.DataSetName);
Console.WriteLine("DataSet locale: "+ds.Locale.Name);
foreach(DataTable t in ds.Tables)
{
Console.WriteLine("Table name: "+t.TableName);
Console.WriteLine("Table locale: "+t.Locale.Name);
foreach(DataColumn c in t.Columns)
{
Console.WriteLine("Column name: "+c.ColumnName);
Console.WriteLine("Null allowed? "+c.AllowDBNull);
}
foreach(DataRow r in t.Rows)
{
Console.WriteLine("Row: "+(string)r[0]);
}
}
}
}

View File

@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<DataSet>
<xs:schema id="myDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="myDataSet" msdata:IsDataSet="true" msdata:Locale="en-GB">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="table1">
<xs:complexType>
<xs:sequence>
<xs:element name="thing" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</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">
<myDataSet>
<table1 diffgr:id="table11" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<thing>Thing 0</thing>
</table1>
<table1 diffgr:id="table12" msdata:rowOrder="1" diffgr:hasChanges="inserted">
<thing>Thing 1</thing>
</table1>
<table1 diffgr:id="table13" msdata:rowOrder="2" diffgr:hasChanges="inserted">
<thing>Thing 2</thing>
</table1>
<table1 diffgr:id="table14" msdata:rowOrder="3" diffgr:hasChanges="inserted">
<thing>Thing 3</thing>
</table1>
<table1 diffgr:id="table15" msdata:rowOrder="4" diffgr:hasChanges="inserted">
<thing>Thing 4</thing>
</table1>
<table1 diffgr:id="table16" msdata:rowOrder="5" diffgr:hasChanges="inserted">
<thing>Thing 5</thing>
</table1>
<table1 diffgr:id="table17" msdata:rowOrder="6" diffgr:hasChanges="inserted">
<thing>Thing 6</thing>
</table1>
<table1 diffgr:id="table18" msdata:rowOrder="7" diffgr:hasChanges="inserted">
<thing>Thing 7</thing>
</table1>
<table1 diffgr:id="table19" msdata:rowOrder="8" diffgr:hasChanges="inserted">
<thing>Thing 8</thing>
</table1>
<table1 diffgr:id="table110" msdata:rowOrder="9" diffgr:hasChanges="inserted">
<thing>Thing 9</thing>
</table1>
</myDataSet>
</diffgr:diffgram>
</DataSet>

View File

@@ -0,0 +1,114 @@
using System;
using System.IO;
using System.Collections;
using System.Xml.Serialization;
public class Test
{
public static void Main()
{
Test t=new Test();
t.Create("icollection.xml");
t.Read("icollection.xml");
}
private void Create(string filename)
{
Employees emps=new Employees();
/* Note that only the collection is serialized, not
* the CollectionName or any other public property of
* the class.
*/
emps.CollectionName="Employees";
Employee john100=new Employee("John", "100xxx");
emps.Add(john100);
XmlSerializer ser=new XmlSerializer(typeof(Employees));
TextWriter writer=new StreamWriter(filename);
ser.Serialize(writer, emps);
writer.Close();
}
private void Read(string filename)
{
XmlSerializer ser=new XmlSerializer(typeof(Employees));
FileStream fs=new FileStream(filename, FileMode.Open);
Employees emps;
emps=(Employees)ser.Deserialize(fs);
fs.Close();
/* Not serialized! */
Console.WriteLine("Collection name: "+emps.CollectionName);
foreach(Employee emp in emps)
{
Console.WriteLine("Employee name: "+emp.EmpName);
Console.WriteLine("Employee ID: "+emp.EmpID);
}
}
}
public class Employees:ICollection
{
public string CollectionName;
private ArrayList empArray=new ArrayList();
public Employee this[int index]
{
get {
return((Employee)empArray[index]);
}
}
public void CopyTo(Array a, int index)
{
empArray.CopyTo(a, index);
}
public int Count
{
get {
return(empArray.Count);
}
}
public object SyncRoot
{
get {
return(this);
}
}
public bool IsSynchronized
{
get {
return(false);
}
}
public IEnumerator GetEnumerator()
{
return(empArray.GetEnumerator());
}
public void Add(Employee newEmployee)
{
empArray.Add(newEmployee);
}
}
public class Employee
{
public string EmpName;
public string EmpID;
public Employee()
{}
public Employee(string empName, string empID)
{
EmpName=empName;
EmpID=empID;
}
}

View File

@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfEmployee xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Employee>
<EmpName>John</EmpName>
<EmpID>100xxx</EmpID>
</Employee>
</ArrayOfEmployee>

View File

@@ -0,0 +1,184 @@
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
// The XmlRootAttribute allows you to set an alterate name
// (PurchaseOrder) for the XML element and its namespace. By
// default, the XmlSerializer uses the class name. The attribute
// also allows you to set the XML namespace for the element. Lastly,
// the attribute sets the IsNullable property, which specifies whether
// the xsi:null attribute appears if the class instance is set to
// a null reference.
[XmlRootAttribute("PurchaseOrder", Namespace="http://cpandl.com",
IsNullable=false)]
public class PurchaseOrder
{
public Address ShipTo;
public string OrderDate;
// The XmlArrayAttribute changes the XML element name
// from the default of "OrderedItems" to "Items".
[XmlArrayAttribute("Items")]
public OrderedItem[] OrderedItems;
public decimal SubTotal;
public decimal ShipCost;
public decimal TotalCost;
}
public class Address
{
// The XmlAttribute instructs the XmlSerializer to serialize the Name
// field as an XML attribute instead of an XML element (the default
// behaviour).
[XmlAttribute]
public string Name;
public string Line1;
// Setting the IsNullable property to false instructs the
// XmlSerializer that the XML attribute will not appear if
// the City field is set to a null reference.
[XmlElementAttribute(IsNullable=false)]
public string City;
public string State;
public string Zip;
}
public class OrderedItem
{
public string ItemName;
public string Description;
public decimal UnitPrice;
public int Quantity;
public decimal LineTotal;
// Calculate is a custom method that calculates the price per item
// and stores the value in a field.
public void Calculate()
{
LineTotal=UnitPrice*Quantity;
}
}
public class Test
{
public static void Main()
{
// Read and write purchase orders.
Test t=new Test();
t.CreatePO("potest.xml");
t.ReadPO("potest.xml");
}
private void CreatePO(string filename)
{
// Creates an instance of the XmlSerializer class;
// specifies the type of object to serialize.
XmlSerializer serializer=new XmlSerializer(typeof(PurchaseOrder));
TextWriter writer=new StreamWriter(filename);
PurchaseOrder po=new PurchaseOrder();
// Creates an address to ship and bill to.
Address billAddress=new Address();
billAddress.Name="Teresa Atkinson";
billAddress.Line1="1 Main St.";
billAddress.City="AnyTown";
billAddress.State="WA";
billAddress.Zip="00000";
// Sets ShipTo and BillTo to the same addressee.
po.ShipTo=billAddress;
po.OrderDate=System.DateTime.Now.ToLongDateString();
// Creates an OrderedItem.
OrderedItem i1=new OrderedItem();
i1.ItemName="Widget S";
i1.Description="Small widget";
i1.UnitPrice=(decimal)5.23;
i1.Quantity=3;
i1.Calculate();
// Inserts the item into the array.
OrderedItem[] items={i1};
po.OrderedItems=items;
// Calculate the total cost.
decimal subTotal=new decimal();
foreach(OrderedItem oi in items)
{
subTotal+=oi.LineTotal;
}
po.SubTotal=subTotal;
po.ShipCost=(decimal)12.51;
po.TotalCost=po.SubTotal+po.ShipCost;
// Serializes the purchase order, and closes the TextWriter.
serializer.Serialize(writer, po);
writer.Close();
}
protected void ReadPO(string filename)
{
// Creates an instance of the XmlSerializer class;
// specifies the type of object to be deserialized.
XmlSerializer serializer=new XmlSerializer(typeof(PurchaseOrder));
// If the XML document has been altered with unknown
// nodes or attributes, handles them with the
// UnknownNode and UnknownAttribute events.
serializer.UnknownNode+=new XmlNodeEventHandler(serializer_UnknownNode);
serializer.UnknownAttribute+=new XmlAttributeEventHandler(serializer_UnknownAttribute);
// A FileStream is needed to read the XML document.
FileStream fs=new FileStream(filename, FileMode.Open);
// Declares an object variable of the type to be deserialized.
PurchaseOrder po;
// Uses the Deserialize method to restore the object's state with
// data from the XML document. */
po=(PurchaseOrder)serializer.Deserialize(fs);
fs.Close();
// Reads the order date.
Console.WriteLine("OrderDate: "+po.OrderDate);
// Reads the shipping address.
Address shipTo=po.ShipTo;
ReadAddress(shipTo, "Ship To:");
// Reads the list of ordered items.
OrderedItem[] items=po.OrderedItems;
Console.WriteLine("Items to be shipped:");
foreach(OrderedItem oi in items)
{
Console.WriteLine("\t"+
oi.ItemName+"\t"+
oi.Description+"\t"+
oi.UnitPrice+"\t"+
oi.Quantity+"\t"+
oi.LineTotal);
}
// Reads the subtotal, shipping cost, and total cost.
Console.WriteLine("\n\t\t\t\t\t Subtotal\t"+po.SubTotal+
"\n\t\t\t\t\t Shipping\t"+po.ShipCost+
"\n\t\t\t\t\t Total\t\t"+po.TotalCost);
}
protected void ReadAddress(Address a, string label)
{
// Reads the fields of the Address.
Console.WriteLine(label);
Console.Write("\t"+
a.Name+"\n\t"+
a.Line1+"\n\t"+
a.City+"\t"+
a.State+"\n\t"+
a.Zip+"\n");
}
protected void serializer_UnknownNode(object sender,
XmlNodeEventArgs e)
{
Console.WriteLine("Unknown Node:"+e.Name+"\t"+e.Text);
}
protected void serializer_UnknownAttribute(object sender,
XmlAttributeEventArgs e)
{
System.Xml.XmlAttribute attr=e.Attr;
Console.WriteLine("Unknown attribute "+attr.Name+"='"+attr.Value+"'");
}
}

View File

@@ -0,0 +1,22 @@
<?xml version="1.0" encoding="utf-8"?>
<PurchaseOrder xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://cpandl.com">
<ShipTo Name="Teresa Atkinson">
<Line1>1 Main St.</Line1>
<City>AnyTown</City>
<State>WA</State>
<Zip>00000</Zip>
</ShipTo>
<OrderDate>13 May 2003</OrderDate>
<Items>
<OrderedItem>
<ItemName>Widget S</ItemName>
<Description>Small widget</Description>
<UnitPrice>5.23</UnitPrice>
<Quantity>3</Quantity>
<LineTotal>15.69</LineTotal>
</OrderedItem>
</Items>
<SubTotal>15.69</SubTotal>
<ShipCost>12.51</ShipCost>
<TotalCost>28.20</TotalCost>
</PurchaseOrder>

View File

@@ -0,0 +1,38 @@
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Test
{
public static void Main()
{
Test t=new Test();
t.Create("xmlelement.xml");
t.Read("xmlelement.xml");
}
private void Create(string filename)
{
XmlSerializer ser=new XmlSerializer(typeof(XmlElement));
XmlElement elem=new XmlDocument().CreateElement("MyElement", "ns");
elem.InnerText="Hello, World!";
TextWriter writer=new StreamWriter(filename);
ser.Serialize(writer, elem);
writer.Close();
}
private void Read(string filename)
{
XmlSerializer ser=new XmlSerializer(typeof(XmlElement));
FileStream fs=new FileStream(filename, FileMode.Open);
XmlElement elem;
elem=(XmlElement)ser.Deserialize(fs);
fs.Close();
Console.WriteLine("Node name: "+elem.Name);
Console.WriteLine("Node ns: "+elem.NamespaceURI);
Console.WriteLine("Inner text: "+elem.InnerText);
}
}

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<MyElement xmlns="ns">Hello, World!</MyElement>

View File

@@ -0,0 +1,39 @@
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Test
{
public static void Main()
{
Test t=new Test();
t.Create("xmlnode.xml");
t.Read("xmlnode.xml");
}
private void Create(string filename)
{
XmlSerializer ser=new XmlSerializer(typeof(XmlNode));
XmlNode node=new XmlDocument().CreateNode(XmlNodeType.Element, "MyNode", "ns");
node.InnerText="Hello, World!";
TextWriter writer=new StreamWriter(filename);
ser.Serialize(writer, node);
writer.Close();
}
private void Read(string filename)
{
XmlSerializer ser=new XmlSerializer(typeof(XmlNode));
FileStream fs=new FileStream(filename, FileMode.Open);
XmlNode node;
node=(XmlNode)ser.Deserialize(fs);
fs.Close();
Console.WriteLine("Node type: "+node.NodeType);
Console.WriteLine("Node name: "+node.Name);
Console.WriteLine("Node ns: "+node.NamespaceURI);
Console.WriteLine("Node inner text: "+node.InnerText);
}
}

View File

@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<MyNode xmlns="ns">Hello, World!</MyNode>