如何使用 C# 以编程方式编辑 xsd 文件
How to edit xsd file programatically using c#
我是第一次使用xsd。
我的任务是:
1. 我需要使用 C# 读取一个巨大的 table 表单 mysql。
2. 我需要准备 xml 模式 - 一个 xsd 文件代表读取 mysql table.
- 现在,我需要以编程方式修改这个 xsd 即每次我可以从这个模式中添加或删除一些列并且
- 最后我需要从这个架构中准备一个数据table。
示例代码:
class Program
{
public static DataTable toBeUpdatedTable;
static void Main(string[] args)
{
// Deserialize the stored
string path = "D:\SerilaizedFile";
ReadMiscInfo(path);
string connectionString = ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString;
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand command;
connection.Open();
DataSet set = new DataSet();
try
{
command = connection.CreateCommand();
command.CommandText = "select * from my_db.table";
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
adapter.Fill(set);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
set.WriteXmlSchema("D:\file.xsd");
// Modify schema code
DataSet ds = new DataSet();
ds.ReadXmlSchema("D:\file.xsd");
if (toBeUpdatedTable.Columns.Count > 0)
{
DataTable tempTable = ds.Tables[0];
foreach (DataColumn col in tempTable.Columns)
{
if (!toBeUpdatedTable.Columns.Contains(col.ColumnName))
toBeUpdatedTable.Columns.Add(col.ColumnName, col.DataType);
}
Console.WriteLine("Table Updated");
}
else
{
toBeUpdatedTable = ds.Tables[0];
Console.WriteLine("Table Created");
}
foreach (DataColumn col in toBeUpdatedTable.Columns)
{
Console.WriteLine(col.ColumnName + " type : " + col.DataType);
}
WriteMiscInfo(path);
Console.ReadLine();
}
private static void WriteMiscInfo(string path)
{
if (path != null)
{
Stream s = new FileStream(path, FileMode.Create);
IFormatter f2 = new BinaryFormatter();
f2.Serialize(s, toBeUpdatedTable);
s.Close();
}
}
private static void ReadMiscInfo(string path)
{
if (File.Exists(path))
{
Stream s = new FileStream(path, FileMode.Open, FileAccess.Read);
IFormatter f1 = new BinaryFormatter();
try
{
toBeUpdatedTable = (DataTable)f1.Deserialize(s);
}
catch
{
s.Close();
toBeUpdatedTable = new DataTable();
}
s.Close();
}
else
{
toBeUpdatedTable = new DataTable();
}
}
}
帮我看看如何修改模式?
您可以使用此处提到的代码here。
我已经复制了你的代码并得到了解决方案。但是我无法维护数据表的记录。
class Program
{
static void Main(string[] args)
{
// Read a mysql table
string connectionString = ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString;
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand command;
connection.Open();
DataSet set = new DataSet();
try
{
command = connection.CreateCommand();
//command.CommandText = "select * from toolsite.user limit 5";
command.CommandText = "select FirstName, LastName, PersonID, City from my_db.Persons limit 5";
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
adapter.Fill(set);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
// Write Sxml Schema
set.WriteXmlSchema("D:\file.xsd");
// Modify schema code
FileStream fs = new FileStream("D:\file.xsd", FileMode.Open);
XmlSchema schema;
ValidationEventHandler eventHandler = new ValidationEventHandler(Program.ShowCompileErrors);
try
{
// Read the schema into an XMLSchema object.
schema = XmlSchema.Read(fs, eventHandler);
// Compile the schema.
schema.Compile(eventHandler);
// Define an XMLSchemaObjectTable to read the schema elements.
// This schematable will contain a single element named "NewDataSet" according to mySql syntax
XmlSchemaObjectTable schematable;
schematable = schema.Elements;
// Define a QualifiedName to identify the elements.
XmlQualifiedName qname = new XmlQualifiedName("NewDataSet", "");
XmlSchemaElement singleElement = new XmlSchemaElement();
XmlSchemaComplexType complextype = new XmlSchemaComplexType();
// See if the XmlSchemaObjectTable has the element.
if (schematable.Contains(qname))
{
singleElement = (XmlSchemaElement)schematable[qname];
if (singleElement.SchemaTypeName.ToString() == "")
{
complextype = (XmlSchemaComplexType)singleElement.SchemaType;
XmlSchemaChoice choice = new XmlSchemaChoice();
choice = (XmlSchemaChoice)complextype.Particle;
XmlSchemaObjectCollection collection = choice.Items;
XmlSchemaObject obj = collection[0];
XmlSchemaComplexType complexsubtype = (XmlSchemaComplexType)((XmlSchemaElement)obj).SchemaType;
XmlSchemaSequence seqelement = new XmlSchemaSequence();
seqelement = (XmlSchemaSequence)complexsubtype.Particle;
// Remove column here
XmlSchemaElement passwordElement = new XmlSchemaElement();
passwordElement = (XmlSchemaElement)seqelement.Items[1];
seqelement.Items.Remove(passwordElement);
// Add any new column here
XmlSchemaElement Newelement = new XmlSchemaElement();
Newelement.Name = "Country";
Newelement.MinOccurs = 0;
Newelement.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
seqelement.Items.Add(Newelement);
}
}
// Display the schema in the Output window.
schema.Write(Console.Out);
Console.ReadLine();
// Save the modified schema
StreamWriter strmWrtr = new StreamWriter("D:\newfile.xsd", false);
schema.Write(strmWrtr);
strmWrtr.Close();
}
catch (XmlSchemaException schemaEx)
{
Console.WriteLine(schemaEx.Message);
}
catch (XmlException xmlEx)
{
Console.WriteLine(xmlEx.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
fs.Close();
}
// Read the modified schema
// PROBLEM HERE : My schema is modified but my data of table is lost. I want to just modify the columns .
//I dont want to lose data
set.ReadXmlSchema("D:\newfile.xsd");
foreach (DataColumn col in set.Tables[0].Columns)
{
Console.WriteLine(col.ColumnName + " type : " + col.DataType);
}
Console.WriteLine("Total records = " + set.Tables[0].Rows.Count);
foreach (DataRow row in set.Tables[0].Rows)
{
for(int i = 0; i < row.ItemArray.Count(); i++)
{
Console.Write(row.ItemArray[i] + " ");
}
Console.WriteLine();
}
Console.ReadLine();
}
public static void ShowCompileErrors(object sender, ValidationEventArgs args)
{
Console.WriteLine("ERROR : Validation Error: {0}", args.Message);
}
}
因此,现在您可能需要有关如何保存数据记录和数据列应根据架构更改的帮助。
我是第一次使用xsd。
我的任务是: 1. 我需要使用 C# 读取一个巨大的 table 表单 mysql。 2. 我需要准备 xml 模式 - 一个 xsd 文件代表读取 mysql table.
- 现在,我需要以编程方式修改这个 xsd 即每次我可以从这个模式中添加或删除一些列并且
- 最后我需要从这个架构中准备一个数据table。
示例代码:
class Program
{
public static DataTable toBeUpdatedTable;
static void Main(string[] args)
{
// Deserialize the stored
string path = "D:\SerilaizedFile";
ReadMiscInfo(path);
string connectionString = ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString;
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand command;
connection.Open();
DataSet set = new DataSet();
try
{
command = connection.CreateCommand();
command.CommandText = "select * from my_db.table";
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
adapter.Fill(set);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
set.WriteXmlSchema("D:\file.xsd");
// Modify schema code
DataSet ds = new DataSet();
ds.ReadXmlSchema("D:\file.xsd");
if (toBeUpdatedTable.Columns.Count > 0)
{
DataTable tempTable = ds.Tables[0];
foreach (DataColumn col in tempTable.Columns)
{
if (!toBeUpdatedTable.Columns.Contains(col.ColumnName))
toBeUpdatedTable.Columns.Add(col.ColumnName, col.DataType);
}
Console.WriteLine("Table Updated");
}
else
{
toBeUpdatedTable = ds.Tables[0];
Console.WriteLine("Table Created");
}
foreach (DataColumn col in toBeUpdatedTable.Columns)
{
Console.WriteLine(col.ColumnName + " type : " + col.DataType);
}
WriteMiscInfo(path);
Console.ReadLine();
}
private static void WriteMiscInfo(string path)
{
if (path != null)
{
Stream s = new FileStream(path, FileMode.Create);
IFormatter f2 = new BinaryFormatter();
f2.Serialize(s, toBeUpdatedTable);
s.Close();
}
}
private static void ReadMiscInfo(string path)
{
if (File.Exists(path))
{
Stream s = new FileStream(path, FileMode.Open, FileAccess.Read);
IFormatter f1 = new BinaryFormatter();
try
{
toBeUpdatedTable = (DataTable)f1.Deserialize(s);
}
catch
{
s.Close();
toBeUpdatedTable = new DataTable();
}
s.Close();
}
else
{
toBeUpdatedTable = new DataTable();
}
}
}
帮我看看如何修改模式?
您可以使用此处提到的代码here。
我已经复制了你的代码并得到了解决方案。但是我无法维护数据表的记录。
class Program
{
static void Main(string[] args)
{
// Read a mysql table
string connectionString = ConfigurationManager.ConnectionStrings["MySQL"].ConnectionString;
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand command;
connection.Open();
DataSet set = new DataSet();
try
{
command = connection.CreateCommand();
//command.CommandText = "select * from toolsite.user limit 5";
command.CommandText = "select FirstName, LastName, PersonID, City from my_db.Persons limit 5";
MySqlDataAdapter adapter = new MySqlDataAdapter(command);
adapter.Fill(set);
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (connection.State == ConnectionState.Open)
{
connection.Close();
}
}
// Write Sxml Schema
set.WriteXmlSchema("D:\file.xsd");
// Modify schema code
FileStream fs = new FileStream("D:\file.xsd", FileMode.Open);
XmlSchema schema;
ValidationEventHandler eventHandler = new ValidationEventHandler(Program.ShowCompileErrors);
try
{
// Read the schema into an XMLSchema object.
schema = XmlSchema.Read(fs, eventHandler);
// Compile the schema.
schema.Compile(eventHandler);
// Define an XMLSchemaObjectTable to read the schema elements.
// This schematable will contain a single element named "NewDataSet" according to mySql syntax
XmlSchemaObjectTable schematable;
schematable = schema.Elements;
// Define a QualifiedName to identify the elements.
XmlQualifiedName qname = new XmlQualifiedName("NewDataSet", "");
XmlSchemaElement singleElement = new XmlSchemaElement();
XmlSchemaComplexType complextype = new XmlSchemaComplexType();
// See if the XmlSchemaObjectTable has the element.
if (schematable.Contains(qname))
{
singleElement = (XmlSchemaElement)schematable[qname];
if (singleElement.SchemaTypeName.ToString() == "")
{
complextype = (XmlSchemaComplexType)singleElement.SchemaType;
XmlSchemaChoice choice = new XmlSchemaChoice();
choice = (XmlSchemaChoice)complextype.Particle;
XmlSchemaObjectCollection collection = choice.Items;
XmlSchemaObject obj = collection[0];
XmlSchemaComplexType complexsubtype = (XmlSchemaComplexType)((XmlSchemaElement)obj).SchemaType;
XmlSchemaSequence seqelement = new XmlSchemaSequence();
seqelement = (XmlSchemaSequence)complexsubtype.Particle;
// Remove column here
XmlSchemaElement passwordElement = new XmlSchemaElement();
passwordElement = (XmlSchemaElement)seqelement.Items[1];
seqelement.Items.Remove(passwordElement);
// Add any new column here
XmlSchemaElement Newelement = new XmlSchemaElement();
Newelement.Name = "Country";
Newelement.MinOccurs = 0;
Newelement.SchemaTypeName = new XmlQualifiedName("string", "http://www.w3.org/2001/XMLSchema");
seqelement.Items.Add(Newelement);
}
}
// Display the schema in the Output window.
schema.Write(Console.Out);
Console.ReadLine();
// Save the modified schema
StreamWriter strmWrtr = new StreamWriter("D:\newfile.xsd", false);
schema.Write(strmWrtr);
strmWrtr.Close();
}
catch (XmlSchemaException schemaEx)
{
Console.WriteLine(schemaEx.Message);
}
catch (XmlException xmlEx)
{
Console.WriteLine(xmlEx.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
fs.Close();
}
// Read the modified schema
// PROBLEM HERE : My schema is modified but my data of table is lost. I want to just modify the columns .
//I dont want to lose data
set.ReadXmlSchema("D:\newfile.xsd");
foreach (DataColumn col in set.Tables[0].Columns)
{
Console.WriteLine(col.ColumnName + " type : " + col.DataType);
}
Console.WriteLine("Total records = " + set.Tables[0].Rows.Count);
foreach (DataRow row in set.Tables[0].Rows)
{
for(int i = 0; i < row.ItemArray.Count(); i++)
{
Console.Write(row.ItemArray[i] + " ");
}
Console.WriteLine();
}
Console.ReadLine();
}
public static void ShowCompileErrors(object sender, ValidationEventArgs args)
{
Console.WriteLine("ERROR : Validation Error: {0}", args.Message);
}
}
因此,现在您可能需要有关如何保存数据记录和数据列应根据架构更改的帮助。