c#每次读取一条记录插入xml中
public void mdbToXml()
{
OleDbConnection mycon = null;
try
{
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\1.mdb;";
mycon = new OleDbConnection(strcon);
mycon.Open();
//获取文件名列
string sql = "select 文件名 from temp";
OleDbDataAdapter ad = new OleDbDataAdapter(sql, strcon);
DataSet ds = new DataSet();
ad.Fill(ds,"文件名");
string[] filesName = new string[ds.Tables[0].Rows.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
filesName[i] = ds.Tables[0].Rows[i]["文件名"].ToString();
}
//获取单位编码列
string sql1 = "select 单位编码 from temp";
OleDbDataAdapter ad1 = new OleDbDataAdapter(sql1, strcon);
DataSet ds1 = new DataSet();
ad1.Fill(ds1);
string[] dwbm = new string[ds1.Tables[0].Rows.Count];
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
//dwbm = new string[ds.Tables[0].Rows.Count];
dwbm[i] = ds1.Tables[0].Rows[i]["单位编码"].ToString();
}
foreach (string dw in dwbm)//遍历所有文件名
{
textBox3.Text += dw;
}
//读取文件名并插入单位编码
foreach (string dir in filesName)//遍历所有文件名
{
string fn = @"D:\1" + dir;
string s = null;
XElement element = XElement.Load(fn);
foreach (string dw in dwbm)//遍历所有单位编码
{
s += dw;
element.Descendants("DataField").Descendants("Data").ToList().ForEach(item => { item.SetAttributeValue("ToCorpID",s); });
}
textBox4.Text = s;
element.Save(fn);
}
}
finally
{
mycon.Close();
}
}public void mdbToXml()
{
OleDbConnection mycon = null;
try
{
string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\1.mdb;";
mycon = new OleDbConnection(strcon);
mycon.Open();
//获取文件名列
string sql = "select 文件名 from temp";
OleDbDataAdapter ad = new OleDbDataAdapter(sql, strcon);
DataSet ds = new DataSet();
ad.Fill(ds,"文件名");
string[] filesName = new string[ds.Tables[0].Rows.Count];
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
filesName[i] = ds.Tables[0].Rows[i]["文件名"].ToString();
}
//获取单位编码列
string sql1 = "select 单位编码 from temp";
OleDbDataAdapter ad1 = new OleDbDataAdapter(sql1, strcon);
DataSet ds1 = new DataSet();
ad1.Fill(ds1);
string[] dwbm = new string[ds1.Tables[0].Rows.Count];
for (int i = 0; i < ds1.Tables[0].Rows.Count; i++)
{
//dwbm = new string[ds.Tables[0].Rows.Count];
dwbm[i] = ds1.Tables[0].Rows[i]["单位编码"].ToString();
}
foreach (string dw in dwbm)//遍历所有文件名
{
textBox3.Text += dw;
}
//读取文件名并插入单位编码
foreach (string dir in filesName)//遍历所有文件名
{
string fn = @"D:\1" + dir;
string s = null;
XElement element = XElement.Load(fn);
foreach (string dw in dwbm)//遍历所有单位编码
{
s += dw;
element.Descendants("DataField").Descendants("Data").ToList().ForEach(item => { item.SetAttributeValue("ToCorpID",s); });
}
textBox4.Text = s;
element.Save(fn);
}
}
finally
{
mycon.Close();
}
}
}
这个改下
for(int i =0;i< dwbm.length; i++)
{
s += dw;
element.Descendants("DataField").Descendants("Data").ToList().ForEach(item => { item.SetAttributeValue("ToCorpID",s); });
dwbm.Remove(dwbm[i]);
break;
}
textBox4.Text = s;
element.Save(fn);
[解决办法]
//读取文件名并插入单位编码
foreach (string dir in filesName)//遍历所有文件名
{
string fn = @"D:\1" + dir;
string s = null;
XElement element = XElement.Load(fn);
if(文件名对应编码条件成立)
{
foreach (string dw in dwbm)//遍历所有单位编码
{
s += dw;
element.Descendants("DataField").Descendants("Data").ToList().ForEach(item =>
{ item.SetAttributeValue("ToCorpID",s); });
}
textBox4.Text = s;
element.Save(fn);
}
}
[解决办法]
引用System.Xml和System.Xml.Linq2个dll系统文件到项目中
[解决办法]
using System;
using Entityies;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
namespace JYLib.Business
{
/// <summary>
/// 视频下载到本地,相关信息处理
/// </summary>
public class LocalCourseHelper : IDisposable
{
#region 实现接口
/// <summary>
/// 析构函数
/// </summary>
~LocalCourseHelper()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposing)
return;
}
#endregion
public LocalCourseHelper ()
{
}
/// <summary>
/// 初始化文件
/// </summary>
/// <description>>app启动的时候判断文件是否从存在并执行此方法</description>
public static void InitializeXmlFile()
{
XDocument doc = new XDocument (///创建XDocument类的实例
new XDeclaration ("1.0", "utf-8", "yes"),///XML的声明,包括版本,编码,xml文件是否独立
new XElement ("CourseInfo", null) ///添加根节点
);
///保存XML文件到指定地址
doc.Save (LocalCourseXmlPath);
}
/// <summary>
/// 1、创建 loadcourse.xml 文件,由函数 CreateXmlFile()完成
/// </summary>
/// <param name="model">下载的视频信息model.</param>
/// <remarks>调用之前先判断此文件是否存在</remarks>
public void CreateXmlFile(LocalCourseModel model)
{
XDocument doc = new XDocument (///创建XDocument类的实例
new XDeclaration ("1.0", "utf-8", "yes"),///XML的声明,包括版本,编码,xml文件是否独立
new XElement ("CourseInfo", ///添加根节点
new XElement ("Course", ///添加一个节点
new XAttribute ("Course_Number", model.Course_Number),///添加属性 Course_Number
new XElement ("Course_Name", model.Course_Name), ///添加元素 Course_Name
new XElement ("Course_LocalName", model.Course_LocalName), ///添加元素 Course_LocalName
new XElement ("Course_Image", model.Course_Image), ///添加元素 Course_Image
new XElement ("Course_TotalByte", model.Course_TotalByte),///添加元素 Course_TotalByte
new XElement ("Course_DownloadByte", model.Course_DownloadByte),///添加元素 Course_TotalByte
new XElement ("Load_DateTime", model.Load_DateTime)///添加元素 Load_DateTime
)
)
);
///保存XML文件到指定地址
doc.Save (LocalCourseXmlPath);
}
/// <summary>
/// 2、添加元素
/// </summary>
/// <param name="xmlpath">XML文件的路径</param>
public void AddXmlElement(LocalCourseModel model)
{
///导入XML文件
XElement xe = XElement.Load (LocalCourseXmlPath);
///创建一个新节点
XElement course = new XElement ("Course",
new XAttribute ("Course_Number", model.Course_Number),///添加属性 Course_Number
new XElement ("Course_Name", model.Course_Name),
new XElement ("Course_LocalName", model.Course_LocalName),
new XElement ("Course_Image", model.Course_Image),
new XElement ("Course_TotalByte", model.Course_TotalByte),
new XElement ("Course_DownloadByte", model.Course_DownloadByte),
new XElement ("Load_DateTime", model.Load_DateTime)
);
///添加节点到XML文件中,并保存
xe.Add (course);
///保存到XML文件中
xe.Save (LocalCourseXmlPath);
}
/// <summary>
/// 3、修改XML文件的元素
/// </summary>
/// <param name="strElement">指定的修改视频id</param>
public void ModifyXmlElement(string strElement, LocalCourseModel model)
{
XElement xe = XElement.Load (LocalCourseXmlPath);
///查询修改的元素
IEnumerable<XElement> element = from e in xe.Elements ("Course")
where e.Attribute ("Course_Number").Value == strElement
select e;
///修改元素
if (element.Count () > 0) {
XElement firstelement = element.First ();
///设置新的属性
firstelement.SetAttributeValue ("Course_Number", model.Course_Number);
///替换成新的节点
firstelement.ReplaceNodes (
new XElement ("Course_Name", model.Course_Name),
new XElement ("Course_LocalName", model.Course_LocalName),
new XElement ("Course_Image", model.Course_Image),
new XElement ("Course_TotalByte", model.Course_TotalByte),
new XElement ("Course_DownloadByte", model.Course_DownloadByte),
new XElement ("Load_DateTime", model.Load_DateTime)
);
}
xe.Save (LocalCourseXmlPath);
}
/// <summary>
/// 4、删除XML文件的元素
/// </summary>
/// <param name="strElement">指定删除视频的编号</param>
public void DeleteXmlElement(string strElement)
{
XElement xe = XElement.Load (LocalCourseXmlPath);
///查询修改的元素
IEnumerable<XElement> element = from e in xe.Elements ("Course")
where e.Attribute ("Course_Number").Value == strElement
select e;
///删除元素
if (element.Count () > 0) {
XElement firstelement = element.First ();
///删除此元素的所有节点和属性
firstelement.RemoveAll ();
///删除此元素的属性
//firstelement.RemoveAttributes();
///删除此元素的子节点
//firstelement.RemoveNodes();
}
xe.Save (LocalCourseXmlPath);
}
/// <summary>
/// 5、将XML文件中的属性更换成元素
/// </summary>
/// <param name="strAttribute">指定更换视频的编号属性</param>
public void ConvertAttributeToElement(string strAttribute)
{
XElement xe = XElement.Load (LocalCourseXmlPath);
///查询更换的元素
IEnumerable<XElement> element = from e in xe.Elements ("Course")
where e.Attribute ("Course_Number").Value == strAttribute
select e;
///更换为元素
if (element.Count () > 0) {
XElement firstelement = element.First ();
//获取第一个属性
XAttribute attr = firstelement.FirstAttribute;
//XAttribute attr = firstelement.Attribute("Course_Number");
///将属性转换成元素
firstelement.AddFirst (
new XElement (attr.Name, attr.Value)//添加 Course_Number 元素
);
///删除属性
firstelement.RemoveAttributes ();
}
xe.Save (LocalCourseXmlPath);
}
private static string LocalCourseXmlPath
{
get{
var documents = Environment.GetFolderPath (Environment.SpecialFolder.MyDocuments);
return System.IO.Path.Combine (documents, KeyCenter.Key_LoadCourseFile);
}
}
/// <summary>
/// 6、是否存在元素
/// </summary>
/// <param name="strElement">指定的视频id</param>
public static bool ExistElement(string strElement)
{
if (!System.IO.File.Exists (LocalCourseXmlPath)) {
return false;
}
XElement xe = XElement.Load (LocalCourseXmlPath);
///查询修改的元素
IEnumerable<XElement> element = from e in xe.Elements ("Course")
where e.Attribute ("Course_Number").Value == strElement
select e;
///修改元素
return element.Count () > 0;
}
/// <summary>
/// 获取所有下载过的视频信息
/// </summary>
public List<LocalCourseModel> GetLoadCourseList()
{
if (!System.IO.File.Exists (LocalCourseXmlPath)) {
return null;
}
XElement xe = XElement.Load (LocalCourseXmlPath);
///查询修改的元素
var q = from e in xe.Elements ("Course")
select new LocalCourseModel {
Course_Number = e.Attribute ("Course_Number").Value,
Course_Name = e.Element ("Course_Name").Value,
Course_LocalName = e.Element ("Course_LocalName").Value,
Course_Image = e.Element ("Course_Image").Value,
Course_TotalByte = e.Element ("Course_TotalByte").Value,
Course_DownloadByte = e.Element ("Course_DownloadByte").Value,
Load_DateTime = e.Element ("Load_DateTime").Value
};
return q.ToList ();
}
//...
}
}