请教关于c#中操作xml文件的问题

CNSC 2003-08-18 04:10:00
虚心请教关于c#操作xml文件(增删改查,还有新建)等操作.
最好给一个详细的例子
...全文
97 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
dotnba 2003-08-22
  • 打赏
  • 举报
回复
不错,很完整啊
win911 2003-08-19
  • 打赏
  • 举报
回复
如果会使用dataset,简单的直接使用dataset.ReadXml把xml文件的内容读到dataset中,对dataset操作结束后使用dataset.writexml在写回xml文件就可以了
雪狼1234567 2003-08-18
  • 打赏
  • 举报
回复
最常用的也就如下的几种:
1.。。
using System;
using System.Data;
using System.IO;
using System.Xml;
DataSet myDataSet=new DataSet();
FileStream fsReadXml=new FileStream(myFilename,FileMode.Open);
XmlTextReader myXmlReader=new XmlTextReader(fsReadXml);
myDataSet.ReadXml(myXmlReader);
myXmlReader.Close();
//提出当前选票的个数的字符
string votes=myDataSet.Tables[1].Rows[answer-1].ItemArray[1].ToString();
int votesInt=int.Parse(votes);//转化为整数
//int votesInt=Votes[answer];
DataRow myVotesRow=myDataSet.Tables[1].Rows[answer-1];//提取当前操作行
myVotesRow["Votes"]=(votesInt+1).ToString();//修改
StreamWriter myStream=new StreamWriter(myFilename);//建立写流
myDataSet.WriteXml(myStream,XmlWriteMode.IgnoreSchema);//保存文件
myStream.Close();
2....
//读节点1----------------------------------------------------------------
// XmlTextReader tr=new XmlTextReader("books.xml");
// while(tr.Read())
// {
// if(tr.NodeType==XmlNodeType.Text)
// listBox1.Items.Add(tr.Value);
// }
//----------------------------------------------------------------------
//读属性
FileStream fs=new FileStream("books.xml",FileMode.Open);
XmlTextReader tr=new XmlTextReader(fs);
while(tr.Read())
{
if(tr.MoveToContent()==XmlNodeType.Element)
{

for(int i=0;i<tr.AttributeCount;i++)
{
listBox1.Items.Add(tr.GetAttribute(i));
}
}

}
//读节点2----------------------------------------------------------------
private void button1_Click(object sender, System.EventArgs e)
{
FileStream fs=new FileStream("books.xml",FileMode.Open);
XmlTextReader tr=new XmlTextReader(fs);
while(!tr.EOF)
{
if(tr.MoveToContent()==XmlNodeType.Element&&tr.Name=="title")

{
//从元素中读取文本数据
//listBox1.Items.Add(tr.ReadElementString());//自动定位到下一个节点
LoadList(tr);//忽略异常

}
else
{
tr.Read();
}
}
}
private void LoadList(XmlReader reader)
{
try
{
listBox1.Items.Add(reader.ReadElementString());//自动定位到下一个节点
}
catch(XmlException er){}//ignore

}
//----------------------------------------------------------------------
3....
using System.IO;
using System.Xml;
------------------------
private void FormHistory_Load(object sender, System.EventArgs e)
{
//初始化ListView控件
listViewReco.View=View.Details;
listViewReco.FullRowSelect=true;
this.listViewReco.Columns.Add("姓名",100,HorizontalAlignment.Left);
this.listViewReco.Columns.Add("移动电话",100,HorizontalAlignment.Left);
this.listViewReco.Columns.Add("发送内容",500,HorizontalAlignment.Left);
this.listViewReco.Columns.Add("发送时间",500,HorizontalAlignment.Left);
//
LoadHistory();

}
--------------------------
private void LoadHistory()
{
//填充数据
listViewReco.Items.Clear();
listViewReco.BeginUpdate();

//
FileStream fs=new FileStream("history.xml",FileMode.Open);
XmlTextReader tr=new XmlTextReader(fs);
while(!tr.EOF)
{
if(tr.MoveToContent()==XmlNodeType.Element&&tr.Name=="record")

{
LoadList(tr);//忽略异常
}
else
{
tr.Read();
}
}

listViewReco.EndUpdate();
}
--------------------
private void LoadList(XmlReader reader)
{
try
{
ListViewItem lvi;
lvi = new ListViewItem();
for(int i=0;i<reader.AttributeCount;i++)
{

if(i==0)
lvi.Text=reader.GetAttribute(i).ToString();
else
lvi.SubItems.Add(reader.GetAttribute(i).ToString());

}
listViewReco.Items.Add(lvi);
reader.ReadElementString();

}
catch(XmlException er){}//ignore

}
------------------------
csharplove 2003-08-18
  • 打赏
  • 举报
回复
我每次对XML操作几乎都用XmlDocument类,功能强大易用:
XmlDocument doc=new XmlDocument();
doc.load(filename);
里面有足够多的方法和属性
也可以用XmlTextWriter,控制更灵活,功能更强大
godliu521 2003-08-18
  • 打赏
  • 举报
回复
和你一同学习了,呵呵
colin666 2003-08-18
  • 打赏
  • 举报
回复
/// <summary>
/// XmlConfig 的摘要说明。
/// </summary>
public class XmlConfig
{
private XmlDocument doc;
private string xmlFileName;
private MemoryStream xmlStream;
private byte[] xmlcontent = new byte[0];
public byte[] Content
{
get
{
return System.Text.Encoding.Default.GetBytes(doc.InnerXml);
}
set
{
}
}
public XmlConfig(string filename)
{

xmlFileName = filename;
doc = new XmlDocument();
try
{
doc.Load(xmlFileName);
}
catch
{
doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><Settings></Settings>");
}
}

public XmlConfig(byte[] contain)
{
xmlStream = new MemoryStream();

xmlStream.Write(contain,0,contain.Length);
// MemoryStream stream =new MemoryStream(contain);
// xmlStream = (Stream) stream;
doc = new XmlDocument();

try
{
if(contain.Length==0)
doc.Load(xmlStream);
else
{
string xml = System.Text.Encoding.Default.GetString(contain);
doc.LoadXml(xml);
}
}
catch(Exception ee)
{
doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><Settings></Settings>");
}
}
public void Save()
{
try
{
doc.Save(xmlFileName);
}
catch
{
}
}
public void StreamSave()
{
try
{
doc.Save(xmlStream);
}
catch(Exception ee)
{
Console.WriteLine(ee.ToString());

}

}

public string Read(string key, string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
return node.InnerText;
else
return value;
}

public void Write(string key, string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
{
node.InnerText = value;
}
else
{
node = doc.DocumentElement;
string[] path = key.Split(new char[] {'/'});
for (int i = 0; i < path.Length; i++)
{
XmlNode node2;
if ( (node2 = node.SelectSingleNode(path[i])) == null)
{
node2 = doc.CreateElement(path[i]);
node.AppendChild(node2);
}
node = node2;
}
node.InnerText = value;
}
}

//增加一个父节点
public void Write(string key)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
{
}
else
{
node = doc.DocumentElement;
string[] path = key.Split(new char[] {'/'});
for (int i = 0; i < path.Length; i++)
{
XmlNode node2;
if ( (node2 = node.SelectSingleNode(path[i])) == null)
{
node2 = doc.CreateElement(path[i]);
node.AppendChild(node2);
}
node = node2;
}
}

}
public void ChildWrite(string key,string Childkey,string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
XmlNode node2=node.SelectSingleNode(Childkey);
if(node2!=null)
{
node2.InnerText=value;
}
else
{
XmlNode node3=doc.CreateElement(Childkey);

node.PrependChild(node3);
node3.InnerText=value;

}
}
//key为父节点的值,chlidkey为子节点的值。
public string Read(string key,string Childkey,string value)
{

XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if(node!=null)
{
XmlNode node2=node.SelectSingleNode(Childkey);
if(node2!=null)
{
return node2.InnerText;
}
else
return value;
}
else
return value;
}

public XmlNodeList NodeRead()
{
XmlNode node = doc.ChildNodes[1];
XmlNodeList nodelist = node.ChildNodes;
return nodelist;
}
}
RLearnP 2003-08-18
  • 打赏
  • 举报
回复
using System.Xml;

string sFileName =Application.StartupPath+"\\file.xml";
XmlDocument myDoc = new XmlDocument();
if(System.IO.File.Exists(ss)) //如果存在
{
myDoc.Load(ss);
//读写该文件
int i=1;
XmlNode myNode;
myNode = myDoc.DocumentElement.ChildNodes[i];
//读写myNode

}
else
{
//新建元素
XmlElement tempElement = myDoc.CreateElement("新建1");
...
}

myDoc.Save("file.xml");
mfsto 2003-08-18
  • 打赏
  • 举报
回复
查查以前的贴子,有很多相关的
最近一直在研究爬虫和Lucene,虽然开始决定选用Heritrix来执行爬虫操作,但是后来发现用它来做还是存在一定的问题,比如需要程序生成相应的XML文件,对于同一个Job,怎样才能保证重复运行该Job时文件夹始终是同一个(Heritrix为Job创建文件夹的规则是“Job名称-时间戳”)等等,都是需要考虑的问题,最终还是将其搁浅。    后来google了一下,找到了一个简单爬虫的程序代码(http://www.blogjava.net/Jack2007/archive/2008/03/24/188138.html),随即试验了一下,发现确实能得到网页的内容,在这里还是要谢谢代码的提供者——Jack.Wang。    虽然试验成功,但是在随后的大数据量试验时,还是出现了问题。最初试验时,我只是让程序去抓取10个URL链接,当我将URL链接数改为100个时,问题出现了——URL存在重复,而且非常容易的就变成死循环。举个例子来说,比如我首先爬的是A.html,在A.html有两个链接:B.html,C.html,等爬完A.html以后,程序会爬B.html,这时如果B.html的所有链接有A.html这个页面的链接,那么程序又会去爬A.html这个页面,如此一来就形成了一个死循环,永远也不能停止。    跟踪程序发现,原来是在添加要抓取的网页的链接列表,没有将已经抓取过的URL去除,所以才造成了死循环。现在虽然加上了这个判断,但是从我运行程序的效果来看,也不是很理想,总是感觉有些慢,800个页面要一两分钟才能爬完,这个我觉得有点说不过去。    这个产品,做到现在,我遇到了这么几个情况,有和大家分享的,也有向大家请教,求助的。    1.关于对应关系数据的保存方式    在创建索引的时候,需要将网页的URL和网页的内容传到相应的方法,当然URL和内容是要对应的,也许是经验太少吧,我采取的是通过构建一个JavaBean的方式来传递的,不知道大家有没有更好的方法       2.关于要创建索引的内容的保存方式    最初的想法是不创建文件,直接将内容保存到变量,然后创建索引,即先抓取网页的内容,然后将网页的内容和URL保存到自己构建的JavaBean对象,接着将这个对象放到一个list列表,等所有网页抓取完毕以后,将这个列表传到创建索引的方法。这种做法看似不错,可是当URL数量很大时,会导致内存不够用,所以还是创建文件比较稳妥。    3.关于网页编码问题    遇到这个问题也是一个巧合,本来我抓取的是客户的一个网站,后来同事说如果客户看访问日志,这个月的数据会和平常的数据不一样,所以我就抓取公司的网站,结果,问题出现了。原先公司的网站是用GB2312编码做的页面,现在采用的是UTF-8的编码,虽然我已经判断了页面的编码,可是依然不能解决保存的文件文乱码的问题,不知道大家有什么好办法没有。错误信息为:java.io.UnsupportedEncodingException    附件为爬虫代码 本文出自 “徘徊在c#,java,php之间” 博客,请务必保留此出处http://jerrysun.blog.51cto.com/745955/221879

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧