XML数据从数据库中读取(XML参考YTBank.Xml)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
class Program
{
static void Main(string[] args)
{
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\")
|| dataDir.EndsWith(@"\bin\Release\"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
}
using (IDbConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\YTBank.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
IDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from T_Records";
using (IDataReader reader = cmd.ExecuteReader())
{
XElement cfx = new XElement("CFX");
while (reader.Read())
{
XElement msg = new XElement("MSG");
XElement e交易码 = new XElement("交易码");
e交易码.SetAttributeValue("val",reader["TradeNo"]);
msg.Add(e交易码);
XElement e流水号 = new XElement("流水号");
e流水号.SetAttributeValue("val", reader["SeqNo"]);
msg.Add(e流水号);
XElement e金额 = new XElement("金额");
e金额.SetAttributeValue("val", reader["Amount"]);
msg.Add(e金额);
XElement e付款机构 = new XElement("付款机构");
e付款机构.SetAttributeValue("val", reader["PayCompany"]);
msg.Add(e付款机构);
XElement e付款单位账号 = new XElement("付款单位账号");
e付款单位账号.SetAttributeValue("val", reader["PayAccount"]);
msg.Add(e付款单位账号);
XElement e收款机构 = new XElement("收款机构");
e收款机构.SetAttributeValue("val", reader["RecvCompany"]);
msg.Add(e收款机构);
XElement e收款单位账号 = new XElement("收款单位账号");
e收款单位账号.SetAttributeValue("val", reader["RecAccount"]);
msg.Add(e收款单位账号);
cfx.Add(msg);
}
Console.WriteLine(cfx.ToString());
}
}
Console.ReadKey();
}
}
{
static void Main(string[] args)
{
string dataDir = AppDomain.CurrentDomain.BaseDirectory;
if (dataDir.EndsWith(@"\bin\Debug\")
|| dataDir.EndsWith(@"\bin\Release\"))
{
dataDir = System.IO.Directory.GetParent(dataDir).Parent.Parent.FullName;
AppDomain.CurrentDomain.SetData("DataDirectory", dataDir);
}
using (IDbConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\YTBank.mdf;Integrated Security=True;User Instance=True"))
{
conn.Open();
IDbCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from T_Records";
using (IDataReader reader = cmd.ExecuteReader())
{
XElement cfx = new XElement("CFX");
while (reader.Read())
{
XElement msg = new XElement("MSG");
XElement e交易码 = new XElement("交易码");
e交易码.SetAttributeValue("val",reader["TradeNo"]);
msg.Add(e交易码);
XElement e流水号 = new XElement("流水号");
e流水号.SetAttributeValue("val", reader["SeqNo"]);
msg.Add(e流水号);
XElement e金额 = new XElement("金额");
e金额.SetAttributeValue("val", reader["Amount"]);
msg.Add(e金额);
XElement e付款机构 = new XElement("付款机构");
e付款机构.SetAttributeValue("val", reader["PayCompany"]);
msg.Add(e付款机构);
XElement e付款单位账号 = new XElement("付款单位账号");
e付款单位账号.SetAttributeValue("val", reader["PayAccount"]);
msg.Add(e付款单位账号);
XElement e收款机构 = new XElement("收款机构");
e收款机构.SetAttributeValue("val", reader["RecvCompany"]);
msg.Add(e收款机构);
XElement e收款单位账号 = new XElement("收款单位账号");
e收款单位账号.SetAttributeValue("val", reader["RecAccount"]);
msg.Add(e收款单位账号);
cfx.Add(msg);
}
Console.WriteLine(cfx.ToString());
}
}
Console.ReadKey();
}
}