public static DataSet ConvertXMLFileToDataSet(string xmlFile)
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
XmlDocument xmld = new XmlDocument();
xmld.Load(xmlFile); DataSet xmlDS = new DataSet();
stream = new StringReader(xmld.InnerXml);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
//xmlDS.ReadXml(xmlFile);
return xmlDS;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (reader != null)
reader.Close();
}
} private void button23_Click(object sender, EventArgs e)
{
DataSet dt = ConvertXMLFileToDataSet("3.xml");
for (int i = 0; i < dt.Tables["Data"].Rows.Count; i++)
{
textBox1.Text+= dt.Tables["Data"].Rows[i]["CorpOrderID"].ToString()+"\n\r";
} }
{
StringReader stream = null;
XmlTextReader reader = null;
try
{
XmlDocument xmld = new XmlDocument();
xmld.Load(xmlFile); DataSet xmlDS = new DataSet();
stream = new StringReader(xmld.InnerXml);
//从stream装载到XmlTextReader
reader = new XmlTextReader(stream);
xmlDS.ReadXml(reader);
//xmlDS.ReadXml(xmlFile);
return xmlDS;
}
catch (System.Exception ex)
{
throw ex;
}
finally
{
if (reader != null)
reader.Close();
}
} private void button23_Click(object sender, EventArgs e)
{
DataSet dt = ConvertXMLFileToDataSet("3.xml");
for (int i = 0; i < dt.Tables["Data"].Rows.Count; i++)
{
textBox1.Text+= dt.Tables["Data"].Rows[i]["CorpOrderID"].ToString()+"\n\r";
} }
System.Xml.Linq.XDocument doc = System.Xml.Linq.XDocument.Load("e:\\1.txt");
foreach (var employee in doc.Document.Descendants("DataField").First().Descendants("Data"))
{
MessageBox.Show(employee.Attribute("CorpOrderID").Value);
}
* 本内容使用CSDN 小秘书回复 *
* 每天回帖即可得10分可用分! *
*****************************[/align]
Where(x => (string)x.Attribute("Code") == "81143900028040405694").FirstOrDefault();
if (query != null)
{
query.SetAttributeValue("ToCorpID", "000003202879");
}
Where(x => (string)x.Attribute("Code") == "81143900028040405694").FirstOrDefault();
if (query != null)
{
query.SetAttributeValue("ToCorpID", "000003202879");
}
同去加一句:
element.Save(@"c:\1.xml");
Where(x => (string)x.Attribute("Code") == "81143900028040405694").FirstOrDefault();
if (query != null)
{
query.SetAttributeValue("ToCorpID", "000003202879");
}
7楼说的没错,不过我想把
<Data Code="81143900028040251957" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027511806124" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027510453880" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027509255484" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027508057635" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027508204343" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027509403677" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027510601766" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027511950686" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900028040405694" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
这几行后边全加上ToCorpID="000003202879"属性,怎么写个循环呀?
Where(x => (string)x.Attribute("Code") == "81143900028040405694").FirstOrDefault();
if (query != null)
{
query.SetAttributeValue("ToCorpID", "000003202879");
}
7楼说的没错,不过我想把
<Data Code="81143900028040251957" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027511806124" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027510453880" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027509255484" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027508057635" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027508204343" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027509403677" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027510601766" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027511950686" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900028040405694" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
这几行后边全加上ToCorpID="000003202879"属性,怎么写个循环呀?你指的节点 DataField 下面所有的Data节点都要加吗?
.ForEach(item => { item.SetAttributeValue("ToCorpID", "000003202879"); });
<Data Code="81143900028040251957" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027511806124" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027510453880" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027509255484" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027508057635" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027508204343" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027509403677" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027510601766" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900027511950686" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
<Data Code="81143900028040405694" ReplaceCode="" CorpOrderID="138204" Actor="1" ActDate="2013-12-19 08:47:09" WrongCode="" UpperCorpOrderID="" />
后边全部加上ToCorpID="000003202879"属性怎么写呀?
for (int i=1;i<6;i++
{
XElement element = XElement.Load(@"c:\"+i+".xml");
element.Descendants("DataField").Descendants("Data").ToList()
.ForEach(item => { item.SetAttributeValue("ToCorpID", "000003202879"); });
}
{
XElement element = XElement.Load(@"c:\"+i+".xml");
element.Descendants("DataField").Descendants("Data").ToList()
.ForEach(item => { item.SetAttributeValue("ToCorpID", "000003202879"); });
}
for (int i=1;i<6;i++)
{
XElement element = XElement.Load(@"c:\"+i+".xml");
element.Descendants("DataField").Descendants("Data").ToList()
.ForEach(item => { item.SetAttributeValue("ToCorpID", "000003202879"); });
element.Save(@"c:\"+i+".xml");
}
SalesWareHouseOut_138375.xml,SalesWareHouseOut_138402.xml,SalesWareHouseOut_138404.xml,SalesWareHouseOut_138405.xml,SalesWareHouseOut_138411.xml,而且有几百个,怎么弄呀?
SalesWareHouseOut_138375.xml,SalesWareHouseOut_138402.xml,SalesWareHouseOut_138404.xml,SalesWareHouseOut_138405.xml,SalesWareHouseOut_138411.xml,而且有几百个,怎么弄呀?比如你在搜索C盘下面的XML文件,只需要
string[] _files = Directory.GetFiles("C:\\", "*.xml");
然后在遍历就好了。
FileInfo[] fileInfo = dir.GetFiles("*.xml");
foreach (FileSystemInfo f in fileInfo)
{
XElement element = XElement.Load(f.FullName);
element.Descendants("DataField").Descendants("Data").ToList()
.ForEach(item => { item.SetAttributeValue("ToCorpID", "000003202879"); });
element.Save(f.FullName);
}
SalesWareHouseOut_138375.xml,SalesWareHouseOut_138402.xml,SalesWareHouseOut_138404.xml,SalesWareHouseOut_138405.xml,SalesWareHouseOut_138411.xml,而且有几百个,怎么弄呀?比如你在搜索C盘下面的XML文件,只需要
string[] _files = Directory.GetFiles("C:\\", "*.xml");
然后在遍历就好了。
private void ShowSchemaButton_Click(object sender, EventArgs e)
{
string[] files = Directory.GetFiles(@"C:\Users\Administrator\Desktop\销售单", "*.xml");//获取目录下所有文件名
foreach (string dir in files)//遍历所有文件名
{
XElement element = XElement.Load(dir);
element.Descendants("DataField").Descendants("Data").ToList().ForEach(item => { item.SetAttributeValue("ToCorpID", "000003202879"); });
element.Save(dir);
}
}
这么些对不对呀?怎么运行后没反应?
SalesWareHouseOut_138375.xml,SalesWareHouseOut_138402.xml,SalesWareHouseOut_138404.xml,SalesWareHouseOut_138405.xml,SalesWareHouseOut_138411.xml,而且有几百个,怎么弄呀?比如你在搜索C盘下面的XML文件,只需要
string[] _files = Directory.GetFiles("C:\\", "*.xml");
然后在遍历就好了。
private void ShowSchemaButton_Click(object sender, EventArgs e)
{
string[] files = Directory.GetFiles(@"C:\Users\Administrator\Desktop\销售单", "*.xml");//获取目录下所有文件名
foreach (string dir in files)//遍历所有文件名
{
XElement element = XElement.Load(dir);
element.Descendants("DataField").Descendants("Data").ToList().ForEach(item => { item.SetAttributeValue("ToCorpID", "000003202879"); });
element.Save(dir);
}
}
这么些对不对呀?怎么运行后没反应?
你看你的文件,数据已添加了
SalesWareHouseOut_138375.xml,SalesWareHouseOut_138402.xml,SalesWareHouseOut_138404.xml,SalesWareHouseOut_138405.xml,SalesWareHouseOut_138411.xml,而且有几百个,怎么弄呀?比如你在搜索C盘下面的XML文件,只需要
string[] _files = Directory.GetFiles("C:\\", "*.xml");
然后在遍历就好了。
private void ShowSchemaButton_Click(object sender, EventArgs e)
{
string[] files = Directory.GetFiles(@"C:\Users\Administrator\Desktop\销售单", "*.xml");//获取目录下所有文件名
foreach (string dir in files)//遍历所有文件名
{
XElement element = XElement.Load(dir);
element.Descendants("DataField").Descendants("Data").ToList().ForEach(item => { item.SetAttributeValue("ToCorpID", "000003202879"); });
element.Save(dir);
}
}
这么些对不对呀?怎么运行后没反应?
你看你的文件,数据已添加了
运行没问题了,还有个问题,我想读取SalesWareHouseOut_138375.xml,SalesWareHouseOut_138402.xml,SalesWareHouseOut_138404.xml,SalesWareHouseOut_138405.xml,SalesWareHouseOut_138411.xml,这几个文件中的CorpOrderID属性怎么写呀?是不是和添加的类似?
就算你不看一下上面的代码,你也自己练习一下读写XML啊。
不能什么问题都来问的,自己先实践一下。。
SalesWareHouseOut_138375.xml,SalesWareHouseOut_138402.xml,SalesWareHouseOut_138404.xml,SalesWareHouseOut_138405.xml,SalesWareHouseOut_138411.xml,而且有几百个,怎么弄呀?比如你在搜索C盘下面的XML文件,只需要
string[] _files = Directory.GetFiles("C:\\", "*.xml");
然后在遍历就好了。
private void ShowSchemaButton_Click(object sender, EventArgs e)
{
string[] files = Directory.GetFiles(@"C:\Users\Administrator\Desktop\销售单", "*.xml");//获取目录下所有文件名
foreach (string dir in files)//遍历所有文件名
{
XElement element = XElement.Load(dir);
element.Descendants("DataField").Descendants("Data").ToList().ForEach(item => { item.SetAttributeValue("ToCorpID", "000003202879"); });
element.Save(dir);
}
}
这么些对不对呀?怎么运行后没反应?
你看你的文件,数据已添加了
运行没问题了,还有个问题,我想读取SalesWareHouseOut_138375.xml,SalesWareHouseOut_138402.xml,SalesWareHouseOut_138404.xml,SalesWareHouseOut_138405.xml,SalesWareHouseOut_138411.xml,这几个文件中的CorpOrderID属性怎么写呀?是不是和添加的类似?
方法:让CorpOrderID的值等于:"138206”
query.SetAttributeValue("CorpOrderID", "138206"); XElement element = XElement.Load("XMLFile1.xml");
var query = element.Descendants("DataField").Descendants("Data").
Where(x => (string)x.Attribute("Code") == "81143900028040405694").FirstOrDefault();
if (query != null)
{
query.SetAttributeValue("CorpOrderID", "138206");
}
element.Save("XMLFile1.xml");
SalesWareHouseOut_138375.xml,SalesWareHouseOut_138402.xml,SalesWareHouseOut_138404.xml,SalesWareHouseOut_138405.xml,SalesWareHouseOut_138411.xml,而且有几百个,怎么弄呀?比如你在搜索C盘下面的XML文件,只需要
string[] _files = Directory.GetFiles("C:\\", "*.xml");
然后在遍历就好了。
private void ShowSchemaButton_Click(object sender, EventArgs e)
{
string[] files = Directory.GetFiles(@"C:\Users\Administrator\Desktop\销售单", "*.xml");//获取目录下所有文件名
foreach (string dir in files)//遍历所有文件名
{
XElement element = XElement.Load(dir);
element.Descendants("DataField").Descendants("Data").ToList().ForEach(item => { item.SetAttributeValue("ToCorpID", "000003202879"); });
element.Save(dir);
}
}
这么些对不对呀?怎么运行后没反应?
你看你的文件,数据已添加了
运行没问题了,还有个问题,我想读取SalesWareHouseOut_138375.xml,SalesWareHouseOut_138402.xml,SalesWareHouseOut_138404.xml,SalesWareHouseOut_138405.xml,SalesWareHouseOut_138411.xml,这几个文件中的CorpOrderID属性怎么写呀?是不是和添加的类似?
方法:让CorpOrderID的值等于:"138206”
query.SetAttributeValue("CorpOrderID", "138206"); XElement element = XElement.Load("XMLFile1.xml");
var query = element.Descendants("DataField").Descendants("Data").
Where(x => (string)x.Attribute("Code") == "81143900028040405694").FirstOrDefault();
if (query != null)
{
query.SetAttributeValue("CorpOrderID", "138206");
}
element.Save("XMLFile1.xml");
这个不用foreach遍历所有文件吗?
SalesWareHouseOut_138375.xml,SalesWareHouseOut_138402.xml,SalesWareHouseOut_138404.xml,SalesWareHouseOut_138405.xml,SalesWareHouseOut_138411.xml这几个文件的CorpOrderID属性都不一样,我想根据不同的CorpOrderID属性添加ToCorpID属性行不行?